/********************************************************************** * Author: Eric Heim * Date Created: 7/14/2011 * Course: CS0007 * Description: Shows the use of the exists method to detect if a file * exists for reading. **********************************************************************/ import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; public class ReadExists{ public static void main(String[] args) throws FileNotFoundException { Scanner keyboard = new Scanner(System.in); File myFile; Scanner inputFile; String filename; System.out.print("Please enter the name of your file: "); filename = keyboard.nextLine(); myFile = new File(filename); while(!myFile.exists()){ System.out.println("The filename entered does not exist"); System.out.print("Please enter the name of your file: "); filename = keyboard.nextLine(); myFile = new File(filename); } inputFile = new Scanner(myFile); System.out.println("The contents of the file are: "); while(inputFile.hasNext()) { System.out.println(inputFile.nextLine()); } inputFile.close(); } }