File.createNewFile() thowing IOException No such file or directory

Related searches

I have a method that writes to a log file. If the file exists it should append to it, if not then I want it to create a new file.

if (!file.exists() && !file.createNewFile()) {
    System.err.println("Error with output file: " + outFile
        + "\nCannot create new file.");
    continue;
}

I have that to check that a file can be created. file is a java.io.File object. createNewFile is throwing an IOException: No such file or directory. This method has been working perfectly since I wrote it a few weeks ago and has only recently starting doing this although I don't know what I could have changed. I have checked, the directory exists and I have write permissions for it, but then I thought it should just return false if it can't make the file for any reason.

Is there anything that I am missing to get this working?

normally this is something you changed recently, first off your sample code is if not file exists and not create new file - you are trying to code away something - what is it?

Then, look at a directory listing to see if it actually exists and do a println / toString() on the file object and getMessage() on the exception, as well as print stack trace.

Then, start from zero knowledge again and re factor from the get-go each step you are using to get here. It's probably a duh you stuck in there somewhere while conceptualizing in code ( because it was working ) - you just retrace each step in detail, you will find it.

Cannot make file java.io.IOException: No such file or directory , Print the full file name out or step through in a debugger. When I get foo/bar/ doesn't exist, you can't create a file there, so make sure you create the directory first. getParentFile().mkdirs(); if (!f.exists()) f.createNewFile();. While coding Java had this problem: file.createNewFile() throws IOException "No such file or directory". Checked out other answers but nothing seems to be helpful.

try to ensure the parent directory exists with:

file.getParentFile().mkdirs()

java.io.IOException: No such file or directory at java.io.File , IOException: No such file or directory at java.io.UnixFileSystem. createFileExclusively(Native Method) at java.io.File.createNewFile(File.java:883) at� Apache NetBeans Bugzilla – Bug 192790 java.io.IOException: No such file or directory Last modified: 2016-03-21 14:50:31 UTC

Perhaps the directory the file is being created in doesn't exist?

Solved: java.io.IOException: No such file or directory whi, If the file exists it should append to it, if not then I want it to create a new file. if (! file.exists() createNewFile is throwing an IOException: No such file or directory. createNewFile() ) or the "file" is already a directory. Try ( file. IOException: No such file or directory instead of telling the user what is wrong #6520. (Native Method) at java.io.File.createNewFile(File.java:1006) at org

I think the exception you get is likely the result from the file check of the atomic method file.createNewFile(). The method can't check if the file does exist because some of the parent directories do not exist or you have no permissions to access them. I would suggest this:

if (file.getParentFile() != null && !file.getParentFile().mkDirs()) {
    // handle permission problems here
}
// either no parent directories there or we have created missing directories
if (file.createNewFile() || file.isFile()) {
    // ready to write your content
} else {
    // handle directory here
}

If you take concurrency into account, all these checks are useless because in every case some other thread is able to create, delete or do anything else with your file. In this case you have to use file locks which I would not suggest doing ;)

File.createNewFile() thowing IOException N, Creates a new directory in the default temporary-file directory, using the given If no options are present then it is equivalent to opening the file with the newOutputStream(path, CREATE, APPEND); // always create new file, failing if or next method throwing a DirectoryIteratorException with the IOException as the cause. I am trying to send an image file through android using a server socket. I have used AsyncTask class on the client side and created a TransferFile class to send the file. I am getting a NO SUCH FILE OR DIRECTORY exception while creating the file on client's side. The code is attached below. Permissions.

According to the [java docs](http://java.sun.com/j2se/1.5.0/docs/api/java/io/File.html#createNewFile() ) createNewFile will create a new file atomically for you.

Atomically creates a new, empty file named by this abstract pathname if and only if a file with this name does not yet exist.

Given that createNewFile is atomic and won't over-write an existing file you can re-write your code as

try {
    if(!file.createNewFile()) {
        System.out.println("File already exists");
    } 
} catch (IOException ex) {
    System.out.println(ex);
}

This may make any potential threading issues, race-conditions, etc, easier to spot.

Files (Java Platform SE 8 ), If the destination directory is not existing, it will throw DownloadManager.ERROR_FILE_ERROR in the failure callback. I should get two inputs for downloadrequest if this issue needs to be solved. 1) Destination Directory 2) Destination file name. So that i can create the directory if it doesn't exists and create the file under the directory.

ASF Bugzilla – Bug 57947 "No such file or directory" after upgrade to poi-ooxml 3.12 Last modified: 2016-03-12 17:04:45 UTC

File createNewFile() method returns true if new file is created and false if file already exists. This method also throws java.io.IOException when it’s not able to create the file. The files created is empty and of zero bytes.

Apache NetBeans Bugzilla – Bug 144114 java.io.IOException: No such file or directory Last modified: 2009-10-06 20:04:08 UTC

Comments
  • Does the IOException contain a message or anything else that might be helpful?
  • May be old, but you'd get the same error if you don't have permissions to write to where you're wanting to write to. <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> fixes it.
  • Thanks. I spent a while going back through everything and found where it reads the path from a cfg file it wasn't removing a colon. output_file:path When it was last working I hadn't done the cfg file yet, it was a hard coded path.
  • @Android: Just curious: Is this any different from what I recommended in stackoverflow.com/questions/1525060/…?
  • @Hemal Pandya - I saw your comment, and as noted it is correct - I just have been through this so many times i thought I could word it better for the poster. @Android - remember this lesson well, let it constantly be with you. World class operations have been completely bamboozled by this a sufficient number of times that carefully crafted recovery is a super-critical central skill, one that only comes with doing this type of recovery work. Use some type of Feed-Forward archive, with gig drives available at hand, there is no reason to lose recoverable trails.
  • Was no dir in my case.
  • @JorgeAntonioDíaz-Benito I imagine the upvote is from you? Thank you for that.
  • You shouldn't just log an exception, except when the operation was really not vital to your program. If createNewFile throws an exception then it's safe to assume that you either wanted that file to be created (it wasn't -> bad) or you wanted to later write to it (will result in an exception -> bad). In any case it's good to fail fast and let the caller decide how to procede.
  • Is this any different from my answer?
  • Have you read the JavaDocs for createNewFile? Checking and creating is indeed an atomic operation and hence thread safe.
  • file.exist() is atomic and file.createNewFile is atomic. But between these two calls concurrent changes can occur.
  • just have a look at this answer: stackoverflow.com/questions/1525060/…
  • agree with Thomas, he's talking about a threading issue. The statement "!file.exists() && !file.createNewFile()" calls two methods on the "file" object. While both methods are atomic, other code running at the same time can create the file between these two atomic actions. Refer to: en.wikipedia.org/wiki/Race_condition