java.nio.file.NoSuchFileException:为什么不创建文件[关闭]

问题描述 投票:4回答:1

我正在使用java.nio.file包,并尝试使用以下代码创建文件。

private static void printReport(String filename, String str)throws Exception{

        ErrorCheck ec = new ErrorCheck();
        String fileName = "/var/Emails_log/"+filename;      
        Path filePath = Paths.get(fileName);
        File file = new File(fileName);             
        final BufferedWriter out = Files.newBufferedWriter(filePath, StandardCharsets.UTF_8, StandardOpenOption.APPEND);            

        try{                
            final Path tmp = filePath.getParent();
            if (tmp != null){ // null will be returned if the path has no parent
                    Files.createDirectories(tmp);   
            } else {
                out.write(str);
                out.write('\n');
            }               
        } catch(Exception e) {
            ec.errorMsg("ERROR: GSW.SendEmail.Exception =>",e);
        } finally {
            if (out != null) {
                out.flush();
                out.close();
            }
        }           
    }

这将引发以下异常:

java.nio.file.NoSuchFileException: /var/Emails_log/GSWvalidSentAddresses.txt
at sun.nio.fs.UnixException.translateToIOException(UnixException.java:86)
at sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:102)
at sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:107)
at sun.nio.fs.UnixFileSystemProvider.newByteChannel(UnixFileSystemProvider.java:214)
at java.nio.file.spi.FileSystemProvider.newOutputStream(FileSystemProvider.java:430)
at java.nio.file.Files.newOutputStream(Files.java:170)
at java.nio.file.Files.newBufferedWriter(Files.java:2720)
at SendEmail.printReport(SendEmail.java:114) SendEmail.send(SendEmail.java:87)

我的问题是为什么不创建文件?

请告知

感谢您的期待

java file-io nio
1个回答
9
投票

更新后的答案

现在您已经显示了完整的代码,有两个主要问题:

  1. 您正在尝试打开文件,然后确保导致该文件的目录存在,以及

  2. 您正在使用StandardOpenOption.APPEND,但不会创建文件;它将附加到existing文件。

...以及围绕最佳实践的大量问题,与实际代码的行数有关。

查看评论:

private static void printReport(String filename, String str) throws Exception /* No, it doesn't. The only calls you had outside your catch-all `try` don't throw exceptions. */ {
    ErrorCheck ec           = new ErrorCheck();            // Recommend not creating this until/unless you need it
    String fileName         = "/var/Emails_log/"+filename; // VERY poor practice having two locals that only differ by the capitalization of one character in the middle (`filename` and `fileName`)
    Path filePath           = Paths.get(fileName);
    //  File file               = new File(fileName);      <== Removed, since you never use it for anything

    try {
        // Make sure the directories exist
        Files.createDirectories(filePath.getParent());  // No need for your null check, so I removed it; based on `fileName`, it will always have a parent

        // Open the file, creating it if it doesn't exist
        try (
            final BufferedWriter out = Files.newBufferedWriter(
                                            filePath,
                                            StandardCharsets.UTF_8,
                                            StandardOpenOption.CREATE,
                                            StandardOpenOption.APPEND)
        ) {
            // Write to out here
        }
    } catch (Exception e) {
        // Log-and-continue isn't generally best practice; and if you're going to do it
        ec.errorMsg("ERROR: GSW.SendEmail.Exception =>",e); // <== Um...send mail failed? This isn't sending mail, it's putting something in a file.
    }
    // Using the try-with-resources, we don't have to worry about the flush and close calls
}

但是这是我建议您写的方式:

private static void printReport(String filename, String str) throws IOException {
    Path filePath           = Paths.get("/var/Emails_log/" + filename);

    // Make sure the directories exist
    Files.createDirectories(filePath.getParent());

    // Open the file, creating it if it doesn't exist
    try (
        final BufferedWriter out = Files.newBufferedWriter(
                                        filePath,
                                        StandardCharsets.UTF_8,
                                        StandardOpenOption.CREATE,
                                        StandardOpenOption.APPEND)
    ) {
        // Write to out here, perhaps outputting `str`?
    }
}

...并在调用层处理异常。再次注意,因为我们使用的是try-with-resources,所以close是自动的(有异常时和无异常时)。

或者,如果您确实要执行日志记录并继续:

private static void printReport(String filename, String str) {
    try {
        Path filePath           = Paths.get("/var/Emails_log/" + filename);

        // Make sure the directories exist
        Files.createDirectories(filePath.getParent());

        // Open the file, creating it if it doesn't exist
        try (
            final BufferedWriter out = Files.newBufferedWriter(
                                            filePath,
                                            StandardCharsets.UTF_8,
                                            StandardOpenOption.CREATE,
                                            StandardOpenOption.APPEND)
        ) {
            // Write to out here, perhaps outputting `str`?
        }
    }
    catch (Exception e) {
        new ErrorCheck().errorMsg("ERROR: GSW.SendEmail.Exception =>", e); // <== But surely this message is suspect? I don't see anything sending email here.
    }
}

原始答案

您尚未显示实际失败的代码,这是对newBufferedWriterthis 1或this 1)的调用。 newBufferedWriter占用OpenOption,可从StandardOpenOption获得其标准集。确保您指定了StandardOpenOptionStandardOpenOption.CREATE

如果您使用这些标志之一,而您的问题中的代码是[StandardOpenOption.CREATE调用之前[[之前

,除非有其他问题(权限),它应该可以工作:StandardOpenOption.CREATE_NEW
© www.soinside.com 2019 - 2024. All rights reserved.