【发布时间】:2019-03-20 06:31:12
【问题描述】:
我正在使用PrintWriter 使用以下代码从磁盘写入一个字符串文件:
public static void storeString(String string, String path) {
PrintWriter out = null;
try {
out = new PrintWriter(path, "UTF-8");
out.println(string);
out.flush();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (out != null) {
out.close();
}
}
}
问题是如果路径包含目录,那么 PrintWriter 会抛出异常。如何告诉 PrintWriter 创建其路径的缺失文件夹?
【问题讨论】:
标签: java eclipse outputstream fileoutputstream printwriter