gpt4 book ai didi

android - 向android中的文件写入数据

转载 作者:行者123 更新时间:2023-11-29 22:12:19 25 4
gpt4 key购买 nike

此代码覆盖文件....

我希望我以前的数据保持不变..

osw..append (data);  

但没有得到结果

public void WriteSettings(Context context, String data){ 
FileOutputStream fOut = null;
OutputStreamWriter osw = null;

try{
fOut = context.openFileOutput("abc.txt",Context.MODE_PRIVATE);
osw = new OutputStreamWriter(fOut);
osw.write(data);
osw.flush();
Toast.makeText(context, "Settings saved",Toast.LENGTH_SHORT).show();
}
catch (Exception e) {
e.printStackTrace();
Toast.makeText(context, "Settings not saved",Toast.LENGTH_SHORT).show();
}
finally {
try {
osw.close();
fOut.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

最佳答案

 public void WriteSettings(Context context, String data){ 
FileOutputStream fOut = null;
OutputStreamWriter osw = null;

try{
fOut = context.openFileOutput("abc.txt",Context.MODE_APPEND);
osw = new OutputStreamWriter(fOut);
osw.write(data);
osw.flush();
Toast.makeText(context, "Settings saved",Toast.LENGTH_SHORT).show();
}
catch (Exception e) {
e.printStackTrace();
Toast.makeText(context, "Settings not saved",Toast.LENGTH_SHORT).show();
}
finally {
try {
osw.close();
fOut.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

关于android - 向android中的文件写入数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9329438/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com