Android read write file method summary
- 2020-05-17 06:28:11
- OfStack
1. Get files from the raw folder in resource and read the data (resource files can only be read but not written)
myTextView. setText (res); // displays the resulting content on TextView
2. Get files from asset and read data (resource files can only be read but not written)
3. To read the file from sdcard, first transfer the file from the local computer to sdcard via android-sdk-windows \tools\ adb. exe adb.push e:/ Y.txt /sdcard/, adb.exe push e:\ Y.txt \sdcard\ same: copy files from the emulator to the local computer: adb pull. / data data/com. tt/files/Test txt e: /
4. Write the file, 1 in \data\data\ com. test\files\, open DDMS to see file explorer, you can see the file directory structure of the simulator
5. Write and read files in data/data/ directory (equivalent to AP working directory) using openFileOutput
6. Write and read files in the sdcard directory. Use FileOutputStream, not openFileOutput
Note: openFileOutput is compiled in raw. FileOutputStream is any file
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");
// In accordance with the bbi.txt The encoding type to select the appropriate encoding, if not adjusted will be garbled
in.close();
}catch(Exception e){
e.printStackTrace();
}
myTextView. setText (res); // displays the resulting content on TextView
2. Get files from asset and read data (resource files can only be read but not written)
String fileName = "yan.txt"; // The file name
String res="";
try{
InputStream in = getResources().getAssets().open(fileName);
// \Test\assets\yan.txt There are files like this
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 the file from sdcard, first transfer the file from the local computer to sdcard via android-sdk-windows \tools\ adb. exe adb.push e:/ Y.txt /sdcard/, adb.exe push e:\ Y.txt \sdcard\ same: copy files from the emulator to the local computer: adb pull. / data 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);
// I can't use that. I have to 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 the file, 1 in \data\data\ com. test\files\, open DDMS to see file explorer, you can see the file directory structure of the simulator
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 data/data/ directory (equivalent to AP working directory) using openFileOutput
// Write the file in ./data/data/com.tt/files/ The following
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 in ./data/data/com.tt/files/ The following
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 files in the sdcard directory. Use FileOutputStream, not openFileOutput
// Written in the book /mnt/sdcard/ The files under the 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 in /mnt/sdcard/ The files under the 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