很抱歉这个含糊不清的标题,但我正在做以下操作,将一个简单的字符串写入文件:
try {
File root = Environment.getExternalStorageDirectory();
if (root.canWrite()){
System.out.println("Can write.");
File def_file = new File(root, "default.txt");
FileWriter fw = new FileWriter(def_file);
BufferedWriter out = new BufferedWriter(fw);
String defbuf = "default";
out.write(defbuf);
out.flush();
out.close();
}
else
System.out.println("Can't write.");
}catch (IOException e) {
e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)
但root.canWrite()似乎每次都会回归虚假.我没有在模拟器上运行它,我将我的android Eris通过USB插入我的计算机并通过Eclipse从我的手机上运行应用程序.有没有办法给我的应用程序权限,所以这不会发生?
此外,这段代码似乎是创建文件,default.txt但如果它已经存在,它会忽略创建并只是打开它来编写或者我必须捕获类似的东西FileAlreadyExists(如果存在这样的异常)然后只是打开它并写入?
谢谢你的帮助.
Com*_*are 17
有没有办法给我的应用程序权限,所以这不会发生?
你需要这个WRITE_EXTERNAL_STORAGE许可.
您还需要在开发计算机上不安装SD卡 - 计算机或手机可以访问SD卡,但不能同时访问两者.
| 归档时间: |
|
| 查看次数: |
8322 次 |
| 最近记录: |