-2

If you are using a FileWriter in append mode (i.e append set to true in the constructor) is there any easy way of determining if you are appending to an existing file or writing to a new file?

Clearly you could try reading the file first but I wonder if there a one liner to know whether you are creating the file or not?

Thanks

Dan
  • 9,681
  • 14
  • 55
  • 70

1 Answers1

0

Before initializing the FileWriter you can use file.exists() (which returns a boolean) to see if the file exists. Then you can do other checks such as file.isFile() and !file.isDirectory() to make sure that these are files you can write to.

Zach
  • 4,652
  • 18
  • 22