// TextFileToNumbers.java (c) 2005 Kari Laitinen // http://www.naturalprogramming.com // 2005-08-03 File created. // 2005-08-03 Last modification. import java.io.* ; import java.util.* ; class TextFileToNumbers { 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 as hexadecimal numbers. Give file name: " ) ; String file_name_from_user = keyboard.nextLine() ; try { BufferedReader file_to_print = new BufferedReader( new FileReader( file_name_from_user ) ) ; 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( "\n" ) ; for ( int character_index = 0 ; character_index < text_line_from_file.length() ; character_index ++ ) { int character_code = text_line_from_file.charAt( character_index ) ; System.out.printf( "%02X ", character_code ) ; } // Let's forge Windows-style line termination characters System.out.print( "0D 0A " ) ; } } file_to_print.close() ; } 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" ) ; } } }