简体   繁体   English

使用java.nio.file.Files.createTempDirectory时出现NoSuchFileException

[英]NoSuchFileException when using java.nio.file.Files.createTempDirectory

I have an issue when trying to create a temporary directory with java.nio.file.Files.createTempDirectory. 尝试使用java.nio.file.Files.createTempDirectory创建临时目录时遇到问题。 I keep getting NoSuchFileException when trying to create the directory. 尝试创建目录时,我不断收到NoSuchFileException。

Here is my code: 这是我的代码:

import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class TempFileTesting {
   private static final String ROOT = "/resources/";

   public static void main(String[] args) throws Exception{
      Path root = Paths.get(ROOT);
      Path tempDir = Files.createTempDirectory(root, "dir");
      Path tempFile = Files.createTempFile(tempDir, "t1", "t2");
   }
}

When I do this I get a NoSuchFileException on the line calling "createTempDirectory" despite the root Path clearly being created successfully. 当我这样做时,尽管路径显然已成功创建,但我在调用“ createTempDirectory”的行上得到了NoSuchFileException。 The resources directory does exist. 资源目录确实存在。

The StackTrace looks like this: StackTrace看起来像这样:

java.nio.file.NoSuchFileException: \resources\dir170003182480656885
at sun.nio.fs.WindowsException.translateToIOException(WindowsException.java:79)
at sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:97)
at sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:102)
at sun.nio.fs.WindowsFileSystemProvider.createDirectory(WindowsFileSystemProvider.java:504)
at java.nio.file.Files.createDirectory(Files.java:674)
at java.nio.file.TempFileHelper.create(TempFileHelper.java:136)
at java.nio.file.TempFileHelper.createTempDirectory(TempFileHelper.java:173)
at java.nio.file.Files.createTempDirectory(Files.java:950)
at filetestingstuff.testers.TempFileTesting.main(TempFileTesting.java:15)

Full Path: "C:\\Users\\Admin\\Desktop\\eclipse-oxygen\\workspace\\FileStuff\\resources" 完整路径:“ C:\\ Users \\ Admin \\ Desktop \\ eclipse-oxygen \\ workspace \\ FileStuff \\ resources”

Does anyone have any idea why exactly this causes this Exception to occur? 有谁知道为什么这会导致该异常的发生? I am grateful for any advice, no matter how small. 无论多小建议,我都感激不尽。

You specified "/resources/" as path to the directory in which to create the temporary directory. 您将"/resources/"指定为要在其中创建临时目录的目录的路径。
First, it is not a valid format for windows. 首先,它不是Windows的有效格式。 As I test it creates the temp directory at the root of the drive where windows is installed. 当我测试时,它将在安装Windows的驱动器的根目录下创建temp目录。

Besides what you want is a relative path : "resources" to the working folder of the JVM that is C:\\Users\\Admin\\Desktop\\eclipse-oxygen\\workspace\\FileStuff . 除了所需的路径外,还有一个相对路径: "resources"到JVM的工作文件夹,即C:\\Users\\Admin\\Desktop\\eclipse-oxygen\\workspace\\FileStuff Note that the trailing / is not required any longer. 请注意,结尾的/不再需要。
So that should solve your issue : 这样就可以解决您的问题:

private static final String ROOT = "resources";     

At last, you should avoid using folders as C:\\Users to contain your Java source code. 最后,您应该避免将文件夹用作C:\\Users来包含Java源代码。 You may have right issues too. 您可能也有正确的问题。
They should be located in a not specific Windows directory. 它们应位于非特定的Windows目录中。

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

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