Algorithm Programming 1 89-210 I/O via Java Streams Bar-Ilan University 2007-2008 תשס"ח by Moshe Fresko.

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 2. 2 Files and Exceptions When creating files and performing I/O operations on them, the systems generates errors.
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.
Streams Dwight Deugo Nesa Matic Portions of the notes for this lecture include excerpts from.
III. Streams. Introduction Often a program needs to bring in information from an external source or to send out information to an external destination.
Decorator Pattern Applied to I/O stream classes. Design Principle Classes should be open for extension, but closed for modification –Apply the principle.
02 - Structural Design Patterns – 1 Moshe Fresko Bar-Ilan University תשס"ח 2008.
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.
Streams and Files The objectives of this chapter are: To understand the principles of I/O streams and where to use them To understand the options and limitations.
Java I/O and Java Networking (Client Side) Yoshi.
Overview of Java I/O. Stream A Program often needs to read/write information from/to outer source/destination. Outer source/destination can be : –A File.
File I/O in Java CS 311, Winter File Basics Recall that a file is block structured. What does this mean? What happens when an application opens.
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/
5-Oct-15 Air Force Institute of Technology Electrical and Computer Engineering Object-Oriented Programming Design Topic : Streams and Files Maj Joel Young.
Input and Output F Stream Classes F Processing External Files F Data Streams F Print Streams F Buffered Streams F Text Input and Output on the Console.
Richiami di Java Input/Output. import java.io.*; public class Simple { public static void main(String a[]){ new Simple(); } public Simple() { byte buffer[]=new.
Copyright © Curt Hill Java I/O Flexibility and Layering.
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.
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.
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.
L5: Input & Output COMP206, Geoff Holmes and Bernhard Pfahringer.
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));
Introduction to Computation and Problem Solving Class 29: Introduction to Streams Prof. Steven R. Lerman and Dr. V. Judson Harward.
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.
Java IO. Why IO ? Without I/O, your program is a closed box. Without I/O, your program is a closed box. I/O gives your Java program access to your hard.
Copyright(c) Systems and Computer Engineering, Carleton Univeristy, * Object-Oriented Software Development Unit 13 I/O Stream Hierarchy Case.
– 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.
Lecture 14 Inheritance vs Composition. Inheritance vs Interface Use inheritance when two objects share a structure or code relation Use inheritance when.
Chapter 15: Input and Output
Company Input/Output Stream –. Input/Output Stream Data Program Input Stream Output Stream.
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.
Session 22 java.io Package.
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.
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.
1 Putting Streams to use. 2 Stream Zoo C++ gives you istream, ostream, iostream, ifstream, ofstream, fstream, wistream, wifstream, istrsteam… (18) Java.
Java Programming: Advanced Topics 1 Input/Output and Serialization.
Java Programming Language (3) - Input/Output Stream –
CS 210 Introduction to Design Patterns September 14 th, 2006.
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
The Java IO System Different kinds of IO Different kinds of operations
CSC1351 Class 6 Classes & Inheritance.
Fundamental of Java Programming
Sequential files creation & writing
IO in java.
Exception Handling, Reading and Writing in Files, Serialization,
Chapter 8: Input and Output
Object Writing in files
12: The Java I/O System stream model.
8/30/2018 CPRG 215 Introduction to Object-Oriented Programming with Java Module 5- The java.io Package Topic 5.1 Input and Output Streams, Readers.
I/O Basics.
File.
Programming in Java Files and I/O Streams
Објектно орјентисано програмирање
Методика наставе рачунарства
תרגול מס' 5: IO (קלט-פלט) זרמי קלט וזרמי פלט ((Input & Output Streams,
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,
Presentation transcript:

Algorithm Programming I/O via Java Streams Bar-Ilan University תשס"ח by Moshe Fresko

Java I/O Streams  Java I/O uses abstraction of “ streaming ” concept Stream : Abstract Representation of an Input/Output device, that is source of, or destination of data.  Input stream : To read data from.  Output stream : To write data to.  In library java.io.* we have two sets of I/O classes Byte Oriented classes  The Abstract Classes are: InputStream OutputStream Char Oriented classes (16 bit Unicode)  The Abstract Classes are: Reader Writer

Defining a File  java.io.File class : An abstract representation of file or directory pathnames. File(String pathname) … String getAbsolutePath() String getPath() String getName() … boolean exists() boolean canRead() boolean canWrite() … boolean createNewFile() boolean mkdir() boolean delete() etc.

Java Byte Oriented I/O  Byte based Abstract I/O Classes java.io.InputStream  InputStream()  int read() throws IOException  int read(byte[] dest) throws IOException  int read(byte[] dest, int offset, int length) throws IOException  void close() throws IOException  int available() throws IOException  long skip(long nbytes) throws IOException  boolean markSupported()  void mark(int readlimit)  void reset() throws IOException java.io.OutputStream  OutputStream()  void write(int b) throws IOException  void write(byte[] b) throws IOException  void write(byte[] b, int offset, int length) throws IOException  void flush() throws IOException  void close() throws IOException

DECORATOR pattern  We want to define an abstract Food class (or a Food Interface) to represent any food that can be sold in a Pizza shop. Let ’ s look to the following design … interface Food { float getPrice() ; String getDescription() ; } class Pizza implements Food { float getPrice() { return 50 ; } String getDescription() { return “Regular Pizza” ; } } class PizzaWithOnion extends Pizza // Adds 5 shekels { float getPrice() { return super.getPrice()+5 ; } String getDescription() { return super.getDescription() + “ with Onion” ; } }

DECORATOR pattern class PizzaWithCorn extends Pizza {// Adds 7 shekels … } class PizzaWithOnionAndCorn extends Pizza {// Adds 12 shekels … } // Etc. … // In the program Food f = new PizzaWithOnionAndCorn() ; … System.out.println(“You bought : “+f.getDescription()) ; System.out.println(“Price in Shekels is : “+f.getPrice()) ; …

DECORATOR pattern  Problems: 1. Many classes have to be defined … (For all the combinations) 2. What if Corn ’ s price will rise from 7 shekels to 8 shekel ? 3. What if we have another addition, let ’ s say “ Tuna Fish ” ?

DECORATOR pattern  Alternative design with “ Decorator ” pattern interface Food { float getPrice() ; String getDescription() ; } class Pizza implements Food { float getPrice() { return 50 ; } String getDescription() { return “Regular Pizza” ; } } class Addition implements Food { Food f ; Addition(Food f) { this.f = f ; } float getPrice() { return f.getPrice() ; } String getDescription() { return f.getDescription() ; } }

DECORATOR pattern class PlusOnion extends Addition { PlusOnion(Food f) { super(f) ; } float getPrice() { return super.getPrice() + 5 ; } String getDescription() { return super.getDescription() + “, with Onion”); } } class PlusCorn extends Addition { PlusCorn(Food f) { super(f) ; } float getPrice() { return super.getPrice() + 7 ; } String getDescription() { return super.getDescription() + “, with Corn”); } } // Etc. … // In the program Food f = new PlusOnion(new PlusCorn(new Pizza())) ; … System.out.println(“You bought : “+f.getDescription()) ; System.out.println(“Price in Shekels is : “+f.getPrice()) ;

DECORATOR pattern  The use of layered objects to dynamically and transparently add responsibilities to individual objects is referred to as the “ decorator ” pattern.  Some properties 1. Used when sub-classing creates too many (and inflexible) classes More flexible then static inheritance 2. All decorators that wrap around the original object must have the same basic interface 3. Changing the skin (vs. changing functionality internals like in Strategy) 4. Component classes must be lightweight 5. We can omit abstract decorator if we have only one decorator 6. Tradeoff: a. Coding is more complicated when using decorators b. A decorator and its component aren ’ t identical c. Lots of little objects 7. Decorator classes can add functionality

Decorator

Java Byte Oriented I/O classes  “ InputStream ” classes FileInputStream : Reads from a file ByteArrayInputStream : Reads from a byte-array StringBufferInputStream : Reads from a String PipedInputStream : Reads from another output pipe SequenceInputStream : Reads from two or more “ InputStreams ” sequentially FilterInputStream : As a “ DECORATOR ” abstract class  DataInputStream : To Read Primitive Data Types. readInt(), readByte()  BufferedInputStream : Adds buffering  LineNumberInputStream : Counts the number of lines  PushbackInputStream : Can push-back a character  “ OutputStream ” classes FileOutputStream ByteArrayOutputStream PipedOutputStream FilterOutputStream  DataOutputStream  PrintStream  BufferedOutputStream

Input Stream Definition // As it is defined in Java public abstract class InputStream { //... public abstract int read() throws IOException; public int read(byte b[]) throws IOException { return read(b, 0, b.length); } public int read(byte b[], int off, int len) throws IOException { //... } //... public void close() throws IOException { } //... }

Filter Input Stream Definition // As it is defined in Java public class FilterInputStream extends InputStream { protected InputStream in; protected FilterInputStream(InputStream in) { this.in = in; } public int read() throws IOException { return in.read(); } public int read(byte b[]) throws IOException { return read(b, 0, b.length); } public int read(byte b[], int off, int len) throws IOException { return in.read(b, off, len); } public void close() throws IOException { in.close(); } //... }

I/O Examples import java.io.* ; public class FileCopy { // Copies one file to another public static void main(String[] args) throws IOException { if (args.length<2) { System.out.println("arguments: SourceFile DestFile") ; System.exit(1) ; } InputStream in=new BufferedInputStream( new FileInputStream(args[0])); OutputStream out=new BufferedOutputStream( new FileOutputStream(args[1])); int next ; while((next=in.read())>=0) out.write(next) ; in.close() ; out.close() ; }

Char based I/O Classes  Unicode Char based Abstract I/O Classes java.io.Reader  Reader()  Reader(Object lock)  int read() throws IOException  int read(char[] dest) throws IOException  int read(char[] dest, int offset, int length) throws IOException  void close() throws IOException  long skip(long nchars) throws IOException  boolean markSupported()  void mark(int readlimit)  void reset() throws IOException  void ready() throws IOException java.io.Writer  Writer()  Writer(Object lock)  void write(int c) throws IOException  void write(char[] b) throws IOException  void write(char[] c, int offset, int length) throws IOException  void write(String s) throws IOException  void write(byte[] c, int offset, int length) throws IOException  void flush() throws IOException  void close() throws IOException

I/O Examples import java.io.* ; class DoubleTheNumber { public static void main(String args[]) throws IOException { BufferedReader br = new BufferedReader( new InputStreamReader(System.in)); System.out.print("Enter a number:") ; String s = br.readLine() ; int i=Integer.parseInt(s) ; i*=2 ; System.out.println("Twice this much is:"+i) ; }