// FileToNumbersBuffered.java (c) 2005 Kari Laitinen // http://www.naturalprogramming.com // 2005-05-08 File created. // 2005-05-08 Last modification. // This program works like FileToNumbers.java, but because in // this version a FileInputStream object is put "inside" a // BufferedInputStream object, this program should be more efficient // than FileToNumbers.java. import java.io.* ; class FileToNumbersBuffered { public static void main( String[] command_line_parameters ) { if ( command_line_parameters.length == 1 ) { try { BufferedInputStream file_to_read = new BufferedInputStream( new FileInputStream( command_line_parameters[ 0 ] ) ) ; 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, bytes_from_file.length )) > 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 " + command_line_parameters[ 0 ] ) ; } catch ( IOException caught_io_exception ) { System.out.print( "\n Error while processing file " + command_line_parameters[ 0 ] ) ; } } else { System.out.print( "\n You have to command this program as: \n" + "\n FileToNumbers file.ext \n") ; } } } /* NOTES: This program uses Java 2 Standard Edition, Version 1.5 features. Compilation with older Java compilers is thus not possible. */