Assignemnt #127 and Displaying A File
Code
/// Name: Brian Phillips
/// Period 5
/// Program Name: Displaying a File
/// File Name: DisplayingAFile.java
/// Date Finished: 6/1/2016
import java.util.Scanner;
import java.io.File;
import java.io.IOException;
public class DisplayingAFile
{
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
String fileName;
String x;
System.out.println("\nHere are a list of files you can open:\n\n- letter.txt\n- receipt.txt\n- 3nums2.txt\n- score.txt\n");
System.out.print("Which file would you like to open? ");
fileName = keyboard.nextLine();
System.out.println();
try{
Scanner fileIn = new Scanner(new File(fileName));
while (fileIn.hasNextLine())
{
x = fileIn.nextLine();
System.out.println(x);
}
fileIn.close();
System.out.println();
}
catch (IOException e){
System.out.println("Error");
}
System.out.println("\nDone");
}
}
Picture of the output