读取和写入 Samsung Galaxy S SD 卡上的文本文件
我认为这一定不是一个很难实现的任务,我已经使用 HTC Desire 完成了它,但由于某种原因,我无法在 Android 应用程序中读取 Samsung 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卡时可以看到我写的内容。
有人知道这个问题的解决办法吗?谢谢
I am thinking that this must not be a very difficult task to achieve and i have managed it with the HTC Desire but for some reason I cannot read from the Samsung Galaxy S SD card in my android application.
I use :
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";
}
}
and this works fine! The file content gets saved and i am very happy. However, when I do the reverse using...
//
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();
}
I receive a file not found exception! I just wrote to it and can see what I wrote when I mount the SD Card.
Does someone know the solution to this? Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您使用了错误的构造函数,您应该使用
而不是
现在您的代码将正常工作。
You are using wrong constructor you should use
instead of
Now your code will work fine.
当您像这样初始化文件对象时会发生什么
首先会发生什么您以这种方式获取路径
并与文件名连接然后您以这种方式获得结果
现在检查是否找不到该文件所以要小心检查完整路径对于文件对象。
接下来你可以像这样使用
What happen when you initializing your file object like this
in this what happen first you getting the path like this way
and concatenate with the file name then you getting the result this way
now check that this file didn't found so beware check the complete path for file object.
next thing you can use like this way