- classes that your programs can use to read and to write data
- I/O Stream
- to input is to open a stream on an information source (a file, memory, a socket) and read the data (16 bits character or a byte)
- Character Input Streams (public abstract class Reader)
java.lang.Object
|
+--java.io.Reader
|
+--BufferedReader, CharArrayReader, FilterReader,
InputStreamReader, PipedReader, StringReader
- to output is to open a stream to a destination and write the data
- Character Output Streams (public abstract class Writer)
java.lang.Object
|
+--java.io.Writer
|
+--BufferedWriter, CharArrayWriter, FilterWriter,
OutputStreamWriter, PipedWriter, PrintWriter,
StringWriter
- to read and write 8-bit bytes, programs should use the byte streams which are subclass of the abstract InputStream and OutputStream class
- input examples
BufferedReader br;
String s;
br = new BufferedReader(new InputStreamReader(System.in));
s = br.readLine(); // read one line from console
br = new BufferedReader(new FileReader("filename"));
s = br.readLine(); // read one line from a file
br = new BufferedReader(new
StringReader("This is a string\nwith two lines.");
s = br.readLine(); // read one line from a string
// (the string resides in
// the StringReader object)
- output examples
PrintWriter pw;
String s = "A line of text.";
pw = new PrintWriter(System.out, true);
pw.println(s); // print one line to console
pw = new PrintWriter(new FileWriter("filename"));
pw.println(s); // print one line to a file
StringWriter sw;
pw = new PrintWriter(sw = new StringWriter());
pw.println(s); // print one line to a string
// (the string resides in
// the StringWriter object)
PrintWriter pw = new PrintWriter(sw);
pw.print("Hello");
pw.print(", there!");
pw.flush();
stdOut.println(sw.toString());
- "streams" metaphor is used in Java to represent flow of data from source to destination
- "same" method BufferedReader.readLine() will be used to read data
- just create different source object
- the console
br = new BufferedReader(new InputStreamReader(System.in));
- a local file
br = new BufferedReader(new FileReader("filename"));
- a remote file
URL address = new URL("http://personal.cityu.edu.hk/~dcywchan/2002ssd3/datafile.html");
URLConnection data = address.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(data.getInputStream()));
- a string in memory
br = new BufferedReader(new StringReader("This is a string\nwith two lines."));
or
String s = "Boss_John_Smith_800.0_\nCommissionWorker_Chan_Tai Man_500.0_4.0_160_\nPieceWorker_Teng_Lewis_2.5_270_\nHourlyWorker_Karen_Price_13.75_40_\nHourlyWorker_Luk_Price_13.75_46_";
br = new BufferedReader(new StringReader(s));
- we have used java.util.StringTokenizer in getting field from a line of data
- however, when the format of the input string is not known, it is very difficult to get the correct token from the the input
- java.io.StreamTokenizer
- Example: parsing and evaluating a Postfix Expression using StreamTokenizer and Stack
INFIX | POSTFIX | PREFIX
1+2 | 12+ | +12
a+b | ab+ | +ab
(a+b)*(c-d) | ab+cd-* | *+ab-cd
a-b/(c*d$e) | abcde$*/- | -a/b*c$de
| | | | |
---|
import java.applet.*;
import java.awt.event.*;
import java.awt.*;
import java.io.*;
import custom.component.CustomLabel;
public class EvaluatingPostfixExpressionFrame extends Frame
implements ActionListener,
WindowListener {
Stack s;
TextField inputTf,outputTf;
Dimension frameDim;
public EvaluatingPostfixExpressionFrame() {
super("EvaluatingPostfixExpressionApplet");
s = new Stack();
inputTf = new TextField(50);
outputTf = new TextField(50);
outputTf.setEditable(false);
setLayout(new FlowLayout());
add(new CustomLabel("Enter expression in Reverse Polish Notation"));
add(inputTf);
add(outputTf);
inputTf.addActionListener(this);
addWindowListener(this);
setSize(500,200);
setResizable(false);
/* Center the frame */
Dimension screenDim = Toolkit.getDefaultToolkit().getScreenSize();
frameDim = getSize();
setLocation((screenDim.width - frameDim.width) / 2,
(screenDim.height - frameDim.height) / 2);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
StreamTokenizer sTz;
int rhs;
sTz = new StreamTokenizer(new StringReader(inputTf.getText()));
sTz.ordinaryChar((int) '+');
sTz.ordinaryChar((int) '-');
sTz.ordinaryChar((int) '*');
sTz.ordinaryChar((int) '/');
try {
sTz.nextToken();
} catch (IOException ioe) {}
while (sTz.ttype != StreamTokenizer.TT_EOF) {
switch (sTz.ttype) {
case StreamTokenizer.TT_NUMBER: s.push((int) sTz.nval); break;
case (int) '+' : rhs = s.pop(); s.push(s.pop() + rhs);break;
case (int) '-' : rhs = s.pop(); s.push(s.pop() - rhs);break;
case (int) '*' : rhs = s.pop(); s.push(s.pop() * rhs);break;
case (int) '/' : rhs = s.pop(); s.push(s.pop() / rhs);break;
}
try {
sTz.nextToken();
} catch (IOException ioe) {}
}
outputTf.setText(s.pop()+"");
}
public void windowActivated(WindowEvent e) {}
public void windowClosed(WindowEvent e) {}
public void windowClosing(WindowEvent e) {
EvaluatingPostfixExpressionFrame.this.dispose();
}
public void windowDeactivated(WindowEvent e) {}
public void windowDeiconified(WindowEvent e) {}
public void windowIconified(WindowEvent e) {}
public void windowOpened(WindowEvent e) {}
}