This is just a simple program to check if a file exists or not, with the user input and the results 'File exits'/'File does not exist' all in the main method and the actual Exception catching being done in a separate method. I've got the basics of the doesFileExist method to catch any exceptions, but I can't figure out how to return that information to the main method so I can inform the user of the results.
Code:
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.Scanner;
public class FileApp {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String filePath = doesFileExist(sc, "Enter the file's full path: ");
if(!validFilePath) {
System.out.println("This File does not exist.");
}
else
System.out.println("This File exists.");
} // End Main
public static String doesFileExist(Scanner sc, String prompt) {
RandomAccessFile file;
String filePath = null;
boolean validFilePath = false;
while (!validFilePath) {
try {
file = new RandomAccessFile(filePath, "p");
validFilePath = true;
} // End Try Statement
catch (FileNotFoundException e) {
} // End Catch Statement 1
catch (IOException e) {
} // End Catch Statement 2
} // End doesFileExist Class
return filePath;
} // End Class FileApp
}