Java figure 14 3 describes how a dataoutputstream

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: taOutputStream object co-operates with a FileOutputStream object when data is written to a file. Class DataOutputStream has a "sister class" named DataInputStream. The methods of DataInputStream class are convenient when we need to read data that is written with the DataOutputStream methods. In the next section a program named Collect.java uses both DataOutputStream and DataInputStream objects in file operations. Program NumbersToFile.java destroys the previous content of its output file if the file already exists. If you want to write data to a binary file without destroying the previously written data, you must open the file in appending mode. If the file opening statement of program NumbersToFile.java were written in the following way FileOutputStream file_output_stream = new FileOutputStream( "NumbersToFile_output.data", true ) ; where the constructor of class FileOutputStream is given two parameters, the program would append data to the end of the NumbersToFile_output.data file, and the file would grow always when the program is executed. When the second constructor parameter is the literal value true, or a boolean variable whose value is true, the file is opened in appending mode. Also in the appending mode the file is created if it does not yet exist. These are sample pages from Kari Laitinen’s book "A Natural Introduction to Computer Programming with Java". For more information, please visit http://www.naturalprogramming.com/javabook.html 473 14.3 Handling files as binary files Class DataOutputStream provides methods like writeInt(), writeShort(), writeChar(), writeUTF(), etc. With these methods it is possible to write many kinds of data items (e.g. values of types int, short, char, and String) to a binary file. You can think that the various kinds of data items that go into the DataOutputStream object are converted to byte arrays and written to the disk via the FileOutputStream object. int DataOutputStream short object File on disk FileOutputStream object Array of bytes char ... S t r i n g et...
View Full Document

This document was uploaded on 01/20/2014.

Ask a homework question - tutors are online