I'm asking the user to give me the name of the file they wish to write to. If that file already exists, I will ask them if they wish to update that file, or just create a different file with a different name. If the file doesn't exist, I just create the file right there.
This was working well through the ".exists()" method of the File class, but apparently RandomAccessFiles can't do that??
I looked the API for the RAF and nowhere have I found anything that resembles ".exists()".
Here's my code:
System.out.println("What is the name of the file you would like to write to?");
public RandomAccessFile receiveFile () { String fileName; String yesno = "y"; RandomAccessFile raf = null; Scanner input = new Scanner(System.in); do { System.out.println("What is the name of the file you would like to write to?"); fileName = input.next(); try { raf = new RandomAccessFile(fileName, "rw"); } catch (IOException ex) { System.out.println(); } // check if file already exists if(raf.exists()) // ERROR THIS DOESN'T WORK { System.out.println("That file already exists. Would you like to update it? (y/n)"); yesno = input.next(); } } while (yesno.equals("n")); return raf; }