| . Is part of the java.io package |
| |
| . Is an extension of the FilterOutputStream class (a superclass
that facilitates the chaining of high-level and low-level output streams) |
| |
|
| |
| . Is a high-level class that can be used to send
primitive values and UTF ("Unicode Transformation Format") strings to a stream |
| |
| . Has a single constructor that "chains" to an object
that descends from the OutputStream class (such as a FileOutputStream object).
For example, if fd is the reference of a File object |
| DataOutputStream out = new
DataOutputStream(new FileOutputStream(fd)); |
| |
| It will construct a DataOutputStream object chained
to a FileOutputStream object for sending primitive values
and UTF strings to the file. Because a checked, IOException
may occur, the statement should be enclosed in a try block
with an appropriate catch. |
| |
| . Has many useful methods as follows: |
| |
|
| |
| Because a checked, IOException may occur, calls to
these methods should be enclosed in a try block with an
appropriate catch. Consult the Java API documentation for more details. |
| |
| . Example. The following program can be used to create
a file of double values on disk. |
| |
import java.io.*;
public class App
{
public static void main(String[] args)
{
// Local variables and object references.
char again = 'y';
File fd;
DataOutputStream out;
// Get the path name from the user.
System.out.print("Enter the file's complete path name: ");
fd = new File(Keyboard.readString());
// Try to write data to the output stream.
try
{
// Open an output stream for the file.
out = new DataOutputStream(new FileOutputStream(fd));
// This loop asks the user to enter a number and writes it to the
// stream. The user is then asked if they want to enter another.
while (again == 'Y' || again == 'y') {
System.out.print("Enter any real number (n.n): ");
double theNumber = Keyboard.readDouble();
out.writeDouble(theNumber);
System.out.print("Again? (Y/N): ");
again = Keyboard.readChar();
}
// Close the stream.
out.close();
System.out.println("Closed - " + fd.getPath());
}
// Catch an IOException if one is thrown.
catch (IOException e)
{
System.out.println(e.getMessage());
}
}
} |
| |
| |