Java FileNotFoundException(系统找不到指定的路径)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4429527/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 17:21:42  来源:igfitidea点击:

FileNotFoundException (The system cannot find the path specified)

javafile-io

提问by Michael

I get this exception:

我得到这个例外:

java.io.FileNotFoundException: C:\...\filename.xml (The system cannot find the path specified)

using this code:

使用此代码:

FileWriter fileWriter = new FileWriter(new File(path + date + time "filename.xml"));
BufferedWriter writer = new BufferedWriter(fileWriter);
writer.write("data");

Path exists but directories for 'date' and 'time' need to be created. Application has full permissions on the directory.

路径存在,但需要创建“日期”和“时间”的目录。应用程序对该目录具有完全权限。

Any ideas?

有任何想法吗?

采纳答案by BalusC

The problem is because I'm creating a subdirectory in which to write the files. So I currently have C:\example\and want to write my files in C:\example\<date>\<time>\<files>

问题是因为我正在创建一个子目录来写入文件。所以我目前拥有C:\example\并想将我的文件写入C:\example\<date>\<time>\<files>

You need to call File#mkdirs()before writing.

你需要File#mkdirs()在写之前打电话。

File file = new File("C:/example/newdir/newdir/filename.ext");
file.mkdirs();
// ...

回答by Frankie

Do assume that the computer is right and you are wrong.

一定要假设计算机是对的,而你是错的。

And, in that scenario, the directory to which you want to write does not exit (or does not have permissions to do so).

而且,在这种情况下,您要写入的目录不会退出(或没有权限这样做)。

  1. check the current working dir System.getProperty("user.dir")
  2. debug from there
  1. 检查当前工作目录 System.getProperty("user.dir")
  2. 从那里调试

回答by jzd

Code works for me. (Need to add a writer.close()for text to show up in the file.)

代码对我有用。(需要添加一个writer.close()for 文本以显示在文件中。)

回答by user3440120

You also need to convert newly created file and folder paths to string.

您还需要将新创建的文件和文件夹路径转换为字符串。

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);
            }
        }

    }

回答by Mohammed Jaffer Ali

What worked for me: The folder where my project was present had a space in the name. I replacedthe space with a hyphen(-). Now, the relative path of the file does not have a space (%20). This change worked for me. Hope it helps someone.

什么对我有用:我的项目所在的文件夹名称中有一个空格。我用连字符(-)替换了空格。现在,文件的相对路径没有空格 (%20)。这种变化对我有用。希望它可以帮助某人。