1
java.io.File s1 = new java.io.File("/saves/save1.sav");
java.io.File s2 = new java.io.File("/saves/save2.sav");
java.io.File s3 = new java.io.File("/saves/save3.sav");
java.io.File s4 = new java.io.File("/saves/save4.sav");

This code sets the s variables to the location of specific files. I am looking for a way to use a for loop to check if variable s+"i" exists until it finds that it doesn't exist, then creates a new file named "save+"i".sav". (The +"i" means the number that will go after it.) Basically, I want it to create a new save file that does not overwrite other save files if there are any and gives it the name of "save(save #).sav". The code above may become obsolete. This way, I won't have to write a bunch of if statements and I can put all the code in a single statement. Thanks in advance to anyone willing to help. -Santiago

Santiago Benoit
  • 994
  • 1
  • 8
  • 22
  • 1
    Have you tried to write a loop to address the issue? Have you looked at the [`File`](http://docs.oracle.com/javase/8/docs/api/java/io/File.html) javadoc, namely `createNewFile`? – nanofarad May 07 '14 at 23:09
  • You can use a do/while loop and check if a file is available with `Files.notExists(s.toPath())`. – assylias May 07 '14 at 23:12
  • Actually, with a do-while loop in play, `exists` would make more sense than `notExists`. – Dawood ibn Kareem May 07 '14 at 23:15

2 Answers2

1

There are two parts to your question you can figure out separately, then just put them together.

First, you want to have a loop that looks at different filenames which only differ by an integer. You can do this by incrementing the integer in a for loop and using String.format() to make your path:

for (int n = 1; n <= 4; n++) {
    String filePath = String.format("/saves/save%d.sav", n);
    // ....
}

Once you have each filePath in this loop (set the range of integers how you like), you just need to check if it exists (which is explained here) and if not, create it:

for (int n = 1; n <= 4; n++) {
    String filePath = String.format("/saves/save%d.sav", n);
    File checkFile = new File(filePath);
    if (!checkFile.exists()) {
       try {
           checkFile.createNewFile();
           // If you want to write content to the file, do it here...
       } catch (IOException e) {
           e.printStackTrace();
       }
    } 
}
Community
  • 1
  • 1
Corbell
  • 1,283
  • 8
  • 15
  • Looking at your question again, I'm not sure if you were saying you only want to create one new file at the end of the list (and can assume they're all contiguous), or if you want to create any missing files (i.e. if you have files 1, 2, 3, 5, 6, create files 4 and 7). My code does the latter and assumes you don't want n to go up forever. (You could easily make the max of n the number of files in the directory + some number.) – Corbell May 07 '14 at 23:40
  • I know this is a dumb question, but where exactly would the file appear? I made a saves folder in the project root folder but I can't find the file that was supposedly created. I am using NetBeans IDE. – Santiago Benoit May 07 '14 at 23:53
  • It should show up there if your program has permission to write, assuming you are not on Windows (where you'd need to specify the root drive - C:/saves/...) – Corbell May 07 '14 at 23:59
  • Would I have to write the entire directory starting with C:/? This should be able to use the source folder as the root drive so that it works on any computer. I was able to load textures for an ImageIcon without starting with C:/ but just using /resources/images/image.png. I am noticing an error message whenever I run this code saying that the system cannot find the path specified. – Santiago Benoit May 08 '14 at 00:06
  • Your first sample uses absolute paths because they start with a separator. If you want to use relative paths, start with ".", or append your path to some known location like the home directory - myDataDirectory + "/saves/...", etc. – Corbell May 08 '14 at 00:22
0
File path_file = new File("/saves");
File f = null;
int i = 0;
while (f == null || f.exists()) {
    i++;
    String filename = "save" + i + ".sav";
    f = new File(path_file, filename);
}

Then f would be your new file.

Disclaimer: I haven't programmed in Java for a while.

Jeff
  • 552
  • 1
  • 8
  • 16