我想用一个字符串保存一个文件,然后加载它。在仿真器上保存并加载文件
不知何故,这不工作。我认为存储的文件无法找到或没有存储(根据日志)。
如何解决这个问题?
这里是节省代码:
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");
尝试在日志语句中打印出sServPath,以确保您正在拖动需要的文本 - 您可能有无效的视图或其他内容。 – 2012-04-25 12:57:26
视图是好的,打印出正确的文本 – ctekk 2012-04-25 13:02:55
好吧我忘了fCheck文件的路径和fName之间的“/”。仍然它的文件不加载我上面添加的代码(NPE) – ctekk 2012-04-26 08:23:41