简体   繁体   English

无法制作文件 java.io.IOException: 没有这样的文件或目录

[英]Cannot make file java.io.IOException: No such file or directory

I am trying to create a file on the filesystem, but I keep getting this exception:我正在尝试在文件系统上创建一个文件,但我不断收到此异常:

java.io.IOException: No such file or directory

I have an existing directory, and I am trying to write a file to that directory.我有一个现有目录,我正在尝试将文件写入该目录。

// I have also tried this below, but get same error
// new File(System.getProperty("user.home") + "/.foo/bar/" + fileName);

File f = new File(System.getProperty("user.home") + "/.foo/bar/", fileName);

if (f.exists() && !f.canWrite())
        throw new IOException("Kan ikke skrive til filsystemet " + f.getAbsolutePath());

if (!f.isFile()) {
    f.createNewFile(); // Exception here
} else {
    f.setLastModified(System.currentTimeMillis());
}

Getting exception:获取异常:

java.io.IOException: No such file or directory
  at java.io.UnixFileSystem.createFileExclusively(Native Method)
  at java.io.File.createNewFile(File.java:883)`

I have write permission to the path, however the file isn't created.我对该路径具有写权限,但是未创建该文件。

If the directory ../.foo/bar/ doesn't exist, you can't create a file there, so make sure you create the directory first.如果目录../.foo/bar/不存在,则无法在那里创建文件,因此请确保先创建目录。

Try something like this:尝试这样的事情:

File f = new File("somedirname1/somedirname2/somefilename");
if (!f.getParentFile().exists())
    f.getParentFile().mkdirs();
if (!f.exists())
    f.createNewFile();

Print the full file name out or step through in a debugger.打印完整的文件名或在调试器中逐步执行。 When I get confused by errors like this, it means that my assumptions and expectations don't match reality.当我对这样的错误感到困惑时,这意味着我的假设和期望与现实不符。 Make sure you can see what the path is;确保您可以看到路径是什么; it'll help you figure out where you've gone wrong.它会帮助你找出你哪里出错了。

Be careful with permissions, it is problably you don't have some of them.小心权限,很可能你没有其中的一些。 You can see it in settings -> apps -> name of the application -> permissions -> active if not.您可以在设置 -> 应用程序 -> 应用程序名称 -> 权限 -> 激活(如果没有)中看到它。

权限应用

Try with试试

f.mkdirs() then createNewFile() f.mkdirs()然后createNewFile()

You may want to use Apache Commons IO 's FileUtils.openOutputStream(File) method.您可能想要使用Apache Commons IOFileUtils.openOutputStream(File)方法。 It has good Exception messages when something went wrong and also creates necessary parent dirs.当出现问题时它有很好的异常消息,并且还会创建必要的父目录。 If everything was right then you directly get your OutputStream - very neat.如果一切正常,那么您将直接获得 OutputStream - 非常整洁。

If you just want to touch the file then use FileUtils.touch(File) instead.如果您只想touch该文件,请改用FileUtils.touch(File)

I got the same problem when using rest-easy.使用rest-easy时我遇到了同样的问题。 After searching while i figured that this error occured when there is no place to keep temporary files .经过搜索,我发现当没有地方保存临时文件时会发生此错误。 So in tomcat you can just create tomcat-root/temp folder.所以在 tomcat 中你可以只创建tomcat-root/temp文件夹。

File.isFile() is false if the file / directory does not exist, so you can't use it to test whether you're trying to create a directory.如果文件/目录不存在,则File.isFile()false ,因此您不能使用它来测试您是否正在尝试创建目录。 But that's not the first issue here.但这不是这里的第一个问题。

The issue is that the intermediate directories don't exist.问题是中间目录不存在。 You want to call f.mkdirs() first.您想先调用f.mkdirs()

i fixed my problem by this code on linux file system我在 linux 文件系统上通过这段代码解决了我的问题

if (!file.exists())
    Files.createFile(file.toPath());

暂无
暂无

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

相关问题 java.io.IOException: 无法运行程序“...”: java.io.IOException: error=2, No such file or directory - java.io.IOException: Cannot run program “…”: java.io.IOException: error=2, No such file or directory java.io.IOException: error=2, 没有那个文件或目录 - java.io.IOException: error=2, No such file or directory java.io.IOException:无法运行程序“/usr/bin/sh”:java.io.IOException:error=2,没有那个文件或目录 - java.io.IOException: Cannot run program “/usr/bin/sh”: java.io.IOException: error=2, No such file or directory java.io.IOException:无法运行程序“ C:\\ AutoIt \\ ModenaAutoIt.exe”:java.io.IOException:error = 2,没有此类文件或目录 - java.io.IOException: Cannot run program “C:\AutoIt\ModenaAutoIt.exe”: java.io.IOException: error=2, No such file or directory java.io.IOException:java.io.FileNotFoundException :(无此类文件或目录) - java.io.IOException: java.io.FileNotFoundException:(No such file or directory) File.createTempFile(错误:java.io.IOException:没有这样的文件或目录) - File.createTempFile (Error: java.io.IOException: No such file or directory) Java.io.IOException:错误= 2,在Java中执行curl时没有此类文件或目录 - Java.io.IOException: error=2, No such file or directory on executing curl in java Java,由:java.io.IOException: error=2, No such file or directory - Java, Caused by: java.io.IOException: error=2, No such file or directory 无法启动测试系统'slim':java.io.IOException:无法运行程序“ java”:error = 2,没有这样的文件或目录 - Unable to start test system 'slim': java.io.IOException: Cannot run program “java”: error=2, No such file or directory 遇到 java.io.IOException: open failed: ENOENT (No such file or directory) - Experiencing java.io.IOException: open failed: ENOENT (No such file or directory)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM