(Android review) File read/write (File Operations), androidreview

Source: Internet
Author: User

(Android review) File read/write (File Operations), androidreview

Files operated in Android can be divided into File, XML, and SharedPreference.


This blog mainly introduces File operations:


1. MainActivity

Package com. example. filetest; import android. OS. bundle; import android. OS. environment; import android. app. activity; import android. view. menu; import android. view. view; import android. widget. button; import android. widget. editText; import android. widget. toast; public class MainActivity extends Activity {private EditText et_name; private EditText et_content; private Button sdBtn; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. main); et_name = (EditText) findViewById (R. id. nameET); et_content = (EditText) findViewById (R. id. contentET); sdBtn = (Button) findViewById (R. id. sdcBT) ;}@ Overrideprotected void onResume () {// This method is executed when the interface is switched to the foreground... super. onResume (); // set whether the button is available based on the SD card status. if (! Environment. getExternalStorageState (). equals (Environment. MEDIA_MOUNTED) {sdBtn. setEnabled (false);} else {sdBtn. setEnabled (true) ;}} public void onClick (View view) {String name = et_name.getText (). toString (); String content = et_content.getText (). toString (); FileService service = new FileService (this); try {switch (view. getId () {case R. id. romBT: System. out. println ("---------> Save to rom"); service. saveToRom (name, content); break; case R. id. sdcBT: System. out. println ("---------> Save to SD card"); service. saveToSD (name, content); break;} Toast. makeText (getApplicationContext (), "saved successfully", 1 ). show ();} catch (Exception e) {e. printStackTrace (); Toast. makeText (getApplicationContext (), "failed to save", 1 ). show () ;}@ Overridepublic boolean onCreateOptionsMenu (Menu menu) {// Inflate the menu; this adds items to the action bar if it is present. getMenuInflater (). inflate (R. menu. main, menu); return true ;}}


2. FileService

Package com. example. filetest; import java. io. fileOutputStream; import android. content. context; import android. OS. environment; public class FileService {private Context context; public FileService (Context context) {this. context = context;} public void saveToSD (String name, String content) throws Exception {/*** create an output stream and specify the location of the SD card * Environment. getExternalStorageDirectory (): Location of the SD card,/mnt/sdcard **/FileOutputStream fos = new FileOutputStream (Environment. getExternalStorageDirectory () + "/" + name); fos. write (content. getBytes (); fos. close ();} public void saveToRom (String name, String content) throws Exception {/*** if enabled successfully, you will create a file directory under/data/package name/and put the file in ** Context. MODE_PRIVATE: only the user can access the created file * Context. MODE_WORLD_READABLE: the world can read, that is, everyone can read * Context. MODE_WORLD_WRITEABLE: can be written all over the world, that is, anyone can write. * Context. MODE_WORLD_READABLE + Context. MODE_WORLD_WRITEABLE: reads and writes can be performed all over the world... * Context. MODE_APPEND: Private & append only. MODE_PRIVATE. At this time, other programs cannot read or write data, but they can only read and write data themselves... */FileOutputStream fos = context. openFileOutput (name, Context. MODE_WORLD_READABLE); // open the file fos for/data/Application Registration. write (content. getBytes (); // write content to ROM without permission fos. close ();}}


3. FileTest (used for testing)

Package com. example. filetest; import java. io. byteArrayOutputStream; import java. io. fileInputStream; import java. io. fileOutputStream; import android. content. context; import android. test. androidTestCase; public class FileTest extends AndroidTestCase {/*** read by yourself (normal result: Successful ). used to test Context. whether MODE_PRIVATE works correctly * @ throws Exception */public void testRead () throws Exception {FileInputStream FCM = getContext (). openFile Input ("wr.txt"); // FileInputStream in = new FileInputStream ("/data/com. exmaple. filetest/files/hjd.txt "); // This method is equivalent to the preceding method: ByteArrayOutputStream baos = new ByteArrayOutputStream (); byte [] buffer = new byte [1024]; int len; while (len = Fi. read (buffer ))! =-1) {baos. write (buffer, 0, len);} FD. close (); baos. close (); String content = new String (baos. toByteArray (); System. out. println ("----> content:" + content);} public void testWrite () throws Exception {FileOutputStream fos = new FileOutputStream ("/data/com. example. filetest/files/hjd.txt "); String content =" hahahahahah "; fos. write (content. getBytes (); fos. close ();}}


II,


Several Modes of Context can be viewed from/data/Application name/files/file in DDMS.





Source code download: http://download.csdn.net/detail/caihongshijie6/7613405







Android file: // Where is the path in the project file?

Click "Window"-"Show View"-"Other" in the Eclipse menu, expand the android folder in the dialog box, and select the File Explorer View below, expand the/data/<package name>/files directory in the File Explorer view to view the File.

Where is the file read/write stored in android real machine debugging? The Virtual Machine path is/data/<package name>/file/filename.

If the real machine does not have root, the folder DDMS cannot be viewed.
Of course, the application can write in it

It is better to apply for sdcard permissions to/mnt/sdcard/filename (more than 2.2)/sdcard/filename (less than 2.1) for debugging)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.