Docsity

Java I/O Streams: Character and Byte Streams, File and Object Serialization, Slides of Java Programming

Partial preview of the text

Download Java I/O Streams: Character and Byte Streams, File and Object Serialization and more Slides Java Programming in PDF only on Docsity!

Standard input, output and error

Lecture Under Construction

Overview of I/O Streams

To bring in information, a program opens a

stream on an information source (a file, memory, a socket) and reads the information sequentially, as shown in the following figure.

Overview of I/O STREAMS Contd.

Similarly, a program can send information to an external destination by opening a stream to a destination and writing the information out sequentially, as shown in the following figure.

Character Streams

  • Reader and Writer are the abstract

superclasses for character streams in java.io.

  • Reader provides the API and partial

implementation for readers ( streams that read 16-bit characters ) and Writer provides the API and partial implementation for writers ( streams that write 16-bit characters).

Character Streams Contd.

  • The following figure shows the class

hierarchies for the Reader and Writer classes.

Byte Streams (cont.)

• These streams are typically used to read and

write binary data such as images and sounds.

  • Two of the byte stream classes,

ObjectInputStream and ObjectOutputStream, are used for object serialization.

Byte Streams (cont.)

  • The class hierarchy for the Reader Class

How to Use File Streams

The file streams-- FileReader , FileWriter , FileInputStream , and FileOutputStream -- read or write from a file on the native file system. Here is simple code to create a file reader File inputFile = new File("farrago.txt"); FileReader in = new FileReader(inputFile); FileWriter out = new FileWriter(outputFile); This reads characters from the reader as long as there's more input in the input file and writes those characters to the writer.

How to Use Pipe Streams

  • Pipes are used to channel the output from

one thread into the input of another. PipedReader and PipedWriter (and their input and output stream counterparts PipedInputStream and PipedOutputStream ) implement the input and output components of a pipe.

How to Concatenate Files

  • The SequenceInputStream creates a single input stream from multiple input sources. example code : ListOfFiles mylist = new ListOfFiles(args); SequenceInputStream s = new SequenceInputStream(mylist); Here, the mylist object is an enumeration that SequenceInputStream uses to get a new InputStream whenever it needs one.

Working with Filter Streams

  • The java.io package provides a set of abstract classes that define and partially implement filter streams. A filter stream filters data as it's being read from or written to the stream.
  • The filter streams are FilterInputStream , and FilterOutputStream.
  • A filter stream is constructed on another stream (the underlying stream).

Uses of Object Serialization

  • Remote Method Invocation (RMI)--

communication between objects via sockets

  • Lightweight persistence--the archival of an

object for use in a later invocation of the same program.

Working with Random Access Files

  • A random access file permits non-sequential or random access to a file's contents.
  • Using Random Access Files Unlike the input and output stream classes in java.io, RandomAccessFile is used for both reading and writing files. You create a RandomAccessFile object with different arguments depending on whether you intend to read or write.