You are on page 1of 23

Java files and streams

chapter 17
Java views each file as a sequential stream of
bytes
Byte-Based and Character-Based Streams
• File streams can be used to input and output data as bytes or
characters.
• Byte-based streams input and output data in its binary format.
Character-based streams input and output data as a sequence of
characters.
• . Files that are created using byte-based streams are referred to as
binary files, while files created using character-based streams are
referred to as text files. Text files can be read by text editors, while
binary files are read by programs that understand the file’s specific
content and its ordering
Standard Input, Standard Output and
Standard Error Streams
• A Java program opens a file by creating an object and associating a
stream of bytes or characters with it. The object’s constructor
interacts with the operating system to open the file.
• Java can also associate streams with different devices. When a Java
program begins executing, in fact, it creates three stream objects that
are associated with devices—System.in, System.out and System.err
• Each stream can be redirected. For System.in, this capability enables
the program to read bytes from a different source.
The java.io Package
• Java programs perform file processing by using classes from package
java.io. This package includes definitions for stream classes, such as
• FileInputStream (for byte-based input from a file), FileOutputStream
(for byte-based output to a file), FileReader (for character-based input
from a file) and FileWriter (for character-based output to a file), which
inherit from classes InputStream, OutputStream, Reader and Writer,
respectively.
• class File, which is useful for retrieving information about files or
directories from disk.
• An absolute path contains all the directories, starting with the root
directory, that lead to a specific file or directory. Every file or directory
on a particular disk drive has the same root directory in its path.
• A relative path normally starts from the directory in which the
application began executing and is therefore “relative” to the current
directory.
sequential-access files
• Java imposes no structure on a file—notions such as records do not
exist as part of the Java language. Therefore, you must structure files
to meet the requirements of your applications.
• Formatter output; // object used to output text to file
• output = new Formatter( "clients.txt" ); // open the file
• input = new Scanner( new File( "clients.txt" ) ); // read data
• scanner. .next() returns next token
object serialization: why?
• if the value "3" is read from a file, there’s no way to tell whether it
came from an int, a String or a double. We have only data, not type
information, on a disk.
• If the program that’s going to read this data “knows” what object type
the data corresponds to, then the data is simply read into objects of
that type.
• Sometimes we’ll not know exactly how the data is stored in a file. In
such cases, we want to read or write an entire object from a file.
object serialization
• In such cases, we want to read or write an entire object from a file.
Java provides such a mechanism, called object serialization.
• A so-called serialized object is an object 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.
Serialization is a mechanism of converting the state of an object into a byte
stream. Deserialization is the reverse process where the byte stream is used
to recreate the actual Java object in memory.
Advantages of Serialization
• 1. To save/persist state of an object.
• 2. To travel an object across a network.
• Persistence: Serialization allows objects to be stored to a persistent storage medium, such as a
file or database, and later deserialized and reloaded into memory.
• Communication: Serialization allows objects to be transmitted over a network, such as between
a client and a server, or between different components of a distributed system.
• Cloning: Serialization can be used to create deep copies of objects by serializing and then
deserializing them.
• Caching: Serialization can be used to cache frequently accessed or expensive-to-create objects
in memory or on disk for faster access.
• Overall, serialization and deserialization provide a flexible and powerful mechanism for
managing objects in Java, allowing them to be easily stored, transmitted, cloned, and cached as
needed.
• The byte stream created is platform independent. So, the object
serialized on one platform can be deserialized on a different platform.
To make a Java object serializable we implement the
java.io.Serializable interface. The ObjectOutputStream class contains
writeObject() method for serializing an Object.
• Serialization is the process of converting an object in memory into a
format that can be easily stored or transmitted over a network.
Deserialization is the reverse process of taking a serialized object and
creating an identical copy of it in memory.
Suppose you have a class called
Person that represents a
person's information, including
their name and age. Here's
what the class might look like
this.
Note that the Person class
implements the Serializable
interface to indicate that it can
be serialized.
To serialize an instance of this
class, you would first create an
output stream to a file, and
then pass that stream to a
ObjectOutputStream:
This code creates a Person
object with the name "John"
and age 30, and then writes it
to a file called "person.ser"
To deserialize the object, you
would read it from the file
using an input stream and a
ObjectInputStream.
This code reads the Person
object from the "person.ser"
file, and then prints its name
and age to the console. Note
that we cast the Object
returned by readObject() to a
Person object.
Serialization: wrap up
• To use serialization and deserialization in Java, you will need to follow
these basic steps:
• Make the class serializable: In order to serialize an object of a class,
that class must implement the java.io.Serializable interface.
• Create an ObjectOutputStream: To serialize an object, you need to
create an instance of the ObjectOutputStream class.
• Ex: ObjectOutputStream out = new ObjectOutputStream(new
FileOutputStream("data.txt"));
Serialization: wrap up
• Write the object to the stream: Once you have an instance of the
ObjectOutputStream class, you can use its writeObject() method to
serialize an object and write it to the output stream.
Person person = new Person("John", 30);
out.writeObject(person);
• Create an ObjectInputStream: To deserialize an object, you need to
create an instance of the ObjectInputStream class.
ObjectInputStream in = new ObjectInputStream(new
FileInputStream("data.txt"));
Serialization: wrap up
• Read the object from the stream: Once you have an instance of the
ObjectInputStream class, you can use its readObject() method to
deserialize an object from the input stream.
Person person = (Person) in.readObject();
Types of streams
• InputStream and OutputStream are abstract classes that declare methods for
performing byte-based input and output, respectively. We used various concrete
subclasses FileInputStream InputStream and OutputStream to manipulate files.
• 1.Pipe Streams: Pipes are synchronized communication channels between threads.
• 2. Filter Streams: A FilterInputStream filters an InputStream, and a
FilterOutputStream filters an OutputStream. Filtering means simply that the filter
stream provides additional functionality, such as aggregating data bytes into
meaningful primitive-type units.
• 3.A PrintStream (a subclass of FilterOutputStream) performs text output to the
specified stream. Actually, we’ve been using PrintStream output throughout the text
to this point—System.out and System.err are PrintStream objects.
• 4. Data Streams:
Buffered Streams
Buffering is an I/O-performance-enhancement technique. With a
BufferedOutputStream (a subclass of class FilterOutputStream), each
output statement does not necessarily result in an actual physical
transfer of data to the output device (which is a slow operation
compared to processor and main memory speeds).
Rather, each output operation is directed to a region in memory called
a buffer that’s large enough to hold the data of many output
operations. Then, actual transfer to the output device is performed in
one large physical output operation each time the buffer fills.
• With a BufferedOutputStream, a partially filled buffer can be forced
out to the device at any time by invoking the stream object’s flush
method.
• Using buffering can greatly increase the performance of an
application. Typical I/O operations are extremely slow compared with
the speed of accessing data in computer memory. Buffering reduces
the number of I/O operations by first combining smaller outputs
together in memory. The number of actual physical I/O operations is
small compared with the number of I/O requests issued by the
program. Thus, the program that’s using buffering is more efficient.
• Using buffering can greatly increase the performance of an
application. Typical I/O operations are extremely slow compared with
the speed of accessing data in computer memory.
• Buffering reduces the number of I/O operations by first combining
smaller outputs together in memory. The number of actual physical
I/O operations is small compared with the number of I/O requests
issued by the program. Thus, the program that’s using buffering is
more efficient.
• Memory-Based byte Array Streams: A ByteArrayInputStream (a
subclass of InputStream) reads from a byte array in memory. A
ByteArrayOutputStream (a subclass of OutputStream) outputs to a
byte array in memory. One use of byte-array I/O is data validation. For
example, data can be stored in a byte array, using the same formatting
that will be displayed at a later time, and the byte array can then be
output to a file to preserve the formatting.
• Sequencing Input from Multiple Streams: A SequenceInputStream (a
subclass of InputStream) logically concatenates several InputStreams
—the program sees the group as one continuous InputStream.

You might also like