Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
130 views

JavaClass Lecture11 StreamManipulation

The document discusses stream manipulation in Java, including basic classes for file input/output like FileInputStream and FileOutputStream. It describes how to attach filters to streams to make reading and writing more efficient, such as using DataInputStream and DataOutputStream. The document also covers writing and reading objects to files using object serialization with ObjectInputStream and ObjectOutputStream.

Uploaded by

api-3751900
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
130 views

JavaClass Lecture11 StreamManipulation

The document discusses stream manipulation in Java, including basic classes for file input/output like FileInputStream and FileOutputStream. It describes how to attach filters to streams to make reading and writing more efficient, such as using DataInputStream and DataOutputStream. The document also covers writing and reading objects to files using object serialization with ObjectInputStream and ObjectOutputStream.

Uploaded by

api-3751900
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

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 "myfile.txt" for reading
FileInputStream fis = new FileInputStream("myfile.txt");

Open a file "outfile.txt" for writing


FileOutputStream fos = new FileOutputStream ("myfile.txt");

2
Display File Contents
import java.io.*;
public class FileToOut1 {
public static void main(String args[]) {
try {
FileInputStream infile = new FileInputStream("testfile.txt");
byte buffer[] = new byte[50];
int nBytesRead;
do {
nBytesRead = infile.read(buffer);
System.out.write(buffer, 0, nBytesRead);
} while (nBytesRead == buffer.length);
}
catch (FileNotFoundException e) {
System.err.println("File not found");
}
catch (IOException e) { System.err.println("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 java.io.*;
public class GenerateData {
public static void main(String args[]) {
try {
FileOutputStream fos = new FileOutputStream("stuff.dat");
DataOutputStream dos = new DataOutputStream(fos);
dos.writeInt(2);
dos.writeDouble(2.7182818284590451);
dos.writeDouble(3.1415926535);
dos.close(); fos.close();
}
catch (FileNotFoundException e) {
System.err.println("File not found");
}
catch (IOException e) {
System.err.println("Read or write failed");
}
}
} 5
Reading data from a file using filters
import java.io.*;
public class ReadData {
public static void main(String args[]) {
try {
FileInputStream fis = new FileInputStream("stuff.dat");
DataInputStream dis = new DataInputStream(fis);
int n = dis.readInt();
System.out.println(n);
for( int i = 0; i < n; i++ ) { System.out.println(dis.readDouble());
}
dis.close(); fis.close();
}
catch (FileNotFoundException e) {
System.err.println("File not found");
}
catch (IOException e) { System.err.println("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(“Student.dat”) );
.
.
.
String line = inp.readline(); 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 java.io.*;
import java.util.*;
public class WriteDate {
public WriteDate () {
Date d = new Date();
try {
FileOutputStream f = new FileOutputStream("date.ser");
ObjectOutputStream s = new ObjectOutputStream (f);
s.writeObject (d);
s.close ();
}
catch (IOException e) { e.printStackTrace(); }
}
public static void main (String args[]) {
new WriteDate ();
}
} 9
Read an object from a file
import java.io.*;
import java.util.*;
public class ReadDate {
public ReadDate () {
Date d = null;
ObjectInputStream s = null;
try { FileInputStream f = new FileInputStream ("date.ser");
s = new ObjectInputStream (f);
} catch (IOException e) { e.printStackTrace(); }
try { d = (Date)s.readObject (); }
catch (ClassNotFoundException e) { e.printStackTrace(); }
catch (InvalidClassException e) { e.printStackTrace(); }
catch (StreamCorruptedException e) { e.printStackTrace(); }
catch (OptionalDataException e) { e.printStackTrace(); }
catch (IOException e) { e.printStackTrace(); }
System.out.println ("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 java.util.StringTokenizer;
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(st.hasMoreTokens())
{
String key = st.nextToken();
String val = st.nextToken();
System.out.println(key + "\t" + val);
}
}
}

You might also like