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 \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");
According to Bbi.txt encoding type to choose the appropriate encoding, if not adjusted will 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);
\test\assets\yan.txt Here's a file that exists
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-windows\tools\adb.exe of the local computer files 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";
You can also use string fileName = "Mnt/sdcard/y.txt";
String res= "";
try{
FileInputStream fin = new FileInputStream (fileName);
FileInputStream fin = openfileinput (fileName);
You can't use this, you have to 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 \data\data\com.test\files\ 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 the file 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:
A 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 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