// FileToNumbersAlternative.java (c) 2005 Kari Laitinen // http://www.naturalprogramming.com // 2005-11-19 File created. // 2005-11-19 Last modification. // This program works like FileToNumbers.java but this one // does not use command line parameters. import java.io.* ; import java.util.* ; class FileToNumbersAlternative { public static void main( String[] not_in_use ) { Scanner keyboard = new Scanner( System.in ) ; System.out.print( "\n Please, give file name: " ) ; String given_file_name = keyboard.nextLine() ; try { FileInputStream file_to_read = new FileInputStream( given_file_name ) ; byte[] bytes_from_file = new byte[ 16 ] ; int number_of_bytes_read ; while (( number_of_bytes_read = file_to_read.read( bytes_from_file )) > 0 ) { StringBuilder line_of_bytes = new StringBuilder() ; StringBuilder bytes_as_characters = new StringBuilder () ; for ( int byte_index = 0 ; byte_index < number_of_bytes_read ; byte_index ++ ) { line_of_bytes.append( String.format( " %02X", bytes_from_file[ byte_index ] ) ) ; char byte_as_character = (char) bytes_from_file[ byte_index ] ; if ( byte_as_character >= ' ' ) { bytes_as_characters.append( byte_as_character ) ; } else { bytes_as_characters.append( ' ' ) ; } } System.out.printf( "\n%-48s %s", line_of_bytes, bytes_as_characters ) ; } file_to_read.close() ; } catch ( FileNotFoundException caught_file_not_found_exception ) { System.out.print( "\n Cannot open file " + given_file_name ) ; } catch ( IOException caught_io_exception ) { System.out.print( "\n Error while processing file " + given_file_name ) ; } } } /* NOTES: This program uses Java 2 Standard Edition, Version 1.5 features. Compilation with older Java compilers is thus not possible. */