1 / 12

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 {

maya-dillon
Download Presentation

Using Processing Stream

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Using Processing Stream

  2. 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.

  3. 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”); } }

  4. hello year 2000 h(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)

  5. 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 ?

  6. 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!!!

  7. 2000 2001 2002 2003 2004 2005 218772016 808460338 808464672 842018866 540160048 857748016 808722482 808465677 • 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?

  8. 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.

  9. 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();

  10. 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) { } } }

  11. 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();

  12. 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) { } } }

More Related