Java FileNotFoundException(系统找不到指定的路径)
我得到一个例外:Java FileNotFoundException(系统找不到指定的路径),java,file-io,Java,File Io,我得到一个例外: java.io.FileNotFoundException: C:\...\filename.xml (The system cannot find the path specified) 使用此代码: FileWriter fileWriter = new FileWriter(new File(path + date + time "filename.xml")); BufferedWriter writer = new BufferedWriter(fileWriter
java.io.FileNotFoundException: C:\...\filename.xml (The system cannot find the path specified)
使用此代码:
FileWriter fileWriter = new FileWriter(new File(path + date + time "filename.xml"));
BufferedWriter writer = new BufferedWriter(fileWriter);
writer.write("data");
路径存在,但需要创建“日期”和“时间”的目录。应用程序对目录具有完全权限
有什么想法吗?一定要假设计算机是对的,而你是错的。 而且,在这种情况下,您要写入的目录不退出(或者没有权限退出)
System.getProperty(“user.dir”)
writer.close()
,以便文本显示在文件中。)
问题是因为我正在创建一个子目录来写入文件。所以我现在有C:\example\
,想用C:\example\\\
你需要在写信前打电话
File file = new File("C:/example/newdir/newdir/filename.ext");
file.mkdirs();
// ...
您还需要将新创建的文件和文件夹路径转换为字符串
File folder = new File("src\\main\\json\\", idNumber);
folder.mkdir();
if (!folder.exists()) {
try {
folder.createNewFile();
} catch (IOException ex) {
Logger.getLogger(JsonGeneration.class.getName()).log(Level.SEVERE, null, ex);
}
}
...
...
FileOutputStream output = null;
File file;
String content = data.toString();
try {
String folder_location = folder.toString() + "\\";
String filename = "CurrentInfo";
file = new File(folder_location + filename.toString() + ".json");
output = new FileOutputStream(file);
if (!file.exists()) {
file.createNewFile();
}
byte[] content_in_bytes = content.getBytes();
output.write(content_in_bytes);
output.flush();
output.close();
} catch (IOException ex) {
Logger.getLogger(JsonGeneration.class.getName()).log(Level.SEVERE, null, ex);
} finally {
try {
if (output != null) {
output.close();
}
} catch (IOException e) {
Logger.getLogger(JsonGeneration.class.getName()).log(Level.SEVERE, null, e);
}
}
}
对我有用的是:我的项目所在的文件夹名称中有一个空格。我用连字符(-)替换空格。现在,文件的相对路径没有空间(%20)。这一变化对我起了作用。希望它能帮助别人 是的,我没想到会这样。问题是因为我正在创建一个子目录来写入文件。所以我现在有C:\example\并且想用C:\example\\\来写我的文件。是的,当写入一个存在的目录时,工作正常。刚发现问题是因为我写的子目录不存在。我目前有C:\example\并且想用C:\example编写我的文件\\\