40-244 – Advanced Programming P ROGRAMMING IN Lecture 22 Input and Output System.

Slides:



Advertisements
Similar presentations
A Guide to Advanced Java Faculty:Nguyen Ngoc Tu. 2 Operating System Application #1 Application #2 Java Virtual Machine #1 Local Memory Shared Memory Threads.
Advertisements

1 Streams and Input/Output Files Part I. 2 Introduction So far we have used variables and arrays for storing data inside the programs. This approach poses.
Jan Java I/O Yangjun Chen Dept. Business Computing University of Winnipeg.
Introduction to Java 2 Programming Lecture 7 IO, Files, and URLs.
Lecture 15: I/O and Parsing
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.
Decorator Pattern Applied to I/O stream classes. Design Principle Classes should be open for extension, but closed for modification –Apply the principle.
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.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L07 (Chapter 18) Binary I/O.
Algorithm Programming I/O via Java Streams Bar-Ilan University תשס"ח by Moshe Fresko.
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.
Exceptions and IO Dr. Andrew Wallace PhD BEng(hons) EurIng
Java I/O Input: information brought to program from an external source
Java Programming: I/O1 Java I/O Reference: java.sun.com/docs/books/tutorial/essential/io/
CS203 Programming with Data Structures Input and Output California State University, Los Angeles.
5-Oct-15 Air Force Institute of Technology Electrical and Computer Engineering Object-Oriented Programming Design Topic : Streams and Files Maj Joel Young.
Prepared by : A.Alzubair Hassan Kassala university Dept. Computer Science Lecture 2 I/O Streams 1.
Streams Reading: 2 nd Ed: , rd Ed: 11.1, 19.1, 19.4
Java How to Program, 8/e © by Pearson Education, Inc. All Rights Reserved.
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,
OOP with Java, David J. Barnes Input-Output1 A complex issue in programming language design. The interface to the outside world. –Differences must be accommodated.
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.
I / O in java. java.io BufferedInputStream BufferedOutputStream BufferedReader BufferedWriter ByteArrayInputStream ByteArrayOutputStream CharArrayReader.
MIT AITI 2003 Lecture 15 Streams Input and Output data from/to other sources.
Java Programming: Advanced Topics 1 Input/Output and Serialization Chapter 3.
Applications Development Input and Output in Java Topics covered: Some input-output capabilities of Java Example input code Java.
Java Input/Output CSE301 University of Sunderland Harry Erwin, PhD Half Lecture.
By Rachel Thompson and Michael Deck.  Java.io- a package for input and output  File I/O  Reads data into and out of the console  Writes and reads.
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.
Java Input/Output. Reading standard input Surprisingly complicated (GUI focus) Old-fashioned way: BufferedReader r = new BufferedReader(new InputStreamReader(System.in));
CIS 270—App Dev II Big Java Chapter 19 Files and Streams.
A stream is a sequence of data. A stream is a flowing sequence of characters.
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.
1 CISC 370: I/O Streams James Atlas June 24, 2008.
Copyright(c) Systems and Computer Engineering, Carleton Univeristy, * Object-Oriented Software Development Unit 13 I/O Stream Hierarchy Case.
Streams & Files. Java I/O Classes Saving data to / Loading data from files Two Choices: Binary-Formatted or Text-Formatted Data – int x = 1234; – Binary.
Lecture 14 Inheritance vs Composition. Inheritance vs Interface Use inheritance when two objects share a structure or code relation Use inheritance when.
Chapter - 11 Introduction to File and Streams This chapter includes -  Defining a File  Testing and Checking File Objects  Accessing File Objects.
Exception Handling, Reading and Writing in Files, Serialization, Exceptions, Files, Streams, File Readers and Writers, Serializable SoftUni Team Technical.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 13 Java Fundamentals File I/O Serializing an.
1 Putting Streams to use. 2 Stream Zoo C++ gives you istream, ostream, iostream, ifstream, ofstream, fstream, wistream, wifstream, istrsteam… (18) Java.
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.
Java Programming: Advanced Topics 1 Input/Output and Serialization.
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.
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.
Java IO Exploring the java.io package and living to talk about it.
Chapter 10: I/O Streams Input Streams Output Streams
java.io supports console and file I/O
The Java IO System Different kinds of IO Different kinds of operations
Lecture 8: I/O Streams types of I/O streams Chaining Streams
Java Text I/O CS140 Dick Steflik. Reader Abstract super class for character based input Subclasses: – BufferedReader – CharArrayReader – FilterReader.
Exception Handling, Reading and Writing in Files, Serialization,
Introduction to programming in java
CHAPTER 5 JAVA FILE INPUT/OUTPUT
I/O Basics.
Java’s Hierarchy Input/Output Classes and Their Purpose
JAVA IO.
Comp 212: Intermediate Programming Lecture 30 – Stream and File I/O
Stream Oriented I/O Computer Programming II Dr. Tim Margush
Files and Streams in Java
CS 240 – Advanced Programming Concepts
Streams and Readers The stream hierarchy is for reading bytes, the reader/writer hierarchy is for reading characters Unicode characters can be used internationally,
Comp 212: Intermediate Programming Lecture 30 – Stream and File I/O
Presentation transcript:

– Advanced Programming P ROGRAMMING IN Lecture 22 Input and Output System

What We Will Learn Using directory listings Using Streams Decorators Readers and Writers Tokenizers

Files and Directories File class in java.io package Represents name of a file or a set of files You cannot read or write data using File Used for Manipulating filenames Working with directories Checking file attributes

File Example Using File.list method Directory lister example from TIJ Chap. 11 DirList.java DirList3.java (using anonymous inner classes)

Input and Output Looking into input and output in an abstract way Input: Reading data from a source Source: anything that we can read data items from it Output: Writing data to a sink Sink: anything that we can write data items to it

Streams Abstract class representing a data source/sink InputStream: Represents a source Provides read() methods OutputStream: Represent a sink Provides write() methods

Kinds of Streams Streams are categorized according to type of source or sink they represent Files Arrays of bytes Strings Pipes Internet connections

InputStream Example Using echo: echo(System.in) echo(new FileInputStream("test.txt")) It works for any kind of input stream public void echo(InputStream in) throws IOException { int b; while ((b = in.read()) != -1) System.out.print((char)b); }

Input Stream Classes ByteArrayInputStream Arrays of bytes FileInputStream Files StringBufferInputStream Strings ObjectInputStream Serialized objects

Adding Features Adding various features to streams such as: Compression Line-numbering Buffering Push-back Combining these features with every stream causes inheritance tree explosion! CompressedFileInputStream BufferedFileInputStream CompressedBufferedFileInputStream,...

Filter Streams Wrap around other streams Adding features to them Without changing the interface Such as: BufferedInputStream LineNumberInputStream PushbackInputStream DataInputStream ZipInputStream

Filter Example We want to read from a file which is compressed with buffering mechanism with ability to pushback data into it We use one filter for each

Filter Example close read File I S close read Buffered I S close read Zip I S Pushback I S close read

Using Filters After construction, in can be used as an ordinary input stream Additional methods for pushback is available PushbackInputStream in = new PushbackInputStream( new ZipInputStream( new BufferedInputStream( new FileInputStream("in.dat")))); echo(in); in.pushback(89); in.close();

Decorator Pattern All filters are subclasses of FilterInputStream Which is a InputStream itself Takes an InputStream upon construction And filters data going through it The interface is not changed Since the filter is also an Input Stream This way to define classes to add features is called the Decorator pattern

Another Example A class for downloading URLs Downloader.java

Readers and Writers A separate hierarchy but pretty similar to streams Added from Java 1.1 to handle Unicode IO Reader is similar to InputStream Writer is similar to OutputStream Similar usage of filters

Readers Example Using echoLn: echoLn(new FileReader("in.txt")); public void echoLn(Reader in) throws IOException { LineNumberReader st = new LineNumberReader(in); String line; while ((line = st.readLine()) != null) System.out.println(st.getLineNumber() + ":" + line); }

Streams to Readers To read from a stream as a reader, use InputStreamReader OutputStreamWriter is used for output Reverse conversion is invalid Reader r = new InputStreamReader(System.in); echoLn(r);

Formatted Input Is not easy! One way is to read lines and then parse items Using tokenizers is another way StringTokenizer: breaking strings into tokens StreamTokenizer: reads from a stream or reader token by token

StreamTokenizer Can be used as a scanner Set of configurable items: whitespaces, punctuations, comment characters Can recognize ordinary tokens numbers quoted strings comments

Tokenizer Examples WordCount.java from TIJ Chapter 11 is a good example ComicsManager.java also uses StreamTokenizer to read the configuration file Note about comments