Android file IO

Source: Internet
Author: User

[Context method: The data folder of this application]

Public FileInputStreamOpenFileInput(String name)
Throws FileNotFoundException


Public FileOutputStreamOpenFileOutput(String name, int mode)
Throws FileNotFoundException


Package org. crazyit. io; import java. io. fileInputStream; import java. io. fileOutputStream; import java. io. printStream; import android. app. activity; import android. OS. bundle; import android. view. view; import android. view. view. onClickListener; import android. widget. button; import android. widget. editText; public class FileTest extends Activity {final String FILE_NAME = "crazyit. bin "; @ Overridepublic void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. main); System. out. println (new StringBuilder (""). append ("B "). append ("c "). toString (); // obtain two buttons: Button read = (Button) findViewById (R. id. read); Button write = (Button) findViewById (R. id. write); // get the final EditText edit1 = (EditText) findViewById (R. id. edit1); final EditText edit2 = (EditText) findViewById (R. id. edit2); // bind the event listener write for the write button. setOnClickListener (new OnClickListener () {@ Overridepublic void onClick (View source) {// write the content in edit1 into the file write (edit1.getText (). toString (); edit1.setText ("") ;}}); read. setOnClickListener (new OnClickListener () {@ Overridepublic void onClick (View v) {// read the content of the specified file and display edit2.setText (read ());}});} private String read () {try {// open the file input stream FileInputStream FD = openFileInput (FILE_NAME); byte [] buff = new byte [1024]; int hasRead = 0; stringBuilder sb = new StringBuilder (""); // read the file content while (hasRead = Fi. read (buff)> 0) {sb. append (new String (buff, 0, hasRead);} // close the input stream of the file. close (); return sb. toString ();} catch (Exception e) {e. printStackTrace ();} return null;} private void write (String content) {try {// open the file output stream FileOutputStream fos = openFileOutput (FILE_NAME, MODE_APPEND) in append mode ); // package FileOutputStream into PrintStreamPrintStream ps = new PrintStream (fos); // output file content ps. println (content); // close the file output stream ps. close ();} catch (Exception e) {e. printStackTrace ();}}}

Data Files of applications are stored in/data/by default/ /Files directory.



[Read and write files on the SD card]



Private String read () {try {// if the mobile phone is inserted with an SD card, and the application has the permission to access the SD if (Environment. getExternalStorageState (). equals (Environment. MEDIA_MOUNTED) {// obtain the storage directory File sdCardDir = Environment corresponding to the SD card. getExternalStorageDirectory (); // obtain the input stream FileInputStream (new FileInputStream (sdCardDir. getCanonicalPath () + FILE_NAME); // wrap the specified input stream into BufferedReaderBufferedReader br = new BufferedReader (new InputStreamReader (FCM); Str IngBuilder sb = new StringBuilder (""); String line = null; // read the file content cyclically while (line = br. readLine ())! = Null) {sb. append (line);} // closes the resource br. close (); return sb. toString () ;}} catch (Exception e) {e. printStackTrace ();} return null;} private void write (String content) {try {// if the SD card is inserted into the mobile phone and the application has the permission to access SD if (Environment. getExternalStorageState (). equals (Environment. MEDIA_MOUNTED) {// obtain the directory File sdCardDir = Environment of the SD card. getExternalStorageDirectory (); File targetFile = new File (sdCardDir. getCanonicalPath () + FILE_NAME); // create the RandomAccessFile object RandomAccessFile raf = new RandomAccessFile (targetFile, "rw") with the specified file; // move the file record pointer to the last raf. seek (targetFile. length (); // output file content raf. write (content. getBytes (); // disable RandomAccessFileraf. close () ;}} catch (Exception e) {e. printStackTrace ();}}





Related Article

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.