Java I/O classes – case study in an OO library Flexible and somewhat slick, but a bit of a mess (though improving)

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

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.
III. Streams. Introduction Often a program needs to bring in information from an external source or to send out information to an external destination.
Standard input, output and error. Lecture Under Construction.
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.
Java I/O and Java Networking (Client Side) Yoshi.
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.
Lecture 6 Java I/O Exceptions Basic Swing. Java I/O classes Flexible and somewhat slick, but a bit of a mess.
Algorithm Programming I/O via Java Streams Bar-Ilan University תשס"ח by Moshe Fresko.
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.
Slides prepared by Rose Williams, Binghamton University Chapter 10 File I/O.
CS102--Object Oriented Programming Lecture 14: – File I/O BufferedReader The File class Write to /read from Binary files Copyright © 2008 Xiaoyan Li.
Java I/O classes – case study in an OO library Flexible and somewhat slick, but a bit of a mess.
Chapter 20 – Streams and Binary Input/Output Big Java Early Objects by Cay Horstmann Copyright © 2014 by John Wiley & Sons. All rights reserved.
Java I/O Input: information brought to program from an external source
Java File I/O (Continued). File I/O in Java Like every other programming language, Java supports the writing to and reading from different files with.
CS203 Programming with Data Structures Input and Output California State University, Los Angeles.
Files and Streams. Java I/O File I/O I/O streams provide data input/output solutions to the programs. A stream can represent many different kinds of sources.
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.
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.
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.
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.
Object Persistence and Object serialization CSNB534 Asma Shakil.
Java Programming: Advanced Topics 1 Input/Output and Serialization Chapter 3.
Java Input/Output CSE301 University of Sunderland Harry Erwin, PhD Half Lecture.
The Java I/O Classes and Interfaces cont’d
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.
5-Dec-15 Sequential Files and Streams. 2 File Handling. File Concept.
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.
CS 116 OBJECT ORIENTED PROGRAMMING II LECTURE 11 GEORGE KOUTSOGIANNAKIS Copyright: 2015 / Illinois Institute of Technology/George Koutsogiannakis 1.
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.
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.
 Learn about computer files  Use the Path class  Learn about  Streams  Buffers  file organization  Use Java’s IO classes to write to and read from.
Exception Handling, Reading and Writing in Files, Serialization, Exceptions, Files, Streams, File Readers and Writers, Serializable SoftUni Team Technical.
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.
Spring 2008 Mark Fontenot CSE Honors Principles of Computer Science I Note Set 20.
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.
Lec.9 Midterm & Files (Chapter 10) Jiang (Jen) ZHENG June 20 th, 2005.
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.
CSC1351 Class 6 Classes & Inheritance.
Lecture 8: I/O Streams types of I/O streams Chaining Streams
CSG2H3 Object Oriented Programming
Exception Handling, Reading and Writing in Files, Serialization,
Object Writing in files
I/O Basics.
Chapter 17 Binary I/O 1.
File.
I/O Streams- Basics Byte Streams and Character Streams
File I/O & collection frame work
Programming in Java Files and I/O Streams
Java Programming Course
OBJECT ORIENTED PROGRAMMING II LECTURE 20 GEORGE KOUTSOGIANNAKIS
Web Design & Development Lecture 8
David Davenport Spring 2005
Presentation transcript:

Java I/O classes – case study in an OO library Flexible and somewhat slick, but a bit of a mess (though improving)

Java classes for doing i/o Includes file i/o, memory i/o, socket i/o, inter-process (pipes), etc. All stored in package java.io Excellent example of OO design –Very general and scaleable Unfortunately, also obfuscates simple tasks. How to proceed –Understand basic design –Create some libraries to do common tasks Goal is not to be exhaustive but rather understand how to do basic operations AND understand the OO structure of this library (ie a good case study).

InputStream/OutputStream Start by studying the java.io.InputStream and java.io.OutputStream APIjava.io.InputStream java.io.OutputStream These are base class for performing all binary byte- based i/o Note that these classes are abstract each with a single abstract method –abstract int read() –abstract void write(int) Concrete subclasses must provide implementation of read/write that can get/put a single byte to/from the relevant source

Concrete subclasses of InputStream/OutputStream Since InputStream/OutputStream are abstract, they cannot be used to create objects (of course, they can be used for typing). A very common non-abstract subclass is FileOutputStream/FileInputStream. These can be used in a simple way to do the most basic byte- based file io Other classes for byte-based i/o (sockets, pipes, memory, etc.) behave similarly. File is just the easiest to use to demonstrate the main concepts.

Example with FileInputStream /* class example DataInput1.java */ /* assumes each char is one byte – dangerous (why?) import java.io.FileInputStream; public class DataInput1{ public static void main(String[] args) throws Exception{ String file = args[0]; int input; FileInputStream fin = new FileInputStream(file); while ( (input = fin.read()) != -1){ System.out.print((char) input); }

Example with FileOutputStream /* class example DataOutput1.java */ /* assumes each char is a single byte */ import java.io.FileOutputStream; public class DataOutput1{ public static void main(String[] args) throws Exception{ String file = args[0]; String output = "Hello World"; FileOutputStream fout = new FileOutputStream(file); char[] outputAsChars = output.toCharArray(); for (int i = 0; i < outputAsChars.length; ++i) fout.write(outputAsChars[i]); fout.close(); }

import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; public class CopyBytes { public static void main(String[] args) throws IOException { FileInputStream in = null; FileOutputStream out = null; try { in = new FileInputStream("xanadu.txt"); out = new FileOutputStream("outagain.txt"); int c; while ((c = in.read()) != -1) { out.write(c); } } finally { if (in != null) { in.close(); } if (out != null) { out.close(); } Another Example Combining both

Reader/Writer Java maintains a second class hierarchy for performing higher-level character-based i/o. These automatically convert to native character stream –Often this is 8-byte based but not always –Handled for you by Reader/Writer The two base classes in this case are –java.io.Reader –java.io.Writer Very similar to InputStream/OutputStream there is a concrete set of classes FileReader and FileWriter

import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; public class CopyCharacters { public static void main(String[] args) throws IOException { FileReader inputStream = null; FileWriter outputStream = null; try { inputStream = new FileReader("xanadu.txt"); outputStream = new FileWriter("characteroutput.txt"); int c; while ((c = inputStream.read()) != -1) { outputStream.write(c); } //…. c now stores a 2-byte value

Better: can do String-based i/o /* example Writer1.java in course examples */ /* using a simple FileWriter for String-based i/o */ import java.io.FileWriter; public class Writer1{ public static void main(String[] args) throws Exception{ String file = args[0]; String output = "Hello World!"; FileWriter fw = new FileWriter(file); fw.write(output); fw.close(); }

Can do line-based i/o (see also java.util.Scanner) import java.io.BufferedReader; import java.io.InputStreamReader; public class Reader1{ public static void main(String[] args) throws Exception{ /* convert System.in, which is an InputStream, to a Reader by wrapping in InputStreamReader, then wrap everything in BufferedReader */ String input; BufferedReader bin = new BufferedReader (new InputStreamReader (System.in)); while ( (input = bin.readLine()) != null){ System.out.println("you typed " + input); }}} converts an InputStream to a Reader

Higher-level functionality FileInputStream/FileOuputStream and FileReader/FileWriter allow you to do pretty much any file i/o at a very low level. However, this is too low-level for Java. Java provides many more libraries to read/write higher-level constructs: –characters –Strings –native datatypes –arrays –arbitrary objects (serialization)

“Decorator” Pattern These capabilities are added using a design called the Decorator Pattern. InputStream/OutputStream instances are passed to a wrapper or decorator class that uses them and adds to their functionality. For example, floating point numbers can be read from a file by chaining together a FileInputStream and another class that assembles bytes into portable floating point.

Purpose of Decorator Best way to think of this is as follows: –There are two important issues when constructing an i/o library Where the i/o is going (file, etc). How the data is represented (String, native type, etc.) –Rather than create a class for each combination, Decorator classes allow you to mix and match, augment functionality of base classes. –This is a bit confusing but is very flexible. –Decotators can also add other capabilities, such as peek ahead, push back, write line number, etc.

Java i/o Decorator Classes All Java i/o decorator classes inherit from FilterInputStream and FilterOutputStream Look at the api for these classes and note a few things: –They wrap instances of InputStream/OutputStream respectively. –They inherit from InputStream/OutputStream respectively This is an odd inheritence hierarchy but is necessary to ensure that the FilterStreams support the same interface as the underlying class.

Filter Streams Easiest way to think of the filter streams as wrapping an underlying class which they augment the functionality of. Consider the respective constructors –FilterInputStream(InputStream in); –FilterOutputStream(OutputStream out); In each case, the FilterStreams use an underlying presumably simpler inputstream and augment its functionality.

Some FilterStream examples to clarify this Perhaps most common FilterInputStream is DataInputStream. Study the API and be sure you understand the inheritance hierarchy DataInputStream stores an InputStream and uses this to do higher-level i/o –readInt, readDouble, etc. DataOutputStream is analogous

Example of DataInputStream /* DataInputStream2 example in course examples */ import java.io.DataOutputStream; import java.io.FileOutputStream; public class DataOutput2{ public static void main(String[] args) throws Exception{ String file = args[0]; double[] data = {1.1,1.2,1.3,1.4,1.5}; DataOutputStream dout = new DataOutputStream (new FileOutputStream(file)); for (int i = 0; i < data.length; ++i){ dout.writeDouble(data[i]); } dout.close();}}

Example of DataInputStream /* DataOutput2 example in course examples */ import java.io.DataInputStream; import java.io.FileInputStream; import java.io.EOFException; public class DataInput2{ public static void main(String[] args) throws Exception{ String file = args[0]; DataInputStream din = new DataInputStream(new FileInputStream(file)); double data; try{ while (true){ data = din.readDouble(); System.out.println(data); } catch (EOFException eofe){} din.close();}}

Other Decorators Another common set of decorator classes is BufferedInputStream and BufferedOutputStream. Note that java.util.Scanner is a new and simpler alternative for a lot of high-level parsing tasks. These augment the functionality of the underlying stream by providing system buffering for higher-performance i/o They also add support for the mark method. Examples on next slide (notice how these classes can be multiply chained together in various ways.

BufferedInputStream Example import java.io.*; / public class DataInput3{ public static void main(String[] args) throws Exception{ String file = args[0]; DataInputStream din = new DataInputStream (new BufferedInputStream (new FileInputStream(file))); double data; /* need an exception to know when end of file is hit */ try{ while (true){ data = din.readDouble(); System.out.println(data); } catch (EOFException eofe){} din.close();}}

BufferedOutputStream example import java.io.BufferedOutputStream; import java.io.DataOutputStream; import java.io.FileOutputStream; public class DataOutput3{ public static void main(String[] args) throws Exception{ String file = args[0]; double[] data = {1.1,1.2,1.3,1.4,1.5}; DataOutputStream dout = new DataOutputStream (new BufferedOutputStream (new FileOutputStream(file))); for (int i = 0; i < data.length; ++i){ dout.writeDouble(data[i]); } dout.close();}}

Other output streams FileOutputStream is probably the most common. However, note that we could replace FileOutputStream with another Outputstream in these examples. In that case, the same decorated or undecorated data would be sent to some other device. Good example of this is thread communicatoin, memory i/o, and socket i/o (using Socket class). I strongly encourage you to familiarize yourself with these classes.

Serialization Objects can be written to streams also. This process is known as serialization. This is a huge convenience compared with having to marshal and unmarshal iv’s. But the issue is even deeper – how are methods represented, objects that contain objects as iv’s, etc. Java takes care of all of this with a very nice serialization interface.

Serialization classes Relevant classes –java.io.ObjectInputStream –java.io.ObjectOutputStream Note that these required an underlying Input/OutputStream to do their work. For a class to be serializable, it also must implement the Serializable interface (no methods). Finally, a class-scope variable can be declared as transient, meaning that it is ignored during serialization.

Serialization Example /* simple example of Serialization -- writing an object directly to an OutputStream without having to marshal and unmarshal */ import java.io.*; public class Serialization{ public static void main(String[] args) throws Exception{ String flag = args[0]; String file = args[1]; Currency c = new Currency("US Dollar", "USD“, 10, 5); Currency d; if (flag.equals("-w")){ ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(new File(file))); out.writeObject(c); } else if (flag.equals("-r")){ ObjectInputStream in = new ObjectInputStream(new FileInputStream(new File(file))); System.out.println("Reading serialized object"); d = (Currency) in.readObject(); }}}

Related Topics java.nio (“new io”) classes for file io are now standard as of J2SE. Will go over a bit final week, but aren’t in common use yet. java.io.File class –Very nice. Many methods for portably manipulating files java.io.Socket class –Provides Input/OutputStreams for communication across ports of different computers PrintWriter class (e.g. println method) Writing zip files, jar files, etc. java rmi: Remote Method Invocation: –DO’s on top of serialization

Suggested Readings Eckel’s detailed section on i/o Patterns in Java, A Catalog of Reusable Design Patterns Illustratred with UML, Mark Grand, Wiley Press. Design Patterns, Elements of Reusable Object-Oriented Software, Gamma et al.