31

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.

Bohemian
  • 412,405
  • 93
  • 575
  • 722
Shervin Asgari
  • 23,901
  • 30
  • 103
  • 143

8 Answers8

57

If the directory ../.foo/bar/ doesn't exist, you can't create a file there, so make sure you create the directory first.

Try something like this:

File f = new File("somedirname1/somedirname2/somefilename");
if (!f.getParentFile().exists())
    f.getParentFile().mkdirs();
if (!f.exists())
    f.createNewFile();
John Topley
  • 113,588
  • 46
  • 195
  • 237
Bohemian
  • 412,405
  • 93
  • 575
  • 722
28

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.

duffymo
  • 305,152
  • 44
  • 369
  • 561
  • 1
    Can't imagine why this was down voted 5.5 years after the fact when it was accepted by the OP and acknowledged as the correct fix in a comment. Feels vindictive. Easy to do when anonymous. – duffymo Mar 09 '17 at 17:12
  • Its easy yes, but whoever downvotes also looses reputation, so it not "free" to downvote. – Shervin Asgari Mar 13 '17 at 13:33
  • 2
    I have enough where I don't care. It's the attitude that bothers me, not the loss of rep. – duffymo Mar 13 '17 at 13:34
  • I am having same trouble while merging two videos I am passing paths of two videos which I can see in my log and the path of resulting video is same as these videos but it throws this error -java.io.IOException: No such file or directory – Akash Dubey May 17 '17 at 06:04
  • I have also checked using Bohemian's answer weather my video paths exist, and yes they exists still It throws error!! Need help – Akash Dubey May 17 '17 at 06:05
  • You are still doing it wrong, then. Believe the JVM. Keep looking. Don't hijack 6 year old answers. – duffymo May 17 '17 at 09:02
  • the error for me was because of the case sensitive check in UNIX system on the path. So make sure to check out the leading folder path CASE in the code vs in the filesystem. – KarthiS Jan 25 '19 at 15:03
13

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.

Permissions app

Danh
  • 5,916
  • 7
  • 30
  • 45
4

Try with

f.mkdirs() then createNewFile()

jmj
  • 237,923
  • 42
  • 401
  • 438
  • 3
    Can you try [`File.separator`](http://download.oracle.com/javase/6/docs/api/java/io/File.html#separator) instead of '/' – jmj Sep 19 '11 at 09:52
2

You may want to use Apache Commons IO's FileUtils.openOutputStream(File) method. 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.

If you just want to touch the file then use FileUtils.touch(File) instead.

Fabian Barney
  • 14,219
  • 5
  • 40
  • 60
1

I got the same problem when using 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.

Chinthaka Dinadasa
  • 3,332
  • 2
  • 28
  • 33
1

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. 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.

Sean Owen
  • 66,182
  • 23
  • 141
  • 173
0

i fixed my problem by this code on linux file system

if (!file.exists())
    Files.createFile(file.toPath());
Ahmad R. Nazemi
  • 775
  • 10
  • 26