Transcript document
Overview of Java I/O
Stream
• A Program often needs to read/write information from/to
outer source/destination.
• Outer source/destination can be :
– A File in a disk
– A Network connection(especially socket connction)
– Other memory location(array)
• Stream is a abstract connection between outer world and
your Java program. As the word “Stream” indicates, it has
a direction; incoming or outgoing.
• Through incoming stream a sequence of byte or character
is flowed into your program.
• Through outgoing stream a sequence of byte of character is
flushed out to outer destination.
consists of one or more streams
in java.io package
What Stream means to
Programmer?
Reading:
open a stream
while more information in the stream
read information
close the stream
Writing:
open a stream
while more information
write information to the stream
close the stream
Byte Oriented Stream
• For reading and writing bytes, Java provides two base
classes; InputStream and OutputStream.
InputStream
abstract int read()
int read( byte[] b)
int read(byte[] b, int off, int len)
int available()
void close(), and some more ...
OutputStream
abstract void write(int b)
void write(byte[] b)
void write(byte[], int off, int len)
void close();
void flush();
Character Oriented Stream
• For reading and writing characters, Java provides two base
classes; Reader and Writer.
Reader
abstract int read(char[] cbuf, int off, int len)
int read()
int read(char[] cbuf)
abstract void close(), and some more ...
Writer
abstract void write(char[] cbuf, int off, int len)
void write(int c)
void write(String str)
void write(String str, int off, int len)
abstract void close();
abstract void flush();
What are all those stream classes
for?
• See Figure 12-1 and 12-2 in page 628 and 629 of the text book
and you’ll find so many classes are derived from base class.
• Do not be intimidated by its size!!! No one expect you to
remember all the details.
• Your life will be much easier when you keep the following in
mind.
We can classify each class in java.io package into two
categories:
– sink stream: A stream which directrly interact with source/destination
– processing stream: A stream which performs some special processing on
top of a sink or another processing stream.
What’s Processing Streams for?
• Recall that InputStream/Reader defines the methods to
read series of bytes or characters only.
• Generally sink streams does not offer more methods than
its base classes. Then consider the followings.
–
–
–
–
What if you want to read integer data from stream?
What if you want to read an entire line at a time?
What if you want to open and read compressed file?
Etc.
• All these and many other features are made possible by
using a series of processing streams.
source
program
Sink Stream
Processing Streams
program
destination
Processing Streams Sink Stream
import java.io.*;
public class IOWrite {
public static void main(String args[]) {
try {
DataOutputStream out = new DataOutputStream(
new FileOutputStream(args[0]));
}catch (IOException ioe) {
System.out.println(“io error”);
}
out.writeInt(4);
out.writeFloat((float)3.4);
out.writeChar('h');
out.flush();
out.close();
}
}
import java.io.*;
public class IORead {
public void static main(String args[]) {
try {
DataInputStream in = new DataInputStream(
new FileInputStream(args[0]));
System.out.println("first integer " + in.readInt());
System.out.println("first float " + in.readFloat());
System.out.println("first char " + in.readChar());
} catch(FileNotFoundException fnf){
System.out.println("can't open ");
}
catch(IOException ioe) {
System.out.println("io error");
}
}
}