public String ReadSettings(Context context)
{
FileInputStream fIn = null;
InputStreamReader isr = null;
char[] inputBuffer = new char[255];
String data = null;
try{
fIn = openFileInput("lee.txt");
isr = new InputStreamReader(fIn);
isr.read(inputBuffer);
data = new String(inputBuffer);
Toast.makeText(context, "Settings read",Toast.LENGTH_SHORT).show();
}
catch (Exception e) {
e.printStackTrace();
Toast.makeText(context, "Settings not read",Toast.LENGTH_SHORT).show();
}
finally {
try {
isr.close();
fIn.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return data;
}
public void WriteSettings(String data)
{
FileOutputStream fOut = null;
OutputStreamWriter osw = null;
try{
fOut = openFileOutput("lee.txt",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();
}
}
}
本文介绍了一个简单的Android应用程序中实现设置文件的读取和写入功能的方法。通过使用FileInputStream和FileOutputStream配合InputStreamReader及OutputStreamWriter,文章展示了如何从应用内部存储中读取和保存文本格式的设置数据。
144

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



