user2210901
user2210901

Reputation: 51

java.io.FileNotFoundException when opening file with filewriter in Java

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)                       

Upvotes: 5

Views: 29272

Answers (6)

VGR
VGR

Reputation: 44413

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);

Upvotes: 10

subodh
subodh

Reputation: 6158

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.

Upvotes: 2

Zelldon
Zelldon

Reputation: 5516

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

Upvotes: 0

CloudyMarble
CloudyMarble

Reputation: 37576

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.

Upvotes: -1

devrobf
devrobf

Reputation: 7213

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.

Upvotes: 2

NPE
NPE

Reputation: 500913

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.

Upvotes: 3

Related Questions