Download Byte Streams

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Byte Streams
Programs use byte streams to perform input and output of 8-bit bytes. All byte stream classes
are descended from InputStream and OutputStream.
There are many byte stream classes. To demonstrate how byte streams work, we'll focus on the
file I/O byte streams, FileInputStream and FileOutputStream. Other kinds of byte
streams are used in much the same way; they differ mainly in the way they are constructed.
Using Byte Streams
We'll explore FileInputStream and FileOutputStream by examining an example program
named CopyBytes, which uses byte streams to copy xanadu.txt, one byte at a time.
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class CopyBytes {
public static void main(String[] args) throws IOException {
FileInputStream in = null;
FileOutputStream out = null;
try {
in = new FileInputStream("xanadu.txt");
out = new FileOutputStream("outagain.txt");
int c;
while ((c = in.read()) != -1) {
out.write(c);
}
} finally {
if (in != null) {
in.close();
}
if (out != null) {
out.close();
}
}
}
}
CopyBytes spends most of its time in a simple loop that reads the input stream and writes the
output stream, one byte at a time, as shown in the following figure.
Simple byte stream input and output.
Always Close Streams
Closing a stream when it's no longer needed is very important — so important
thatCopyBytes uses a finally block to guarantee that both streams will be closed even if an
error occurs. This practice helps avoid serious resource leaks.
One possible error is that CopyBytes was unable to open one or both files. When that
happens, the stream variable corresponding to the file never changes from its initial nullvalue.
That's why CopyBytes makes sure that each stream variable contains an object reference
before invoking close.
When Not to Use Byte Streams
CopyBytes seems like a normal program, but it actually represents a kind of low-level I/O that
you should avoid. Since xanadu.txt contains character data, the best approach is to
use character streams, as discussed in the next section. There are also streams for more
complicated data types. Byte streams should only be used for the most primitive I/O.
So why talk about byte streams? Because all other stream types are built on byte streams.
Using Character Streams
All character stream classes are descended from Reader and Writer. As with byte streams,
there are character stream classes that specialize in file I/O: FileReader andFileWriter.
The CopyCharacters example illustrates these classes.
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class CopyCharacters {
public static void main(String[] args) throws IOException {
FileReader inputStream = null;
FileWriter outputStream = null;
try {
inputStream = new FileReader("xanadu.txt");
outputStream = new FileWriter("characteroutput.txt");
int c;
while ((c = inputStream.read()) != -1) {
outputStream.write(c);
}
} finally {
if (inputStream != null) {
inputStream.close();
}
if (outputStream != null) {
outputStream.close();
}
}
}
}
CopyCharacters is very similar to CopyBytes. The most important difference is
thatCopyCharacters uses FileReader and FileWriter for input and output in place
ofFileInputStream and FileOutputStream. Notice that
both CopyBytes andCopyCharacters use an int variable to read to and write from.
However, inCopyCharacters, the int variable holds a character value in its last 16 bits;
inCopyBytes, the int variable holds a byte value in its last 8 bits.
Line-Oriented I/O
Character I/O usually occurs in bigger units than single characters. One common unit is the line:
a string of characters with a line terminator at the end. A line terminator can be a carriagereturn/line-feed sequence ("\r\n"), a single carriage-return ("\r"), or a single line-feed
("\n"). Supporting all possible line terminators allows programs to read text files created on
any of the widely used operating systems.
Let's modify the CopyCharacters example to use line-oriented I/O. To do this, we have to use
two classes we haven't seen before, BufferedReader and PrintWriter. We'll explore these
classes in greater depth in Buffered I/O and Formatting. Right now, we're just interested in their
support for line-oriented I/O.
The CopyLines example
invokes BufferedReader.readLine andPrintWriter.println to do input and output
one line at a time.
import
import
import
import
import
java.io.FileReader;
java.io.FileWriter;
java.io.BufferedReader;
java.io.PrintWriter;
java.io.IOException;
public class CopyLines {
public static void main(String[] args) throws IOException {
BufferedReader inputStream = null;
PrintWriter outputStream = null;
try {
inputStream = new BufferedReader(new FileReader("xanadu.txt"));
outputStream = new PrintWriter(new
FileWriter("characteroutput.txt"));
String l;
while ((l = inputStream.readLine()) != null) {
outputStream.println(l);
}
} finally {
if (inputStream != null) {
inputStream.close();
}
if (outputStream != null) {
outputStream.close();
}
}
}
}
Invoking readLine returns a line of text with the line. CopyLines outputs each line
usingprintln, which appends the line terminator for the current operating system. This might
not be the same line terminator that was used in the input file.
Buffered Streams
Most of the examples we've seen so far use unbuffered I/O. This means each read or write
request is handled directly by the underlying OS. This can make a program much less efficient,
since each such request often triggers disk access, network activity, or some other operation
that is relatively expensive.
To reduce this kind of overhead, the Java platform implements buffered I/O streams. Buffered
input streams read data from a memory area known as a buffer; the native input API is called
only when the buffer is empty. Similarly, buffered output streams write data to a buffer, and the
native output API is called only when the buffer is full.
A program can convert an unbuffered stream into a buffered stream using the wrapping idiom
we've used several times now, where the unbuffered stream object is passed to the constructor
for a buffered stream class. Here's how you might modify the constructor invocations in
the CopyCharacters example to use buffered I/O:
inputStream = new BufferedReader(new FileReader("xanadu.txt"));
outputStream = new BufferedWriter(new FileWriter("characteroutput.txt"));
There are four buffered stream classes used to wrap unbuffered
streams:BufferedInputStream and BufferedOutputStream create buffered byte
streams, while BufferedReader and BufferedWriter create buffered character streams.
Flushing Buffered Streams
It often makes sense to write out a buffer at critical points, without waiting for it to fill. This is
known as flushing the buffer.
Some buffered output classes support autoflush, specified by an optional constructor argument.
When autoflush is enabled, certain key events cause the buffer to be flushed. For example, an
autoflush PrintWriter object flushes the buffer on every invocation ofprintln or format.
See Formatting for more on these methods.
To flush a stream manually, invoke its flush method. The flush method is valid on any output
stream, but has no effect unless the stream is buffered.
object serialization
Java provides a mechanism, called object serialization where an object can be represented as a
sequence of bytes that includes the object's data as well as information about the object's type
and the types of data stored in the object.
After a serialized object has been written into a file, it can be read from the file and deserialized
that is, the type information and bytes that represent the object and its data can be used to
recreate the object in memory.
Most impressive is that the entire process is JVM independent, meaning an object can be
serialized on one platform and deserialized on an entirely different platform.
Classes ObjectInputStream and ObjectOutputStream are high-level streams that contain the
methods for serializing and deserializing an object.
The ObjectOutputStream class contains many write methods for writing various data types, but
one method in particular stands out:
public final void writeObject(Object x) throws IOException
The above method serializes an Object and sends it to the output stream. Similarly, the
ObjectInputStream class contains the following method for deserializing an object:
public final Object readObject() throws IOException,
ClassNotFoundException
This method retrieves the next Object out of the stream and deserializes it. The return value is
Object, so you will need to cast it to its appropriate data type.
To demonstrate how serialization works in Java, I am going to use the Employee class that we
discussed early on in the book. Suppose that we have the following Employee class, which
implements the Serializable interface:
public class Employee implements java.io.Serializable
{
public String name;
public String address;
public transient int SSN;
public int number;
public void mailCheck()
{
System.out.println("Mailing a check to " + name
+ " " + address);
}
}
Notice that for a class to be serialized successfully, two conditions must be met:

The class must implement the java.io.Serializable interface.

All of the fields in the class must be serializable. If a field is not serializable, it must be marked
transient.
If you are curious to know if a Java Standard Class is serializable or not, check the documentation
for the class. The test is simple: If the class implements java.io.Serializable, then it is serializable;
otherwise, it's not.
Serializing an Object:
The ObjectOutputStream class is used to serialize an Object. The following SerializeDemo
program instantiates an Employee object and serializes it to a file.
When the program is done executing, a file named employee.ser is created. The program does
not generate any output, but study the code and try to determine what the program is doing.
Note: When serializing an object to a file, the standard convention in Java is to give the file
a .serextension.
import java.io.*;
public class SerializeDemo
{
public static void main(String [] args)
{
Employee e = new Employee();
e.name = "Reyan Ali";
e.address = "Phokka Kuan, Ambehta Peer";
e.SSN = 11122333;
e.number = 101;
try
{
FileOutputStream fileOut =
new FileOutputStream("/tmp/employee.ser");
ObjectOutputStream out = new ObjectOutputStream(fileOut);
out.writeObject(e);
out.close();
fileOut.close();
System.out.printf("Serialized data is saved in /tmp/employee.ser");
}catch(IOException i)
{
i.printStackTrace();
}
}
}
Deserializing an Object:
The following DeserializeDemo program deserializes the Employee object created in the
SerializeDemo program. Study the program and try to determine its output:
import java.io.*;
public class DeserializeDemo
{
public static void main(String [] args)
{
Employee e = null;
try
{
FileInputStream fileIn = new FileInputStream("/tmp/employee.ser");
ObjectInputStream in = new ObjectInputStream(fileIn);
e = (Employee) in.readObject();
in.close();
fileIn.close();
}catch(IOException i)
{
i.printStackTrace();
return;
}catch(ClassNotFoundException c)
{
System.out.println("Employee class not found");
c.printStackTrace();
return;
}
System.out.println("Deserialized Employee...");
System.out.println("Name: " + e.name);
System.out.println("Address: " + e.address);
System.out.println("SSN: " + e.SSN);
System.out.println("Number: " + e.number);
}
}
This would produce the following result:
Deserialized Employee...
Name: Reyan Ali
Address:Phokka Kuan, Ambehta Peer
SSN: 0
Number:101
Here are following important points to be noted:

The try/catch block tries to catch a ClassNotFoundException, which is declared by the
readObject() method. For a JVM to be able to deserialize an object, it must be able to find the
bytecode for the class. If the JVM can't find a class during the deserialization of an object, it throws
a ClassNotFoundException.

Notice that the return value of readObject() is cast to an Employee reference.

The value of the SSN field was 11122333 when the object was serialized, but because the field
is transient, this value was not sent to the output stream. The SSN field of the deserialized
Employee object is 0.
Challenge
Write a Program That Copy the Contact List from one file (ContactSource.txt) file to another file
(ContactDestination.txt). The contact class should include contactName, contactPhone,
contactEmail.