Now we have got an overview of the class hierarchies involved in Java I/O its time to investigate these classes in more detail. In our second lesson on Java I/O we take a closer look at some of the byte stream classes that are available for our use in the java.io package and how we use them.
The byte stream classes are the original input and output streams which were shipped with JDK 1.0. This is the reason why System.in, System.out and System.err use the InputStream and PrintStream types from the byte stream classes and not the character stream classes as you would expect. The character stream classes were introduced to the language in JDK 1.1.
The java.io.OutputStream Class Top
At the top of the byte output stream hierarchy is the java.io.OutputStream abstract superclass which defines a basic set of output functions that all byte output stream classes have. These methods allow us to flush and write bytes within the byte output stream as well as close the stream when we finish our processing. All methods within the java.io.OutputStream class can throw an IOException.
The table below shows the declarations of all the methods in the java.io.OutputStream class:
Method Declaration
Description
public void close()
Closes output stream and release any system resources associated with it.
public void flush()
Flushes output stream and forces any buffered output bytes to be written out.
public abstract void write(int b)
Writes a single byte of data specified by b to the output stream.
public void write(bytes[] b)
Writes b bytes of data from b array.
public void write(bytes[] b, int off, int len)
Writes len bytes of data from b array starting from off.
write(bytes[]) Example Top
Lets see how we could use the write(bytes[]) method above to write a sequence of characters to the console:
/*
Write character sequence to the console
*/
import java.io.IOException; // Import the IOException class from java.io package
class WriteToConsole {
public static void main(String[] args) {
try {
byte consoleOut[] = new byte[]{‘J’,’a’,’v’,’a’,’6′};
System.out.write(consoleOut);
}
catch (IOException ex) {
System.out.println(“Caught IOException: ” + ex);
}
}
}
Save, compile and run the WriteToConsole test class in directory c:_APIContents2 in the usual way.
The above screenshot shows the output of compiling and running the WriteToConsole class. Using System.out.print and System.out.println are a lot easier to use but you may need to write to the console this way, for some reason.
The java.io.FileOutputStream Class Top
We saw above how we can write bytes to the console using methods from the java.io.OutputStream class. This doesn’t really offers us anything practical when it comes to storing information on the underlying file system. Luckily for us the subclasses in the byte output stream hierarchy give us ways to persist our data. In this part of the lesson we examine the java.io.FileOutputStream class which allows us to output data to a file on the system. This class offers us constuctors to write to physical files represented by File, FileDescriptor and String objects that point to the actual files on the system.
Writing To A File Top
For our example of using the java.io.FileOutputStream class we will create a File object in our working directory and write some data to the file:
/*
Write character sequence output to a file using FileOutputStream
*/
import java.io.*; // Import all file classes from java.io package
class TestFileOutputStream {
public static void main(String[] args) {
byte someData[] = new byte[]{‘J’,’a’,’v’,’a’,’6′}; // Our output data
FileOutputStream fos; // Our FileOutputStream
File newFile = new File(“FileOutputStream.txt”);
if (newFile.exists()) { // Check to see if FileOutputStream.txt already exists
System.out.println(“FileOutputStream.txt already exists.”);
} else {
System.out.println(“Try to create FileOutputStream.txt”);
try {
newFile.createNewFile(); // Create a file called FileOutputStream.txt
System.out.println(“We created FileOutputStream.txt”);
}
catch (IOException ex) {
System.out.println(“We caught exception: ” + ex);
}
}
// Open FileOutputStream.txt and output some data to it
try {
fos = new FileOutputStream(newFile); // Create a stream to write to FileOutputStream.txt
System.out.println(“FileOutputStream fos has been opened.”);
for (int i=0;i
Next >>
Question 2 : What is the abstract superclass of byte input streams called?
·InputStream
·Reader
·FileStream
·FileReader
– The abstract superclass of byte input streams is called InputStream
.
<< Prev
Next >>
Question 3 : How can we persist object state?
·Using the FileOutputStream class
·Using the OutputStream class
·Using the DataOutputStream class
·Using the ObjectOutputStream class
– We can persist object state using the ObjectOutputStream
class.
<< Prev
Next >>
Question 4 : Which class implements all the methods of InputStream with versions that pass all requests to the contained input stream?
·FileinputStream
·FileStream
·FilterinputStream
·FilterStream
– The FilterinputStream
implements all the methods of InputStream
with versions that pass all requests to the contained input stream.
<< Prev
Next >>
Question 5 : We can use the read() method of which class to read character input from the keyboard?
·FileinputStream
·InputStream
·KeyBoardInputStream
·DataInputStream
– We can use the InputStream
class via System.in.read()
.
<< Prev
Next >>
Question 6 : Can we use more than one stream when outputting data to storage?
·yes
·no
– We can use more than one stream when outputting data to storage; in fact it’s often the case that we want to ‘mix and match’ our streams.
<< Prev
finish!
Status Bar Please select an answerWhat's Next?
In our second lesson on Java I/O we look at byte streams.
<< Java I/O Overview Character Stream Classes >>
Java6 Tutor Homepage Top
All the Java6 API Contents lessons are listed below. Click a link to go to that lesson.
API Contents
The String Class
The StringBuilder Class
Packages
Java I/O Overview
Byte Stream Classes
java.io.OutputStream
write(bytes[]) Example
java.io.FileOutputStream
Writing Bytes To A File
java.io.DataOutputStream
Writing Binary Data To A File
java.io.ObjectOutputStream
Writing Objects To A File
java.io.InputStream
read() Example
read(bytes[]) Example
java.io.FileInputStream
Reading Bytes From A File
java.io.DataInputStream
Reading Binary From A File
java.io.ObjectInputStream
Reading Objects From A File
Byte Stream Classes Quiz
Character Stream Classes
Dates, Numbers & Currencies
Regular Expressions
Formatting & Tokenizing