// Showfile.java Copyright (c) 2005 Kari Laitinen // http://www.naturalprogramming.com // 2005-05-09 File created. // 2005-05-09 Last modification. import java.util.* ; import java.io.* ; class Showfile { 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. Please, give the name" + "\n of a text file: " ) ; String file_name_from_user = keyboard.nextLine() ; try { BufferedReader file_to_print = new BufferedReader( new FileReader( file_name_from_user ) ) ; System.out.print( "\n The contents of file is: \n\n" ) ; int character_counter = 0 ; int character_code_from_file = file_to_print.read() ; while ( character_code_from_file != -1 ) { System.out.print( (char) character_code_from_file ) ; character_counter ++ ; character_code_from_file = file_to_print.read() ; } file_to_print.close() ; System.out.print( "\n " + character_counter + " characters were displayed on the screen.\n" ) ; } catch ( Exception caught_exception ) { System.out.print( "\n Cannot read file " + file_name_from_user ) ; } } }