I am using the .exists()
method to make sure a file does not exists and if it does, it adds a number to the file. However, it is not working and creating the file using the original filename and essentially overwriting the previous when creating another one.
This is my code:
int count = 1;
String name = "myFileName" + count;
String path = "/pathway/";
String ext = ".txt";
File file = new File(path + name + ext);
if (file.exists()) {
System.out.println("file found!");
name = "myFileName" + (count++);
file = new File(path + name + ext);
}
System.out.println("wrote file: " + name);
The output is this whenever I run it over again:
[1:47:40:359] wrote file: myFileName1
[1:47:40:361] wrote file: myFileName1