Using Processing Stream. Predefined Streams System.in InputStream used to read bytes from the keyboard System.out PrintStream used to write bytes to the.

Slides:



Advertisements
Similar presentations
1 Streams and Input/Output Files Part 3. 2 Handling Primitive Data Types The basic input and output streams provide read/write methods that can be used.
Advertisements

STRING AN EXAMPLE OF REFERENCE DATA TYPE. 2 Primitive Data Types  The eight Java primitive data types are:  byte  short  int  long  float  double.
Lecture 15: I/O and Parsing
Java File I/O. File I/O is important! Being able to write and read from files is necessary and is also one common practice of a programmer. Examples include.
CS-2852 Data Structures LECTURE 1B Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com.
10-1 Writing to a Text File When a text file is opened in this way, a FileNotFoundException can be thrown – In this context it actually means that the.
Geoff Holmes Overview IO Zoo Stream I/O File I/O Buffering Random-Access Text Streams Examples Serialization Java IO – programs that start with import.
1 LECTURE#7: Console Input Overview l Introduction to Wrapper classes. l Introduction to Exceptions (Java run-time errors). l Console input using the BufferedReader.
Lecture 3 Java Basics Lecture3.ppt.
String Tokenization What is String Tokenization?
1 Text File I/O Overview l I/O streams l Opening a text file for reading l Reading a text file l Closing a stream l Reading numbers from a text file l.
Programming in Java; Instructor:Alok Mehta Objects, Classes, Program Constructs1 Programming in Java Objects, Classes, Program Constructs.
1 Introduction to Console Input  Primitive Type Wrapper Classes  Converting Strings to Numbers  System.in Stream  Wrapping System.in in a Buffered.
Chapter 91 Streams and File I/O Chapter 9. 2 Reminders Project 6 released: due Nov 10:30 pm Project 4 regrades due by midnight tonight Discussion.
1 Streams Overview l I/O streams l Opening a text file for reading l Reading a text file l Closing a stream l Reading numbers from a text file l Writing.
CS102--Object Oriented Programming Lecture 14: – File I/O BufferedReader The File class Write to /read from Binary files Copyright © 2008 Xiaoyan Li.
Tirgul 1 Today’s subject - Java reminders and additions: –Inner classes –Packages –I/O streams –Command Line Arguments –Primitive and Reference Data Types.
Java I/O – what does it include? Command line user interface –Initial arguments to main program –System.in and System.out GUI Hardware –Disk drives ->
Reading Input -- the Keyboard Class The Keyboard class is NOT part of the Java standard class library The Keyboard class is in package cs1. It provides.
Java Programming: I/O1 Java I/O Reference: java.sun.com/docs/books/tutorial/essential/io/
Example 1 :- Handling integer values public class Program1 { public static void main(String [] args) { int value1, value2, sum; value1 = Integer.parseInt(args[0]);
1 Course Lectures Available on line:
SOFTWARE TECHNOLOGY - I CONSTANTS VARIABLES DATA TYPES.
5-Oct-15 Air Force Institute of Technology Electrical and Computer Engineering Object-Oriented Programming Design Topic : Streams and Files Maj Joel Young.
Input / Output Chapter 13.  We use files all the time  Programs are files  Documents are files  We want them to be “permanent”  To last beyond execution.
1 Java Console I/O Introduction. 2 Java I/O You may have noticed that all the I/O that we have done has been output The reasons –Java I/O is based on.
Basic Java Programming CSCI 392 Week Two. Stuff that is the same as C++ for loops and while loops for (int i=0; i
Streams Reading: 2 nd Ed: , rd Ed: 11.1, 19.1, 19.4
Chapter 9 1 Chapter 9 – Part 1 l Overview of Streams and File I/O l Text File I/O l Binary File I/O l File Objects and File Names Streams and File I/O.
Based on OOP with Java, by David J. Barnes Input-Output1 The java.io Package 4 Text files Reader and Writer classes 4 Byte stream files InputStream, FileInputStream,
I/O in Java Dennis Burford
Files Chap. 10 Streams, Readers, Writers 1. 2 Problem In our array example, we entered the students' names and scores from the keyboard. In many situations.
1 Week 12 l Overview of Streams and File I/O l Text File I/O Streams and File I/O.
File IO Basics By Dan Fleck Coming up: Data Streams.
Introduction to Programming G50PRO University of Nottingham Unit 11 : Files Input/Ouput Paul Tennent
Input/output Input in java is stream based.A stream represents sequence of bytes or characters. Stream provides an abstract view of I/O. Stream can be.
Two Ways to Store Data in a File  Text format  Binary format.
Recursive Problem 1 Write a java class named my_WordReverse using recursion. It takes a sentence and returns the sentence in reverse order. Note: Space.
© Amir Kirsh Files and Streams in Java Written by Amir Kirsh.
CS 11 java track: lecture 2 This week: more on object-oriented programming (OOP) objects vs. primitive types creating new objects with new calling methods.
Files and Streams CS /02/05 L7: Files Slide 2 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved.
1 Software 1 Java I/O. 2 The java.io package The java.io package provides: Classes for reading input Classes for writing output Classes for manipulating.
Lecture 5 I/O and Parsing
CS 116 OBJECT ORIENTED PROGRAMMING II LECTURE 11 GEORGE KOUTSOGIANNAKIS Copyright: 2015 / Illinois Institute of Technology/George Koutsogiannakis 1.
CSI 3125, Preliminaries, page 1 Java I/O. CSI 3125, Preliminaries, page 2 Java I/O Java I/O (Input and Output) is used to process the input and produce.
String line = console.readLine() StringTokenizer st = new StringTokenizer(line)
Chapter 15: Input and Output
I/O Basics 26 January Aside from print( ) and println( ), none of the I/O methods have been used significantly. The reason is simple: most real.
I/O Basics Java does provide strong, flexible support for I/O related to files and networks. Java’s console based interaction is limited since in real.
Java Input/Output. Java Input/output Input is any information that is needed by your program to complete its execution. Output is any information that.
Java Input and Output. Java Input  Input is any information needed by your program to complete its execution  So far we have been using InputBox for.
Chapter 9Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Announcements/Reminders l Project 6 due on Thursday March 31 (3 weeks)
Java I/O 1. Java I/O (Input and Output) is used to process the input and produce the output based on the input. The java.io package contains all the classes.
I/O in java and Revision. 2 I/O in Java Many packages and libraries associated with Java provide sophisticated ways to input and output information, e.g.:
CS202 Java Object Oriented Programming Input and Output Chengyu Sun California State University, Los Angeles.
Lecture 8: I/O Streams types of I/O streams Chaining Streams
Strings and File I/O.
Interactive Standard Input/output
Objects, Classes, Program Constructs
CHAPTER 5 JAVA FILE INPUT/OUTPUT
I/O Basics.
Computer Programming Methodology File Input
File.
I/O Streams- Basics Byte Streams and Character Streams
null, true, and false are also reserved.
Introduction to Java Programming
OBJECT ORIENTED PROGRAMMING II LECTURE 20 GEORGE KOUTSOGIANNAKIS
មជ្ឈមណ្ឌលកូរ៉េ សហ្វវែរ អេច អ ឌី
Exception Handling Contents
Presentation transcript:

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 2000 h(104) e(101) l(108) o(111) (32) y(121) e(101) a(97) r(114) (32) 2(50) 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!!!

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