// Fileprint.java (c) 1998-2005 Kari Laitinen // http://www.naturalprogramming.com // 2002-09-12 File created. // 2005-05-08 Last modification. import java.io.* ; import java.util.* ; class Fileprint { public static void main( String[] not_in_use ) { Scanner keyboard = new Scanner( System.in ) ; System.out.print( "\n This program prints the contents of a text" + "\n file to the screen. Give a file name: " ) ; String file_name_from_user = keyboard.nextLine() ; try { BufferedReader file_to_print = new BufferedReader( new FileReader( file_name_from_user ) ) ; int line_counter = 0 ; boolean end_of_file_encountered = false ; while ( end_of_file_encountered == false ) { String text_line_from_file = file_to_print.readLine() ; if ( text_line_from_file == null ) { end_of_file_encountered = true ; } else { System.out.print( text_line_from_file + "\n" ) ; line_counter ++ ; } } file_to_print.close() ; System.out.print( "\n " + line_counter + " lines printed." ) ; } catch ( FileNotFoundException caught_file_not_found_exception ) { System.out.print( "\n \"" + file_name_from_user + "\" not found.\n" ) ; } catch ( IOException caught_io_exception ) { System.out.print( "\n\n File reading error. \n" ) ; } } }