Stream Manipulation
Lecture 11
Streams and I/O
• basic classes for file IO
– FileInputStream, for reading from a file
– FileOutputStream, for writing to a file
• Example:
Open a file "[Link]" for reading
FileInputStream fis = new FileInputStream("[Link]");
Open a file "[Link]" for writing
FileOutputStream fos = new FileOutputStream ("[Link]");
2
Display File Contents
import [Link].*;
public class FileToOut1 {
public static void main(String args[]) {
try {
FileInputStream infile = new FileInputStream("[Link]");
byte buffer[] = new byte[50];
int nBytesRead;
do {
nBytesRead = [Link](buffer);
[Link](buffer, 0, nBytesRead);
} while (nBytesRead == [Link]);
}
catch (FileNotFoundException e) {
[Link]("File not found");
}
catch (IOException e) { [Link]("Read failed"); }
}
} 3
Filters
•Once a stream (e.g., file) has been opened, we
can attach filters
•Filters make reading/writing more efficient
•Most popular filters:
• For basic types:
•DataInputStream, DataOutputStream
• For objects:
•ObjectInputStream, ObjectOutputStream
4
Writing data to a file using Filters
import [Link].*;
public class GenerateData {
public static void main(String args[]) {
try {
FileOutputStream fos = new FileOutputStream("[Link]");
DataOutputStream dos = new DataOutputStream(fos);
[Link](2);
[Link](2.7182818284590451);
[Link](3.1415926535);
[Link](); [Link]();
}
catch (FileNotFoundException e) {
[Link]("File not found");
}
catch (IOException e) {
[Link]("Read or write failed");
}
}
} 5
Reading data from a file using filters
import [Link].*;
public class ReadData {
public static void main(String args[]) {
try {
FileInputStream fis = new FileInputStream("[Link]");
DataInputStream dis = new DataInputStream(fis);
int n = [Link]();
[Link](n);
for( int i = 0; i < n; i++ ) { [Link]([Link]());
}
[Link](); [Link]();
}
catch (FileNotFoundException e) {
[Link]("File not found");
}
catch (IOException e) { [Link]("Read or write failed");
}
}
} 6
Using DataInputStream
• In many applications, it may be required to read in an
entire line of text at a time.
• For this purpose, the DataInputStream class and its
readline method can be used.
• A readline() reads in a line of ASCII text and converts it
to a Unicode string
Example :
DataInputStream inp = new DataInputStream(new
FileInputStream(“[Link]”) );
.
.
.
String line = [Link](); 7
Object serialization
Serialization is the process of writing the state of
an object to a byte stream.
Write objects to a file, instead of writing primitive
types.
Use the ObjectInputStream, ObjectOutputStream
classes, the same way that filters are used.
8
Write an object to a file
import [Link].*;
import [Link].*;
public class WriteDate {
public WriteDate () {
Date d = new Date();
try {
FileOutputStream f = new FileOutputStream("[Link]");
ObjectOutputStream s = new ObjectOutputStream (f);
[Link] (d);
[Link] ();
}
catch (IOException e) { [Link](); }
}
public static void main (String args[]) {
new WriteDate ();
}
} 9
Read an object from a file
import [Link].*;
import [Link].*;
public class ReadDate {
public ReadDate () {
Date d = null;
ObjectInputStream s = null;
try { FileInputStream f = new FileInputStream ("[Link]");
s = new ObjectInputStream (f);
} catch (IOException e) { [Link](); }
try { d = (Date)[Link] (); }
catch (ClassNotFoundException e) { [Link](); }
catch (InvalidClassException e) { [Link](); }
catch (StreamCorruptedException e) { [Link](); }
catch (OptionalDataException e) { [Link](); }
catch (IOException e) { [Link](); }
[Link] ("Date serialized at: "+ d);
}
public static void main (String args[]) { new ReadDate (); }
} 10
Utility : StringTokenizer
• Parsing an input string.
• i.e. division of text into a set of discrete
parts or tokens, which can convey a
semantic meaning.
Utility : StringTokenizer
import [Link];
class STDemo
{
static String str = "title = Java : class;" +
"instructor = A.B.C.;" +
"time = 8.15 A.M.;" +
"Date = 30/03/2007";
public static void main(String args[])
{
StringTokenizer st = new StringTokenizer(str, "=;");
while([Link]())
{
String key = [Link]();
String val = [Link]();
[Link](key + "\t" + val);
}
}
}