private String readFile(String filename) {
String reads = "";
try {
FileInputStream fis = this.openFileInput(filename);
byte[] b = new byte[1024];
ByteArrayOutputStream baos = new ByteArrayOutputStream();
while (fis.read(b) != -1) {
baos.write(b, 0, b.length);
}
baos.close();
fis.close();
reads = baos.toString();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return reads;
}
//str就是转换之后的string,即reads
private void saveFile(String str, String filename) {
FileOutputStream fos;
try {
fos = this.openFileOutput(filename, this.MODE_PRIVATE);
fos.write(str.getBytes());
fos.flush();
fos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
String reads = "";
try {
FileInputStream fis = this.openFileInput(filename);
byte[] b = new byte[1024];
ByteArrayOutputStream baos = new ByteArrayOutputStream();
while (fis.read(b) != -1) {
baos.write(b, 0, b.length);
}
baos.close();
fis.close();
reads = baos.toString();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return reads;
}
//str就是转换之后的string,即reads
private void saveFile(String str, String filename) {
FileOutputStream fos;
try {
fos = this.openFileOutput(filename, this.MODE_PRIVATE);
fos.write(str.getBytes());
fos.flush();
fos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
本文详细介绍了在Android应用中如何进行文件读取与存储操作,包括使用FileInputStream和FileOutputStream进行文件操作,以及如何通过openFileInput和openFileOutput方法实现文件读写功能。
661

被折叠的 条评论
为什么被折叠?



