android文件操作——读写文件demo
2017-05-08 17:13
453 查看
写文件操作:
public void save(String inputText) {
FileOutputStream out = null;
BufferedWriter writer = null;
try {
out = openFileOutput("data", Context.MODE_PRIVATE);
writer = new BufferedWriter(new OutputStreamWriter(out));
writer.write(inputText);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (writer != null) {
writer.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
读文件操作:
public String load() {
FileInputStream in = null;
BufferedReader reader = null;
StringBuilder content = new StringBuilder();
try {
in = openFileInput("data");
reader = new BufferedReader(new InputStreamReader(in));
String line = "";
while ((line = reader.readLine()) != null) {
content.append(line);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return content.toString();
}
public void save(String inputText) {
FileOutputStream out = null;
BufferedWriter writer = null;
try {
out = openFileOutput("data", Context.MODE_PRIVATE);
writer = new BufferedWriter(new OutputStreamWriter(out));
writer.write(inputText);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (writer != null) {
writer.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
读文件操作:
public String load() {
FileInputStream in = null;
BufferedReader reader = null;
StringBuilder content = new StringBuilder();
try {
in = openFileInput("data");
reader = new BufferedReader(new InputStreamReader(in));
String line = "";
while ((line = reader.readLine()) != null) {
content.append(line);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return content.toString();
}
相关文章推荐
- Android SDCard操作(文件读写,容量计算)
- Android下文件操作模式(含SDCard的读写)
- 【Android】文件读写及文件下载操作
- 【Android】文件读写操作(含SDCard的读写)
- Android中SD卡的操作 - 文件读写及容量计算
- 简单的Android对文件进行读写操作
- Android SDCard操作(文件读写,容量计算)
- android文件读写以及不同应用之间的文件读写操作
- Android SDCard操作(文件读写,容量计算)
- Android学习笔记(11)————Android的私人文件夹的文件读写操作
- android文件读写&访问sdcard&文件操作模式
- Android学习笔记(九) Android文件读写操作
- Hello Android - Android SDCard操作(文件读写,容量计算)
- Android之再谈文件操作和SDcard读写
- Android SDCard操作(文件读写,容量计算)
- Android SDCard操作(文件读写,容量计算)
- Android SDCard操作(文件读写,容量计算)
- Android SDCard操作(文件读写,容量计算)
- 转:Android下文件操作模式(含SDCard的读写)
- 【Android】文件读写操作(含SDCard的读写)