我正在尝试将文件写入 Java 中的特定目录。我在 Stack Overflow 论坛上查看了许多已回答的与此相关的问题。然而,我尝试的每种方法都会出现以下错误:
java.io.IOException: No such file or directory
at java.io.UnixFileSystem.createFileExclusively(Native Method)
at java.io.File.createNewFile(File.java:1012)
at mainFiles.fileManagers.NewCSV.nJSON(NewCSV.java:40)
at mainFiles.appWindows.UserDashboard$1.run(UserDashboard.java:30)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:311)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:756)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.awt.EventQueue$3.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:726)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
代码:
public static void nJSON(String username) {
File file = new File("src/jsonFiles/" + username + ".json");
try {
if (file.exists() && file.isDirectory()) {
System.out.println(username + ".json exist.");
} else {
json.put("Username", username);
json.put("Password", mainFiles.windowsSrc.UserInfo.password);
file.createNewFile();
FileWriter writer = new FileWriter(file);
FileOutputStream os = new FileOutputStream(file);
writer.write(json.toJSONString());
writer.flush();
os.close();
writer.close();
System.out.println(username + ".json created.");
}
} catch (IOException e) {
e.printStackTrace();
}
}
关于如何将文件写入某个目录有什么建议吗?
最佳答案
关于您的代码的一些注释:
在文件之前创建父目录。您无法在不存在的目录中创建文件。
file.getParentFile().mkdirs();
删除 FileOutputStream 代码。您对同一个文件同时使用 FileWriter 和 FileOutputStream。这是一个错误,因为一个可能会覆盖另一个,导致文件写入后立即被清除。
createNewFile() 不是必需的。只要目录存在,FileWriter 或 FileOutputStream 就可以创建自己的文件(请参见#1)。
在finally block 中关闭写入器,这样即使在写入过程中发生异常,也可以保证底层流被关闭。
我不确定 isDirectory 检查的用途。如果存在,那么它肯定是一个文件而不是目录。无论哪种方式,file.exists() && file.isDirectory()
都是不必要的,因为 isDirectory 已经检查是否存在。
更新的代码:
public static void nJSON(String username ) {
File file = new File("src/jsonFiles/" + username + ".json");
if (file.isDirectory()) {
System.err.println(username + ".json exist and is a directory.");
return;
}
if(!file.getParentFile().exists() && !file.getParentFile().mkdirs()) {
System.err.println("Failed to create directory " + file.getParent());
return;
}
FileWriter writer = null;
try{
json.put("Username", username);
json.put("Password", mainFiles.windowsSrc.UserInfo.password);
writer = new FileWriter(file);
writer.write(json.toJSONString());
writer.close();
System.out.println(username + ".json created.");
} catch(IOException e){
e.printStackTrace();
} finally {
if(writer != null) {
try {
writer.close();
} catch(IOException ignore) { }
}
}
}
关于java - 如何将文件写入目录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44955669/