Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 955
Shervin Asgari Avatar asked Sep 19 '11 09:09

Shervin Asgari


1 Answers

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(); 
like image 121
Bohemian Avatar answered Oct 08 '22 09:10

Bohemian