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)
String res = "";
Try {
InputStream in = getResources (). openRawResource (R. raw. bbi );
// In \ Test \ res \ raw \ bbi.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)
String fileName = "yan.txt"; // file name
String res = "";
Try {
InputStream in = getResources (). getAssets (). open (fileName );
// \ Test \ assets \ yan.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. to read a file from sdcard, first copy the file on your computer to sdcardthrough \ android-sdk-windows \ tools \ adb.exe. adb.exe pushes e:/Y.txt/sdcard/, which is not applicable to adb.exe push e: \ Y.txt \ sdcard \ similarly: copy the file on the simulator to a local computer and use adb pull. /data/com. tt/files/Test.txt e :/
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 \ data \ com. test \ files \. Open DDMS to view the structure of file explorer.
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
// 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.
// 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;
}
Author: yilip