4

I would like to know how to open a file in java for writing, but I only want to open it if it exists already. Is that possible? I've searched around and found two suggestions so far which both don't meet my needs. One was to open a file for appending. The problem there is that if the file doesn't exist it will create it. The second was to use file.exists() first. That of course is not a valid solution as the file may exist when I call file.exists() but then not exist when I go to open the file. I want something similar to the Windows API OpenFile() in which I can pass OPEN_EXISTING flag such that the call will fail is the file doesn't exists. Does anything like that exist in java?

Only editing question because it was marked duplicate. Not sure why. I thought I was pretty specific about the other answers and why they were not sufficient.

So I'll restate, I want to open a file for writing. I want the open to fail if the file doesn't already exist.

3 Answers3

4

exists() returns true if the file path is a valid directory even if the file isn't there. You can get around this by using:

File f = new File(filePathString);

if(f.exists() && !f.isDirectory()) {/*Do whatever*/}

or use:

File f = new File(filePathString);

if f.isFile() {/*Do whatever*/}
javawocky
  • 899
  • 2
  • 9
  • 31
  • 1
    But I want to open the file for writing. I guess I could check for existence and if it exists open it for writing, but the problem then is the file could be removed between those two checks. –  Aug 06 '15 at 01:41
2

Just catch the FileNotFoundException that is thrown:

try (FileInputStream fis = new FileInputStream(file))
{
    // ...
}
catch (FileNotFoundException exc)
{
    // it didn't exist.
}

Solutions involving File.exists() and friends are vulnerable to timing-window problems and therefore cannot be recommended. They also merely duplicate what the OS already has to do, but at the wrong time, so they are a waste of time and space. You have to deal with IOExceptions anyway, so do that.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • 1
    A good suggestion, but as a side note in terms of space does isnt this more wasteful as your creating a FileDescriptor object you don't need, as well as tying up system resources for the stream itself? – javawocky Aug 03 '15 at 05:43
  • 1
    But I want to open the file for writing, so wouldn't I want a file output stream? –  Aug 06 '15 at 01:39
  • And if you're suggesting opening the file for writing once I know it exists, there is a chance it might get removed between those two operations. I don't want any chance for race conditions. –  Aug 07 '15 at 13:35
-1

No there is nothing like that In Java.... Core library

You should be able to wrap your logic in a if statement that names use of the file.exists() method. If you do the check just before opening the file the you would be extremely unlucky if someone has deleted the file in-between. The method that checks if the file exists and the code to open the file and lock it should run in milliseconds..

Eg

If (file.exists() {
  //Your Code  goes here.. 
} else {
  System.out.Print("missing file");
}
Jacques Ramsden
  • 801
  • 5
  • 20
  • 1
    I'm looking for a solution with no chances for failure. If it exists when I check it might not exist when I open for writing. –  Aug 06 '15 at 01:42