Using Processing Stream
-
Upload
maya-dillon -
Category
Documents
-
view
21 -
download
2
description
Transcript of Using Processing Stream
Using Processing Stream
Predefined Streams
• System.in
InputStream used to read bytes from the keyboard
• System.out
PrintStream used to write bytes to the screen
• System.err
PrintStream used to report errors.
import java.io.*;
public class StdinRead {
public static void main(String args[]) {
int i=0; char c;
try {
do {
i = System.in.read();
c = (char)i;
System.out.println(c +”(“+i+”)”);
}while(i != -1);
} catch (IOException ioe) {
System.err.println(“IO Error”);
}
}
hello year 2000h(104)e(101)l(108)l(108)o(111) (32)y(121)e(101)a(97)r(114) (32)2(50)0(48)0(48)0(48)
(10)?(-1)
Reading int, float, double,...• System.in is an InputStream, with which we can only read
bytes.
• To read in int, float, …, you may want to put a processing stream at the end of stream pipe line.
• DataInput is an interface that defines all the methods for this purpose. – readBoolean(), readInt(), readDouble(), ….
• DataInputStream is a stream class that implements DataInput interface.
• Therefore, to read a java primitive data type from a InputStream, adding DataInputStream as a processing stream is one solution.
• However, does this work with System.in ?
import java.io.*;
public class StdinRead {
public static void main(String args[]) {
try {
DataInputStream din = new DataInputStream(System.in));
do {
int value = din.readInt();
System.out.println(value);
} while ( value != -1 )
} catch (IOException ioe) {
System.err.println(“IO Error”);
}
}
Wrong solution, Not what we expected!!!
2000 2001 2002 2003 2004 2005218772016808460338808464672842018866540160048857748016808722482808465677
• To read a java primitive data type from a InputStream, the stream should provide exact data type in its binary form.
• However, a keyboard input is not for typing in a java primitive data type in binary form.
• Then how do we read various primitive data types from the keyboard?
Read everyting as a String
• What we expect from keyboard inputs is a sequence of characters.
• However System.in is not a Reader stream, but an InputStream.
• Therefore, to read in various types of data from the keyboard, we need to take three steps.– Convert InputStream to Reader
– Read in each line as a String.
– Parse the String and convert it to appropriate type.
How to read a line from InputStream
• InputStreamReader is the bridge between InputStream and Reader.– InputStreamReader isr = new InputStreamReader(System.in);
• With a BufferedReader, we can read a line (as a String) from Reader stream.– BufferedReader breader = new BufferedReader(isr);
• Now we can read a String line from the keyboard.– String line = breader.readLine();
import java.io.*;
class LineReader{ BufferedReader breader;
LineReader(Reader input) throws IOException {breader = new BufferedReader(input);
} public String getLine() throws IOException {
return breader.readLine(); }
public static void main(String args[]) {try { String line=null; LineReader lreader =
new LineReader(new InputStreamReader(System.in));
while ( (line=lreader.getLine()) != null )System.out.println(line);
}catch (IOException ioe) {}
}}
StringTokenizer
• StringTokenizer class allows us to break a String into tokens.
• Without any given delimeter, the space will be used as a default delimeter.
• “Hello year 2000”, will be broken into
“Hello”, “year”, “2000”
• StringTokenizer class provides methods for retrieving and checking availability of tokens.– boolean hasMoreTokens();
– String nextToken();
import java.io.*;
public class InputParser {
public void static main(String args[]) {
try {
LineReader reader = new LineReader(new FileReader(args[0]));
String line = null;
while ( (line=reader.getLine()) != null ) {
StringTokenizer st = new StringTokenizer(line);
while ( st.hasMoreTokens() ) {
String token = st.nextToken();
// Parse a token
// int year = Integer.parseInt(token);
}
}
}catch (IOException e) { }
}
}