Supposing that a file is opened with the above

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: d with the above statement, and that there is the array byte some_array_of_bytes = new byte[ 300 ] ; containing some data, the statement binary_file_for_writing.write( some_array_of_bytes ) ; would write all the 300 bytes from the array referenced by some_array_of_bytes to the file represented by the FileOutputStream object referenced by binary_file_for_writing, and the statement binary_file_for_writing.write( some_array_of_bytes, 2, 5); would use a different version of the write() method, and write the five bytes starting from the third array position that has index 2. The first parameter for the write() method specifies the byte array from which bytes should be copied to a file, the second parameter specifies the array position from which the writing operations should begin, and the third parameter says how many bytes should be written. As there are several versions of the write() method, there is also a version with which it is possible to write just a single byte to a file. This write() method could be called with a statement like binary_file_for_writing.write( 0x22 ) ; which would write the hexadecimal value 0x22 to the binary file that was opened above. NumbersToFile.java is a program that demonstrates how data can be written to a binary file. The program uses class FileOutputStream to open a file for binary writing operations. In addition, the program uses a class named DataOutputStream in the writing operations. In program NumbersToFile.java, a file named NumbersToFile_output.data is first opened for binary writing operations with the statement FileOutputStream file_output_stream = new FileOutputStream( "NumbersToFile_output.data" ) ; and then the FileOutputStream object is used to construct a DataOutputStream object with the statement DataOutputStream file_to_write = new DataOutputStream( file_output_stream ) ; and it is the DataOutputStream object that is used later to write data to the file. 472 Chapter 14: Storing information in files The DataOutputStream class provides many methods for writing operations, and it is therefore useful in a program like NumbersToFile.java. Figure 14-3 describes how a Da...
View Full Document

Ask a homework question - tutors are online