Lecture 8: I/O Streams types of I/O streams Chaining Streams

Slides:



Advertisements
Similar presentations
Introduction to Java 2 Programming Lecture 7 IO, Files, and URLs.
Advertisements

Lecture 15: I/O and Parsing
Exceptions. Definition Exception: something unexpected that can occur in the execution of a program e.g., divide by zero or attempt to open a file that.
The Package Statement Group related interfaces and classes together Purpose: encapsulation and reduces name conflicts –private package classes not visible.
MOD III. Input / Output Streams Byte streams Programs use byte streams to perform input and output of 8-bit bytes. This Stream handles the 8-bit.
Streams Dwight Deugo Nesa Matic Portions of the notes for this lecture include excerpts from.
COMP201 Java Programming Topic 5: Input and Output Reading: Chapter 12.
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.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 12  File Input and Output Stream Classes Text Input and Output.
Chapter 91 Streams and File I/O Chapter 9. 2 Announcements Project 5 due last night Project 6 assigned Exam 2 –Wed., March 21, 7:00 – 8:00 pm, LILY 1105.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Chapter 8: Exceptions and I/O Streams Copyright 2002, Matthew Evett. These slides are based on slides copyrighted by John Lewis and William Loftus, 2002,
1 Introduction to Console Input  Primitive Type Wrapper Classes  Converting Strings to Numbers  System.in Stream  Wrapping System.in in a Buffered.
CS102--Object Oriented Programming Lecture 14: – File I/O BufferedReader The File class Write to /read from Binary files Copyright © 2008 Xiaoyan Li.
Chapter 91 Streams and File I/O CS-180 Recitation-03/07/2008.
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 ->
COMP201 Java Programming Topic 6: Streams and Files Reading: Chapter 12.
Lecture 30 Streams and File I/O COMP1681 / SE15 Introduction to Programming.
Chapter 12 File Input and Output. Topics Stream Classes Files Text Input and Output JFileChooser for GUI programs Binary files.
Chapter 8: Exceptions and I/O Streams Copyright 2002, Matthew Evett. These slides are based on slides copyrighted by John Lewis and William Loftus, 2002,
Exceptions and IO Dr. Andrew Wallace PhD BEng(hons) EurIng
Java I/O Input: information brought to program from an external source
Streams and File I/O Chapter 14. I/O Overview I/O = Input/Output In this context it is input to and output from programs Input can be from keyboard or.
Input/Ouput and Exception Handling. 2 Exceptions  An exception is an object that describes an unusual or erroneous situation  Exceptions are thrown.
5-Oct-15 Air Force Institute of Technology Electrical and Computer Engineering Object-Oriented Programming Design Topic : Streams and Files Maj Joel Young.
Very Brief Introduction to Java I/O with Buffered Reader and Buffered Writer.
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,
JAVA I/O © EnhanceEdu, IIIT Hyderabad. Contents 3/29/2010EnhanceEdu, IIIT - H 2  Command Line I/O  File Class  Streams  Byte Streams [Low level and.
MIT AITI 2003 Lecture 15 Streams Input and Output data from/to other sources.
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.
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.
CIS Intro to JAVA Lecture Notes Set 6 2-June-05.
1 CISC 370: I/O Streams James Atlas June 24, 2008.
Fall 2002CS 150: Intro. to Computing1 Streams and File I/O (That is, Input/Output) OR How you read data from files and write data to files.
CS 116 OBJECT ORIENTED PROGRAMMING II LECTURE 11 GEORGE KOUTSOGIANNAKIS Copyright: 2015 / Illinois Institute of Technology/George Koutsogiannakis 1.
– Advanced Programming P ROGRAMMING IN Lecture 22 Input and Output System.
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.
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.
1 Putting Streams to use. 2 Stream Zoo C++ gives you istream, ostream, iostream, ifstream, ofstream, fstream, wistream, wifstream, istrsteam… (18) Java.
Simple Java I/O Part I General Principles. Streams All modern I/O is stream-based A stream is a connection to a source of data or to a destination for.
Java Input / Output l a modular approach to input/output: - different stream objects are connected/wrapped to handle I/O l a data stream object: a flow.
1 Input-Output A complex issue in programming language design. The interface to the outside world. –Differences must be accommodated as transparently as.
CS202 Java Object Oriented Programming Input and Output Chengyu Sun California State University, Los Angeles.
The Java IO System Different kinds of IO Different kinds of operations
Keerthi Nelaturu Url: site.uottawa.ca/~knela006
CSG2H3 Object Oriented Programming
Java Text I/O CS140 Dick Steflik. Reader Abstract super class for character based input Subclasses: – BufferedReader – CharArrayReader – FilterReader.
OO Design and Programming II I/O: Reading and Writing
Streams & File Input/Output (I/O)
I/O Streams A stream is a sequence of bytes that flows from a source to a destination In a program, we read information from an input stream and write.
I/O Basics.
I/O Streams- Basics Byte Streams and Character Streams
Streams and File I/O Chapter 14.
JAVA IO.
I/O Streams A stream is a sequence of bytes that flow from a source to a destination In a program, we read information from an input stream and write information.
OBJECT ORIENTED PROGRAMMING II LECTURE 20 GEORGE KOUTSOGIANNAKIS
I/O and Applet from Chapter 12
Files and Streams in Java
Web Design & Development Lecture 8
Java Basics Introduction to Streams.
I/O Exceptions & Working with Files
Chapter 8: Exceptions and I/O Streams
David Davenport Spring 2005
Presentation transcript:

Lecture 8: I/O Streams types of I/O streams Chaining Streams Keyboard class processing reading and writing text files object serialization

I/O Streams A stream is a sequence of bytes that flow from a source to a destination In a program, we read information from an input stream and write information to an output stream A program can manage multiple streams at a time The java.io package contains many classes that allow us to define various streams with specific characteristics

I/O Stream Categories The classes in the I/O package divide input and output streams into other categories An I/O stream is either a character stream, which deals with text data byte stream, which deal with byte data An I/O stream is also either a data stream, which acts as either a source or destination processing stream, which alters or manages information in the stream

Character Streams - Unicode

Byte Streams

Character Streams VS. Byte Streams A character stream manages Unicode characters, while a byte stream manages 8-bit bytes. All classes derived from Reader and Writer represent character streams, and all classes derived from InputStream and OutputStream represent byte streams.

Data Streams VS. Processing Streams A data stream is a stream that represents a particular source or destination stream, such as a string in memory or a file on disk. A processing stream (sometimes called a filtering stream) performs some sort of manipulation on the data in a stream. By combining data streams with processing streams, we can create an input or output stream that behaves exactly as we wish. The classes that represent data streams and processing streams are the same classes that represent character streams and byte streams. It is just another way to categories them.

Source/Sink Streams VS. Filter Streams

The IOException Class Many operations performed by I/O classes can potentially throw an IOException. The IOException class is the parent of several exception classes that represent problems when trying to perform I/O. An IOException is a checked exception.

Standard I/O There are three standard I/O streams: standard input – defined by System.in standard output – defined by System.out standard error – defined by System.err We use System.out when we execute println statements System.in is declared to be a generic InputStream reference, and therefore usually must be mapped to a more useful stream with specific characteristics

Chaining Streams FileInputStream fin = new FileInputStream(“employee.dat”);

Chaining Streams File f = new File(“employee.dat”) ; FileInputStream fin = new FileInputStream( f ) ;

Chaining Streams File f = new File(“employee.dat”) ; FileInputStream fin = new FileInputStream( f ) ; DataInputStream din = new DataInputStream ( fin ) ; double d = din.readDouble ( );

Chaining Streams DataInputStream din = new DataInputStream( new FileInputStream( new File(“employee.dat”))) ; double d = din.readDouble ( ) ;

Chaining Streams FileInputStream fin = new FileInputStream("employee.dat"); BufferedInputStream bis = new BufferedInputStream(fin); DataInputStream din = new DataInputStream(bis); double d = din.readDouble();

Chaining Streams DataInputStream din = new DataInputStream( new BufferedInputStream( new FileInputStream("employee.dat"))); double d = din.readDouble();

Chaining Streams PushbackInputStream pbis = new PushbackInputStream( new BufferedInputStream( new FileInputStream("employee.dat"))); byte b = pbis.read(); //gets next byte if (b != '<') pbis.unread(b); //double d = din.readDouble(); ** can't **

Chaining Streams DataInputStream dis = new DataInputStream( pbis = new PushbackInputStream( new BufferedInputStream( new FileInputStream("employee.dat")))); byte b = pbis.read(); //gets next byte if (b != '<') pbis.unread(b); double d = din.readDouble(); ** can do this **

The Keyboard Class Now we can examine the processing of the Keyboard class in detail The Keyboard class: declares a useful standard input stream handles exceptions that may be thrown parses input lines into separate values converts input stings into the expected type handles conversion problems

The Standard Input Stream The Keyboard class declares the following input stream: InputStreamReader isr = new InputStreamReader (System.in) BufferedReader stdin = new BufferedReader (isr); The InputStreamReader object converts the original byte stream into a character stream The BufferedReader object allows us to use the readLine method to get an entire line of input

Text Files Information can be read from and written to text files by declaring and using the correct I/O streams The FileReader class represents an input file containing character data See Inventory.java See InventoryItem.java The FileWriter class represents a text output file See TestData.java

Chaining Streams-Text FileWriter fw = new FileWriter("somefile.txt"); //equivalent to: OutputStreamWriter osw = new OutputStreamWriter( new FileOutputStream("somefile.txt"));

Chaining Streams-Text PrintWriter pw = new PrintWriter( new FileWriter("somefile.txt")); pw.println("This will get written to the file."); // use is similar to System.out.println // println adds correct eol character for target // by System.getProperty("line.separator");

Chaining Streams-Text PrintWriter pw = new PrintWriter( new FileOutputStream("somefile.txt")); //PrintWriter(OutputStream) automatically inserts an //OutputStreamWriter pw.println("This will get written to the file.");

Chaining Streams-Text // no analogous PrintReader BufferedReader br = new BufferedReader( new FileReader("somefile.txt"); String s; while ((s != br.readLine()) != null){ //do something } // readLine() returns null when no more input //FileReader automatically converts bytes to Unicode

Chaining Streams-Text // no automatic conversion like FileReader - must use //InputStreamReader BufferedReader br = new BufferedReader( new InputStreamReader(System.in)); BufferedReader br2 = new BufferedReader( new InputStreamReader(url.openStream())); String s; while ((s != br.readLine()) != null){ //do something } // readLine() returns null when no more input

Object Serialization Object serialization is the act of saving an object, and its current state, so that it can be used again in another program The idea that an object can “live” beyond the program that created it is called persistence Object serialization is accomplished using the classes ObjectOutputStream and ObjectInputStream Serialization takes into account any other objects that are referenced by an object being serialized, saving them too

Object class public class DataObject implements Serializable{ String message; public DataObject(){ message = null; } public void setMessage(String m){ message = m; public String getMessage(){ return message;

Chaining Streams – Object Serialization ObjectOutputStream oos = new ObjectOutputStream( new FileOutputStream("somefile.dat")); DataObject dataobject = new DataObject(); dataobject.setMessage("Hello"); oos.writeObject(dataobject); ObjectInputStream ois = new ObjectInputStream( new FileInputStream("somefile.dat"); DataObject inobject = (DataObject)ois.readObject();

String Tokenizer public void readData(BufferedReader in) throws IOException{ String s = in.readLine(); StringTokenizer t = new StringTokenizer(s, ","); String name = t.nextToken(); double salary = Double.parseDouble(t.nextToken()); int month = Integer.parseInt(t.nextToken()); . . . }

END Of Lecture 8