简体   繁体   English

带有StandardOpenOption.CREATE的Files.newInputStream中的NoSuchFileException

[英]NoSuchFileException in Files.newInputStream with StandardOpenOption.CREATE

I am trying to open a file for reading or create the file if it was not there. 我试图打开一个文件进行阅读或创建该文件,如果它不存在。 I use this code: 我用这个代码:

String location = "/test1/test2/test3/";
new File(location).mkdirs();
location += "fileName.properties";
Path confDir = Paths.get(location);
InputStream in = Files.newInputStream(confDir, StandardOpenOption.CREATE);
in.close();

And I get java.nio.file.NoSuchFileException 我得到java.nio.file.NoSuchFileException

Considering that I am using StandardOpenOption.CREATE option, the file should be created if it is not there. 考虑到我使用的是StandardOpenOption.CREATE选项,如果文件不存在,则应该创建该文件。

Any idea why I am getting this exception? 知道为什么我得到这个例外吗?

It seems that you want one of two quite separate things to happen: 看起来你想要发生两件完全不同的事情中的一件:

  1. If the file exists, read it; 如果文件存在,请阅读; or 要么
  2. If the file does not exist, create it. 如果该文件不存在,请创建它。

The two things are mutually exclusive but you seem to have confusingly merged them. 这两件事是相互排斥的,但你似乎混淆了它们。 If the file did not exist and you've just created it, there's no point in reading it. 如果文件不存在而您刚刚创建它,那么阅读它就没有意义了。 So keep the two things separate: 所以保持两件事分开:

    Path confDir = Paths.get("/test1/test2/test3");
    Files.createDirectories(confDir);
    Path confFile = confDir.resolve("filename.properties");

    if (Files.exists(confFile))
        try (InputStream in = Files.newInputStream(confFile)) {
            // Use the InputStream...
        }
    else
        Files.createFile(confFile);

Notice also that it's better to use "try-with-resources" instead of manually closing the InputStream. 另请注意,最好使用“try-with-resources”而不是手动关闭InputStream。

Accordingly to the JavaDocs you should have used newOutputStream() method instead, and then you will create the file: 根据JavaDocs,你应该使用newOutputStream()方法,然后你将创建文件:

OutputStream out = Files.newOutputStream(confDir, StandardOpenOption.CREATE);
out.close();

JavaDocs: JavaDoc中:

// Opens a file, returning an input stream to read from the file.
static InputStream newInputStream(Path path, OpenOption... options)

// Opens or creates a file, returning an output stream that
// may be used to write bytes to the file.
static OutputStream newOutputStream(Path path, OpenOption... options)

The explanation is that OpenOption constants usage relies on wether you are going to use it within a write(output) stream or a read(input) stream. 解释是OpenOption常量的使用依赖于您将在写入(输出)流或读取(输入)流中使用它。 This explains why OpenOption.CREATE only works deliberatery with the OutputStream but not with InputStream . 这解释了为什么OpenOption.CREATE只能与OutputStream讨论,而不能与InputStream协商。

NOTE: I agree with @EJP, you should take a look to Oracle's tutorials to create files properly. 注意:我同意@EJP,您应该查看Oracle的教程以正确创建文件。

I think you intended to create an OutputStream (for writing to) instead of an InputStream (which is for reading) 我认为你打算创建一个OutputStream (用于写入)而不是InputStream (用于读取)

Another handy way of creating an empty file is using apache-commons FileUtils like this 另一种创建空文件的方法是使用像这样的apache-commons FileUtils

FileUtils.touch(new File("/test1/test2/test3/fileName.properties"));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM