我正在尝试使用MacOS将字符串写入文件。这是我的代码:
private void writeToFile(String data) {
try {
File newTextFile = new File("/Users/myusername/Documents/bitmapencoded.txt");
FileWriter fw = new FileWriter(newTextFile);
fw.write(data);
fw.close();
} catch (IOException iox) {
//do stuff with exception
iox.printStackTrace();
}
}
我收到“找不到文件或目录”的例外情况。是因为Java可能没有写入该文件的权限吗?
如果它没有权限写入用户:我还能在哪里保存文件(我对使用OSX很新)?
答案 0 :(得分:0)
我收到“未找到文件或目录”
如异常所示,请尝试添加:
if (!newTextFile.exists()) {
newTextFile.createNewFile();
}
之前FileWriter fw = ...