0

I have a system that writes a csv file. If the filename the user inputs already exists, then it asks if they would like to override it or rename it. If they choose to override it, the system works fine. However, when they choose to rename it, they get an error because the file object's canWrite() method returns false.


String saveName = scanner.next();
File csvFile = Paths.get(saveName + ".csv").toFile();

while (csvFile.exists()) {
    char answer = scanner.next().charAt(0);
    if (answer == 'y') {
        scanner.close();
        break;
    }
    else if (answer == 'n') {
        scanner.close();
        throw new IllegalArgumentException("Error: Rerun program with unique filename.");
    }
    else if (answer == 'r') {
        String response = scanner.next();
        csvFile = Paths.get(response + ".csv").toFile();
    }
}

if (!csvFile.canWrite()) {
    scanner.close();
    throw new IOException("Error: " + csvFile.getPath() + " is not writable.");
}

Any time the program gets into the final else-if in the while loop, it changes csvFile to another value, and then becomes unwritable. It works fine if I never get to that else-if statement and csvFile stays the same as the initial declaration before the while loop.

Birju Vachhani
  • 6,072
  • 4
  • 21
  • 43
Ethan Seal
  • 31
  • 8
  • Of course. How is Java supposed to know that the file has been renamed? It's pointing to a location which no longer exists so of course you can't write to it. If you want to stop people renaming it while you're using it then acquire a lock on the file. https://stackoverflow.com/questions/128038/how-can-i-lock-a-file-using-java-if-possible – Michael Jul 16 '19 at 18:17
  • Unable to reproduce. What .csv files do you have and what are your inputs? --- And remember, **`canWrite()` is false if the file doesn't exist**, as documented in the [javadoc](https://docs.oracle.com/javase/8/docs/api/java/io/File.html#canWrite--): *Returns true if and only if the file system actually contains a file denoted by this abstract pathname and the application is allowed to write to the file; false otherwise.* – Andreas Jul 16 '19 at 18:24
  • @Michael Sorry, I may have been a bit unclear. The user is not going into the file system and renaming a file. They are suggesting a name for a file, the system checks to see if a file with that name exists, and then prompts them to pick a different name to save their file. So my system never goes into the file system to change a file (unless they choose to overwrite, but that works fine). It's the reassignment in the code. Sorry if I didn't understand your response correctly. – Ethan Seal Jul 16 '19 at 18:25
  • @Andreas thank you, I did not realize this. I added the line csvFile.createNewFile() right after the while loop and it works like a charm. – Ethan Seal Jul 16 '19 at 18:41

1 Answers1

0

@Andreas pointed out that canWrite() returns false if the file does not exist, so I first had to call csvFile.createNewFile().

Ethan Seal
  • 31
  • 8