Android开发笔记——储存文件到Data
麻烦死了
前言
教程里给出的实例,很长
我甚至不想仔细看
private void save(String a){
FileOutputStream out = null;
BufferedWriter writer = null;
try{
out = openFileOutput("data", Context.MODE_PRIVATE);
writer = new BufferedWriter(new OutputStreamWriter(out));
writer.write(a);
}catch (IOException e){
e.printStackTrace();
}finally {
try {
if(writer != null){
writer.close();
}
}catch(IOException e){
e.printStackTrace();
}
}
}
private 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();
}
于是
使用chatgpt为其添加了注释
感觉还是繁琐
private void save(String a){ //定义一个保存数据的函数,参数为数据内容
FileOutputStream out = null; //定义一个输出流
BufferedWriter writer = null; //定义一个缓冲写入器
try{
out = openFileOutput("data", Context.MODE_PRIVATE); //打开名为"data"的文件进行文件输出
writer = new BufferedWriter(new OutputStreamWriter(out)); //将输出流传递给缓冲写入器
writer.write(a); //写入字符串形式的数据内容
}catch (IOException e){ //捕获可能发生的I/O异常
e.printStackTrace(); //输出异常栈信息
}finally { //保证无论如何都会执行的最终操作
try {
if(writer != null){ //如果缓冲写入器不为空
writer.close(); //则将其关闭
}
}catch(IOException e){ //捕获可能发生的I/O异常
e.printStackTrace(); //输出异常栈信息
}
}
}
private String load(){ //定义一个读取数据的函数,返回值为字符串类型的数据内容
FileInputStream in = null; //定义一个输入流
BufferedReader reader = null; //定义一个缓冲读取器
StringBuilder content = new StringBuilder(); //定义一个字符串构建器,用于将读取的数据拼接成完整的字符串
try {
in = openFileInput("data"); //打开名为"data"的文件进行文件输入
reader = new BufferedReader(new InputStreamReader(in)); //将输入流传递给缓冲读取器
String line = ""; //定义一个字符串变量用于存储读取到的每一行数据
while ((line = reader.readLine()) != null){ //每次读取一行数据,如果不为空就继续
content.append(line); //将这行数据添加到字符串构建器中
}
}catch (IOException e){ //捕获可能发生的I/O异常
e.printStackTrace(); //输出异常栈信息
}finally{ //保证无论如何都会执行的最终操作
if(reader != null){ //如果缓冲读取器不为空
try{
reader.close(); //则将其关闭
}catch (IOException e){ //捕获可能发生的I/O异常
e.printStackTrace(); //输出异常栈信息
}
}
}
return content.toString(); //将字符串构建器中拼接好的数据内容以字符串形式返回
}
我觉得一个保存读取不该这么长
于是询问
chatgpt给出的化简方案
// 以上代码已经很简单和高效了,不过可以使用Java 7引入的Try-With-Resources方法,可以进一步简化代码并且不需要在finally块中手动关闭资源。以下是更新后的代码:
private void save(String a) {
try (BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(
openFileOutput("data", Context.MODE_PRIVATE)))) {
writer.write(a);
} catch (IOException e) {
e.printStackTrace();
}
}
private String load() {
StringBuilder content = new StringBuilder();
try (BufferedReader reader = new BufferedReader(new InputStreamReader(
openFileInput("data")))) {
String line;
while ((line = reader.readLine()) != null) {
content.append(line);
}
} catch (IOException e) {
e.printStackTrace();
}
return content.toString();
}
// 这段更新后的代码使用try-with-resources语句简化了底层的IO操作,并且自动关闭资源,而且不需要显式的声明FileInputStream和FileOutputStream对象,可以直接使用Context的openFileInput和openFileOutput方法。
尽管看起来简单了但是差点意思
看来异常处理的部分是不可省略的
不可能每次读取都手打这么多
最好有个模板
正文
基本是个可以复制的八股文了
其中try
部分是可改的有效代码
根据需要处理
缺点是一个数据要放一个文件
保存方法
private void save(String content ,String file){
FileOutputStream out = null;
BufferedWriter writer = null;
try{
out = openFileOutput(file, Context.MODE_PRIVATE); //打开名为"data"的文件进行文件输出
writer = new BufferedWriter(new OutputStreamWriter(out)); //将输出流传递给缓冲写入器
writer.write(content); //写入字符串形式的数据内容
// 捕获异常
}catch (IOException e){
e.printStackTrace();
}finally {
try {
if(writer != null){
writer.close();
}
}catch(IOException e){
e.printStackTrace();
}
}
}
输入的两个参数:要保存的数据,要保存的地方
读取方法
private String load(String file){
FileInputStream in = null;
BufferedReader reader = null;
StringBuilder content = new StringBuilder();
//定义一个字符串构建器,用于将读取的数据拼接成完整的字符串
try {
in = openFileInput(file);
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(); //数据返回
}
输入的一个参数:保存的地方
输出的一个参数:读到的数据