The format specifier 48s stipulates that the tostring

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: ipulates that the toString() method is invoked for the object referenced by line_of_bytes, and the string is printed left-justified into a printing field that is 48 character positions wide. FileToNumbers.java - 1: A program to show the contents of a file as hexadecimal bytes. 14.3 Handling files as binary files FileNotFoundException is thrown by the constructor of class FileInputStream if it cannot find a file with the specified file name. It is important that all files, both binary and text files, are closed with a close() method after they are not used any more. 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 java FileToNumbers file.ext \n") ; } } The only way to give a file name to this program is to write it on the command line. } FileToNumbers.java - 2. The last part of the program. D:\javafiles3>type ministory.txt 1 == ministory.txt === 2 3 aaa AAA bbb BBB 4 5 This is the end. The contents of the StringBuilder object referenced by bytes_as_characters are printed after the contents of the object referenced by line_of_bytes. D:\javafiles3>java FileToNumbers ministory.txt 31 78 20 0A 6E 20 74 41 35 64 3D 20 41 20 2E 3D 3D 41 54 0D 20 3D 20 68 0A 6D 3D 62 69 0D 69 0D 62 73 0A 6E 0A 62 20 69 32 20 69 73 0D 42 73 74 0A 42 20 6F 33 42 74 72 20 0D 68 79 61 0A 65 2E 61 34 20 74 61 0D 65 1 == ministory.t xt === 2 3 aaa AAA bbb BBB 4 5 This is the e nd. All bytes from the file are shown in hexadecimal form. A newline character is represented by two character codes, 0DH and 0AH, inside the file. There are two newlines at the end of the file. FileToNumbers.java - X. Showing the contents of file "ministory.txt&quo...
View Full Document

Ask a homework question - tutors are online