我想保存一个文件,其中包含一个来自edittext的字符串,然后加载它。
不知何故,这不起作用。我认为存储的文件找不到或没有存储(根据日志)。
如何解决这个问题?
这是保存代码:
Log.i("Watcher","Saving...");
ProgressDialog dSave = ProgressDialog.show(this, "Saving", "SAving. Please wait...",false);
String fName = "WatchConf";
EditText servPath = (EditText)findViewById(R.id.ServerPath);
String sServPath = servPath.getText().toString();
try {
FileOutputStream fos = openFileOutput(fName, Context.MODE_PRIVATE);
fos.write(sServPath.getBytes());
fos.flush();
fos.close();
Log.d("Watcher","Saved");
File fCheck = new File(getFilesDir()+fName);
if(fCheck.exists()){
Log.i("Watcher","Saved successfully");
}
加载代码:
Log.i("Watcher","Loading...");
String fName = "WatchConf";
EditText servPath = (EditText)findViewById(R.id.ServerPath);
try {
InputStreamReader isr = new InputStreamReader(openFileInput(getFilesDir()+"/"+fName));
char[] cRead = new char[100];
isr.read(cRead);
String sRead = new String(cRead);
servPath.setText(sRead);
isr.close();
Log.i("Watcher","Loaded");