-3

So I have a program where I need to create a file and read from it once.

The only issue i'm having is for how to check if the file name already exists. Right now it's only a simple set up but I'm going to have to build up on it for later high end methods and checking.

So this is what I have so far -

    x = new Formatter(filename + ".csv");
    x.format("%s, %s", height, weight);
    x.close();

So how do I let my formatter check for the already existing value "filename"?

Willza99
  • 1
  • 1
  • 3
  • possible duplicate of [How do I check if a file exists? (Java on Windows)](http://stackoverflow.com/questions/1816673/how-do-i-check-if-a-file-exists-java-on-windows) – halex Nov 30 '13 at 14:16
  • I'm using Formatter, loads of people do use File, but I prefer this. – Willza99 Nov 30 '13 at 14:19
  • 1
    The `Formatter` doesn't do what you are trying, probably because that code is already tested and proven valid using the `File.exists()` method as people have pointed out. Just use `File.exists()` on the path and then you can use your `Formatter`. Also, please see all of the warnings about `File.exists()` including returning true for directories and the fact that between `File.exists()` and you writing to the file someone else could have created a file so you still need to catch the `FNF`. – Chris Haas Nov 30 '13 at 15:52

1 Answers1

0
File file = new File(PATH, fileName + ".csv");
if (file.exists()) ...;
ferrerverck
  • 618
  • 3
  • 9
  • 17