The while loop of the program puts five values of

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: m. The while loop of the program puts five values of type int to the file. Each int value occupies four bytes in the file, and the bytes are in such an order that the most significant byte is the first in the file. D:\javafiles3>java NumbersToFile D:\javafiles3>java FileToNumbers NumbersToFile_output.data 00 00 00 22 00 00 00 33 00 00 00 44 00 00 00 55 00 00 00 66 12 34 3F F3 C0 83 12 6E 97 8D 01 00 00 08 61 61 41 41 62 62 42 42 4B 61 72 69 The contents of a string are stored in UTF-8 format. Each character code occupies only a single byte. Before the character codes, there is a two-byte (16-bit) value that tells how many bytes are stored. In this case there are 8 bytes. The UTF-8 encoding is such that the characters whose codes are less than 0x80 (i.e., the normal characters and symbols in the English alphabet) are represented by a single byte. The UTF-8 encoding used by the writeUTF() method differs slightly from the official UTF-8 format. (See page 456 for more information about UTF-8.) " 3 D U f 4???? n?? aaAAbbBBKari The double value 1.2345 is stored in eight bytes, but the number is encoded so that it is not possible to read it. The boolean values true and false are stored as bytes containing 1 and 0, respectively. NumbersToFile.java - X. Examining the contents of file "NumbersToFile_output.data". Exercises related to binary files Exercise 14-6. Write a program that compares two files to find out if the files have the same contents. Two files are equal if their lengths are equal, and they contain the same bytes in the same order. You can use method read() of class FileInputStream to read equal size byte blocks from two files. After each reading operation you must test if the byte blocks are the same. Exercise 14-7. Write a program (e.g. FilecopyBinary.java) that makes a copy of a file by using binary file access. You can use the read() and write() methods of classes FileInputStream and FileOutputStream to move byte blocks from one file to another. You do not need to use the DataOutputStream class because class FileOutputStream has a suitable write() method. Exercise 14-8. Program FileToNumbers.java may be a useful program when you work with files, but the program is not convenient to use when large files are examined with it. The contents of a large file do not fit to a single screen. Improve the program so that when it prints the contents of a file it asks the user of the program to press the Enter key before it shows the following 20 or so lines on the screen. 477...
View Full Document

This document was uploaded on 01/20/2014.

Ask a homework question - tutors are online