gpt4 book ai didi

java - 系统找不到用 FileWriter 指定的路径

转载 作者:搜寻专家 更新时间:2023-10-31 08:16:08 25 4
gpt4 key购买 nike

我有这个代码:

private static void saveMetricsToCSV(String fileName, double[] metrics) {
try {
FileWriter fWriter = new FileWriter(
System.getProperty("user.dir") + "\\output\\" +
fileTimestamp + "_" + fileDBSize + "-" + fileName + ".csv"
);

BufferedWriter csvFile = new BufferedWriter(fWriter);

for(int i = 0; i < 4; i++) {
for(int j = 0; j < 5; j++) {
csvFile.write(String.format("%,10f;", metrics[i+j]));
}

csvFile.write(System.getProperty("line.separator"));
}

csvFile.close();
} catch(IOException e) {
System.out.println(e.getMessage());
}
}

但是我得到这个错误:

C:\Users\Nazgulled\Documents\Workspace\Só Amigos\output\1274715228419_5000-List-ImportDatabase.csv (The system cannot find the path specified)

知道为什么吗?

如果重要的话,我会在 Windows 7 上使用 NetBeans...

最佳答案

一般来说,只有父目录存在,Java才会创建一个不存在的文件。您应该检查/创建目录树:

  String filenameFullNoPath = fileTimestamp + "_"  + fileDBSize + "-" 
+ fileName + ".csv";
File myFile = new File(System.getProperty("user.dir") + File.separator
+ "output" + File.separator + filenameFullNoPath);
File parentDir = myFile.getParentFile();
if(! parentDir.exists())
parentDir.mkdirs(); // create parent dir and ancestors if necessary
// FileWriter does not allow to specify charset, better use this:
Writer w = new OutputStreamWriter(new FileOutputStream(myFile),charset);

关于java - 系统找不到用 FileWriter 指定的路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2898179/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com