Streams
What are Streams?
-
ordered sequence of bytes of undetermined length
-
objects that represent sources and destinations of data
-
different sources and destinations could be disk files, devices, other programs, and memory arrays
-
Input & Output Streams
-
File Streams
-
Byte Streams
-
Character Streams
-
Buffered Streams
-
Data Streams
-
Object Streams
Input & Output Streams
Superclasses:
InputStream
OutputStream
Read from and write to a file (File Streams):
FileInputStream
FileOutputStream
Read from a file
FileInputStream in = null;
try {
in = new FileInputStream("in_file.txt");
} finally {
if (in != null) {
in.close();
}
}
Write to a file
FileOutputStream out = null;
try {
out = new FileOutputStream("out_file.txt");
} finally {
if (out != null) {
out.close();
}
}
Read from source file and write to destination file
FileInputStream in = null;
FileOutputStream out = null;
try {
in = new FileInputStream("in_file.txt");
out = new FileOutputStream("out_file.txt");
int c;
while ((c = in.read()) != -1) { // read
out.write(c); // write
}
} catch (IOException io) {
// handle exception
} finally {
// close both streams
}
Closing a stream / 1
FileInputStream in = null;
try {
...
} finally {
if (in != null) {
try {
in.close();
} catch (IOException ioe) {
// handle exception
}
}
-
The "hard" way
Closing a stream / 2
try (FileInputStream in = new FileInputStream("in_file.txt")) {
in.read();
...
} catch (IOException ioe) {
// handle exception
}
-
Autoclosable and try with resources
* Available only in Java 7 and above
Byte & Character Streams
-
Unicode representation
-
Conversion when reading and writing
-
Character Streams do that automatically
-
Internationalization
Character Streams
Superclasses:
Reader
Writer
Read from and write to a text file:
FileReader
FileWriter
Read from a text file
try (FileReader in = new FileReader("in_file.txt")) {
char [] a = new char[50];
in.read(a); // reads the content to the array
} catch (IOException ioe) {
// handle exception
}
Write to a text file
try (FileWriter out = new FileWriter("out_file.txt")) {
out.write("This\n is\n an\n example\n");
out.flush();
} catch (IOException ioe) {
// handle exception
}
Buffered Streams
-
Read and write to the OS in large chunks for optimization
-
Output Streams buffer data into internal buffer until there is enough collected to write out a big chunk
-
Input Streams read a large chunk from disk, network, etc. into internal buffer and all reads are done from that buffer until the buffer is empty, and a new chunk is read in
Buffered Byte Streams
// input stream
BufferedInputStream bis =
new BufferedInputStream(
new FileInputStream("in_file.txt"));
// output stream
BufferedOutputStream bos =
new BufferedOutputStream(
new FileOutputStream("out_file.txt"));
Buffered Character Streams
// input stream
BufferedReader reader =
new BufferedReader(
new FileReader("in_file.txt"));
// output stream
BufferedWriter writer =
new BufferedWriter(
new FileWriter("out_file.txt"));
Other Streams
-
Formatting output
-
Default Streams
System.in
System.out
System.err
Console
System.console
PrintStream
PrintWriter
Reading with java.util.Scanner
try (Scanner inputScanner =
new Scanner(System.in);
Scanner fileScanner =
new Scanner(new FileReader(“f.txt"));
Scanner stringScanner =
new Scanner("someString")) {
while (inputScanner.hasNext()) {
String line = inputScanner.nextLine();
...
}
}
Data Streams
-
Support reading and writing of whole objects
-
Attributes - primitives or String
-
Developer takes care of order of reading and writing
-
Throw EOFException at the end
Example class
public class Person {
String name;
int age;
boolean isMale;
}
Person person = new Person();
Reading and writing with data streams
// writing
DataOutputStream dos =
new DataOutputStream(
new FileOutputStream("data.bin"));
dos.writeChars(person.getName());
dos.writeInt(person.getAge());
dos.writeBoolean(person.isMale());
// reading
DataInputStream dis =
new DataInputStream(
new FileInputStream("data.bin"));
person.setName(dis.readUTF());
person.setAge(dis.readInt());
person.setMale(dis.readBoolean());
Object Streams
-
inherit Data Streams
-
can write whole objects
-
use of Serializable as a marker interface
-
support hierarchies
Reading and writing with object streams
public class Person implements Serializable {
String name;
int age;
boolean isMale;
}
Person person = new Person();
// writing
ObjectOutputStream ous =
new ObjectOutputStream(
new FileOutputStream("data.bin"));
ous.writeObject(person);
//reading
ObjectInputStream ois =
new ObjectInputStream(
new FileInputStream("data.bin"));
person = (Person) ois.readObject();
Questions?!
Streams
By Tsvetan Dimitrov
Streams
- 132