【发布时间】:2011-10-10 19:08:42
【问题描述】:
我认为这一定不是一个很难实现的任务,我已经使用 HTC Desire 进行了管理,但由于某种原因,我无法在我的 Android 应用程序中从三星 Galaxy S SD 卡中读取数据。
我用:
public String writeFile1(String text) {
File sdDir = Environment.getExternalStorageDirectory();
File myFile = new File(sdDir+"/TextFiles/patientDetails.txt");
try{
myFile.createNewFile();
FileOutputStream fOut = new FileOutputStream(myFile);
OutputStreamWriter myOutWriter =
new OutputStreamWriter(fOut);
myOutWriter.write(text);
myOutWriter.close();
fOut.close();
return "success";
}catch (IOException e){
e.printStackTrace();
return "fail";
}
}
这很好用!文件内容被保存,我很高兴。但是,当我使用...进行反向操作时......
//
File f = new File(Environment.getExternalStorageDirectory()+fileName);
FileInputStream fileIS = new FileInputStream(f);
BufferedReader buf = new BufferedReader(new InputStreamReader(fileIS));
String readString = new String();
//just reading each line and pass it on the debugger
String s = "";
while((readString = buf.readLine())!= null){
s+=readString;
}
return s;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e){
e.printStackTrace();
}
我收到文件未找到异常!我刚刚写信给它,当我安装 SD 卡时可以看到我写的内容。
有人知道这个问题的解决方案吗?谢谢
【问题讨论】:
-
只是为了确定,你的阅读方法可以
Log.d(f.toString())吗? -
文件 myFile = new File(sdDir+"/TextFiles/patientDetails.txt");与您用于读取文件的路径相同吗 File f = new File(Environment.getExternalStorageDirectory()+fileName); ?确保它。所以,我觉得 fileName="/TextFiles/patientDetails.txt" 对吧?