Byte Stream Classes

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 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.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 Class Top
At the top of the byte output stream hierarchy is the 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 class can throw an IOException.
The table below shows the declarations of all the methods in the class:
Method Declaration
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; // Import the IOException class from package

class WriteToConsole {
public static void main(String[] args) {
try {
byte consoleOut[] = new byte[]{‘J’,’a’,’v’,’a’,’6′};
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 Class Top
We saw above how we can write bytes to the console using methods from the 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 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 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*; // Import all file classes from 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;iDataOutputStream.
Next >>
Question 2 : What is the abstract superclass of byte input streams called?
– 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?
– 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?
– We can use the InputStream class via
<< Prev Next >>
Question 6 : Can we use more than one stream when outputting data to storage?
– 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 answer What'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
Java I/O Overview
Byte Stream Classes
write(bytes[]) Example
Writing Bytes To A File
Writing Binary Data To A File
Writing Objects To A File
read() Example
read(bytes[]) Example
Reading Bytes From A File
Reading Binary From A File
Reading Objects From A File
Byte Stream Classes Quiz
Character Stream Classes
Dates, Numbers & Currencies
Regular Expressions
Formatting & Tokenizing


Aly Chiman

Aly Chiman is a Blogger & Reporter at which covers a wide variety of topics from local news from digital world fashion and beauty . AlyChiTech covers the top notch content from the around the world covering a wide variety of topics. Aly is currently studying BS Mass Communication at University.