java.io.FileNotFoundException when opening file with filewriter in Java
Asked Answered
F

6

5

I am trying to write something to a file, like this:

FileWriter fw = new FileWriter("somefile.txt", true);

It works correctly when started by a single process. Like this:

java -jar XXXXXXX.jar

But when calling it by another process, an IOException will be thrown. Example:

java.io.FileNotFoundException: 'somefile.txt' (No such file or directory)
    at java.io.FileOutputStream.openAppend(Native Method)                      
    at java.io.FileOutputStream.<init>(FileOutputStream.java:192)              
    at java.io.FileOutputStream.<init>(FileOutputStream.java:116)             
    at java.io.FileWriter.<init>(FileWriter.java:61)                       
Faber answered 26/3, 2013 at 9:39 Comment(3)
Define the "other process". How do you start your program?Thirst
Maybe because the file does not exist in the folder where your other process is located?Upheave
Most likely, your first process started using the file and didn't close the reference to it. Hence, when your second process is trying to access it(append mode), it is throwing the FNFE.Dekeles
C
10

A number of answers have incorrectly suggested that your exception is occurring because the file doesn't exist. That is not the reason; the documentation for the constructor clearly states:

Throws:
IOException - if the named file exists but is a directory rather than a regular file, does not exist but cannot be created, or cannot be opened for any other reason

If you are passing a relative file name (a string with no '/' or '\' in it), it refers to a file in the current directory. I'm guessing that when you run it using java -jar, your current directory is a directory for which you have write permission, but when that other process runs it, the current directory is not writable.

In the past, older Java versions had the habit of throwing FileNotFoundException when trying to write in an unwritable directory. The latest Java doesn't seem to do it, though, so I'm not certain if that's the problem. You can get a clearer exception by using the java.nio.file package instead:

Path path = Paths.get("somefile.txt");
Writer writer = Files.newBufferedWriter(path, Charset.defaultCharset(),
    StandardOpenOption.APPEND, StandardOpenOption.CREATE);
Campanile answered 26/3, 2013 at 10:6 Comment(2)
In this case should not he get an access denied message?Volt
You're right, the exception message would end with '(Permission denied)' or '(Access is denied)' or something similar. I'm sure I've seen cases, at least in older versions of Java, where the inability to write a file resulted in a 'No such file or directory' message, but I don't know to re-create it.Campanile
P
3

There are several possible explanations:

  1. The process does not have permissions to create somefile.txt in the current directory.
  2. On some operating systems, it might not be possible to create/overwrite the file if it already exists and is in use by another process.
Provitamin answered 26/3, 2013 at 9:42 Comment(0)
C
2

As the Exception states, the file somefile.txt does not exist. This would be fine except that the second argument to the FileWriter constructor indicates that you want to append to an existing file, meaning that the file must exist. I suggest that you check for existence of the file using File.exists(), and if it exists use new FileWriter("somefile.txt", true);, otherwise use new FileWriter("somefile.txt", false); to create the file for the first time.

Cham answered 26/3, 2013 at 9:43 Comment(0)
P
2

When start process2:

  1. It will try to find the somefile.txt in your current directory.

  2. If file not found with given name in current directory then,It will try to create a new one,but due to user rights not able to create a new file with given name.

Check it manually, file is exist or not in your current directory.

Pinochle answered 26/3, 2013 at 9:56 Comment(0)
F
0

JavaDoc

This exception will be thrown by the FileInputStream, FileOutputStream, and RandomAccessFile constructors when a file with the specified pathname does not exist. It will also be thrown by these constructors if the file does exist but for some reason is inaccessible, for example when an attempt is made to open a read-only file for writing.

http://docs.oracle.com/javase/1.5.0/docs/api/java/io/FileNotFoundException.html

maybe you have opened the file with a process and not closed the file, so if you try to open the file again these exception will be thrown because the file can't opened twice

Frottage answered 26/3, 2013 at 9:47 Comment(0)
V
-1

Considering the FileNotFoundException it states pretty clear that the file is just not there.

I guess your second process starts at some root folder where the file is just not there, use the absolute path of the file to ensure that this is the issue. Or just use a simple check

 if (yourFile.exists())

before you access it, and if not show the path beeing used by the program.

Volt answered 26/3, 2013 at 9:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.