【发布时间】:2010-04-12 16:09:28
【问题描述】:
抱歉标题不明确,但我正在执行以下操作以将一个简单的字符串写入文件:
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();
}
但是root.canWrite() 似乎每次都返回 false。我不是在模拟器上运行它,而是通过 USB 将我的 android Eris 插入我的计算机并通过 Eclipse 从我的手机上运行该应用程序。有没有办法授予我的应用权限,以免发生这种情况?
另外,这段代码似乎是创建文件default.txt,但如果它已经存在,它会忽略创建并打开它来写入还是我必须捕获FileAlreadyExists之类的东西(如果这样的异常存在)然后打开它并写入?
感谢大家的帮助。
【问题讨论】: