Android file read/write method summary

Source: Internet
Author: User

The following is a summary of how to read and write files in Android. For more information, see

1. Get the file from the raw folder in the resource and read the data (the resource file can only be read but cannot be written)

The Code is as follows:
String res = "";
Try {
InputStream in = getResources (). openRawResource (R. raw. bbi );
// In testresrawbbi.txt,
Int length = in. available ();
Byte [] buffer = new byte [length];
In. read (buffer );
// Res = EncodingUtils. getString (buffer, "UTF-8 ");
// Res = EncodingUtils. getString (buffer, "UNICODE ");
Res = EncodingUtils. getString (buffer, "BIG5 ");
// Select an appropriate encoding type based on bbi.txt. If this parameter is not adjusted, garbled characters are returned.
In. close ();
} Catch (Exception e ){
E. printStackTrace ();
}


MyTextView. setText (res); // display the obtained content on TextView.

2. Get files from asset and read data (resource files can only be read but cannot be written)

The Code is as follows:
String fileName = "yan.txt"; // file name
String res = "";
Try {
InputStream in = getResources (). getAssets (). open (fileName );
// Testassetsyan.txt this file exists
Int length = in. available ();
Byte [] buffer = new byte [length];
In. read (buffer );
Res = EncodingUtils. getString (buffer, "UTF-8 ");
} Catch (Exception e ){
E. printStackTrace ();
}


3. copy the files on the local computer to sdcard, and adb.exe pushes e:/Y.txt/sdcard/. Similarly, adb.exe pushes e: Y.txt sdcard: Copies the files on the simulator to the local computer:Adb pull./data/com. tt/files/Test.txt e :/

The Code is as follows:
String fileName = "/sdcard/Y.txt ";
// You can also use String fileName = "mnt/sdcard/Y.txt ";
String res = "";
Try {
FileInputStream fin = new FileInputStream (fileName );
// FileInputStream fin = openFileInput (fileName );
// This will not work. You must use FileInputStream.
Int length = fin. available ();
Byte [] buffer = new byte [length];
Fin. read (buffer );
Res = EncodingUtils. getString (buffer, "UTF-8 ");
Fin. close ();
} Catch (Exception e ){
E. printStackTrace ();
}
MyTextView. setText (res );


4. Write files, which are generally written in datadatacom. testfiles. Open DDMS to View file explorer, and you can see the structure of the directory where the file is stored in the simulator.

The Code is as follows:
String fileName = "TEST.txt ";
String message = "FFFFFFF11111FFFFF ";
WriteFileData (fileName, message );
Public voidwriteFileData (String fileName, String message ){
Try {
FileOutputStream fout = openFileOutput (fileName, MODE_PRIVATE );
Byte [] bytes = message. getBytes ();
Fout. write (bytes );
Fout. close ();
}
Catch (Exception e ){
E. printStackTrace ();
}
}


5. write and read files in the data/directory (equivalent to the AP working directory). Use openFileOutput

The Code is as follows:
// Write the file under./data/com. tt/files/
Public voidwriteFileData (String fileName, String message ){
Try {
FileOutputStream fout = openFileOutput (fileName, MODE_PRIVATE );
Byte [] bytes = message. getBytes ();
Fout. write (bytes );
Fout. close ();
}
Catch (Exception e ){
E. printStackTrace ();
}
}
//-------------------------------------------------------
// Read the file under./data/com. tt/files/
Public String readFileData (String fileName ){
String res = "";
Try {
FileInputStream fin = openFileInput (fileName );
Int length = fin. available ();
Byte [] buffer = new byte [length];
Fin. read (buffer );
Res = EncodingUtils. getString (buffer, "UTF-8 ");
Fin. close ();
}
Catch (Exception e ){
E. printStackTrace ();
}
Return res;
}


6. write and read the files in the sdcard directory. Use FileOutputStream instead of openFileOutput.

The Code is as follows:
// File written in the/mnt/sdcard/directory
Public voidwriteFileSdcard (String fileName, String message ){
Try {
// FileOutputStream fout = openFileOutput (fileName, MODE_PRIVATE );
FileOutputStream fout = newFileOutputStream (fileName );
Byte [] bytes = message. getBytes ();
Fout. write (bytes );
Fout. close ();
}
Catch (Exception e ){
E. printStackTrace ();
}
}
// Read the files in the/mnt/sdcard/directory
Public String readFileSdcard (String fileName ){
String res = "";
Try {
FileInputStream fin = new FileInputStream (fileName );
Int length = fin. available ();
Byte [] buffer = new byte [length];
Fin. read (buffer );
Res = EncodingUtils. getString (buffer, "UTF-8 ");
Fin. close ();
}
Catch (Exception e ){
E. printStackTrace ();
}
Return res;
}


Note: openFileOutput is compiled in raw, and FileOutputStream is applicable to any file.


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.