The following is a summary of the way to read and write files in Android, the need for friends can come to refer to the next
First, get the file from the Raw folder in resource and read the data (the resource file can only read and write)
Copy Code code 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");
//Bbi.txt encoding type to select the appropriate encoding, if not adjusted will be garbled
In.close ();
}catch (Exception e) {
E.printstacktrace ();
}
Mytextview.settext (RES)//To display the resulting content on the TextView
second, get the file from the asset and read the data (the resource file can only read and write)
Copy Code code as follows:
String fileName = "Yan.txt"; File name
String res= "";
try{
InputStream in = Getresources (). Getassets (). open (FileName);
//Testassetsyan.txt There is such a file here
int length = in.available ();
byte [] buffer = new Byte[length];
in.read (buffer);
res = encodingutils.getstring (buffer, "UTF-8");
}catch (Exception e) {
E.printstacktrace ();
}
third, from the SDcard to read the file, first of all to the file through the Android-sdk-windowstoolsadb.exe of the local computer file copy to sdcard up, Adb.exe push e:/y.txt/sdcard/, You cannot use the Adb.exe push e:y.txt sdcard the same: Copy the files on the emulator to your local computer: adb pull/data/data/com.tt/files/test.txt e:/
Copy Code code as follows:
String fileName = "/sdcard/y.txt";
//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);
Four, write the file, generally write in datadatacom.testfiles inside, open DDMS view File Explorer can see the structure of the emulator file storage directory.
Copy Code code 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 ();
}
}
Five, write, read data/data/directory (rather AP working directory) on the file, with Openfileoutput
Copy Code code as follows:
//write files under./data/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/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;
}
Six, write, read SDcard directory of documents, to use FileOutputStream, can not use Openfileoutput
Copy Code code as follows:
//written in the/mnt/sdcard/directory below the file
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 file below 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, FileOutputStream is any file can be