320341 Programming in Java



Similar documents
What is an I/O Stream?

INPUT AND OUTPUT STREAMS

The Java I/O System. Binary I/O streams (ascii, 8 bits) The decorator design pattern Character I/O streams (Unicode, 16 bits)

D06 PROGRAMMING with JAVA

Chapter 20 Streams and Binary Input/Output. Big Java Early Objects by Cay Horstmann Copyright 2014 by John Wiley & Sons. All rights reserved.

JAVA - FILES AND I/O

Chapter 10. A stream is an object that enables the flow of data between a program and some I/O device or file. File I/O

CS 1302 Ch 19, Binary I/O

File I/O - Chapter 10. Many Stream Classes. Text Files vs Binary Files

READING DATA FROM KEYBOARD USING DATAINPUTSTREAM, BUFFEREDREADER AND SCANNER

AVRO - SERIALIZATION

WRITING DATA TO A BINARY FILE

Principles of Software Construction: Objects, Design, and Concurrency. Design Case Study: Stream I/O Some answers. Charlie Garrod Jonathan Aldrich

Stream Classes and File I/O

Input / Output Framework java.io Framework

Readings and References. Topic #10: Java Input / Output. "Streams" are the basic I/O objects. Input & Output. Streams. The stream model A-1.

Files and input/output streams

Today s Outline. Computer Communications. Java Communications uses Streams. Wrapping Streams. Stream Conventions 2/13/2016 CSE 132

public static void main(string[] args) { System.out.println("hello, world"); } }

Creating a Simple, Multithreaded Chat System with Java

Lesson: All About Sockets

Building a Multi-Threaded Web Server

Java Network Programming. The java.net package contains the Socket class. This class speaks TCP (connection-oriented protocol).

Event-Driven Programming

About the Tutorial. Audience. Prerequisites. Copyright & Disclaimer. AVRO Tutorial

Objectives. Streams and File I/O. Objectives, cont. Outline. I/O Overview. Streams

CS506 Web Design and Development Solved Online Quiz No. 01

Chulalongkorn University International School of Engineering Department of Computer Engineering Computer Programming Lab.

13 File Output and Input

CS 193A. Data files and storage

1 of 1 24/05/ :23 AM

Object-Oriented Programming in Java

java.util.scanner Here are some of the many features of Scanner objects. Some Features of java.util.scanner

Java Fundamental Classes Reference

Scanner. It takes input and splits it into a sequence of tokens. A token is a group of characters which form some unit.

Files and Streams. Writeappropriatecodetoread,writeandupdatefilesusingFileInputStream, FileOutputStream and RandomAccessFile objects.

Java Interview Questions and Answers

First Java Programs. V. Paúl Pauca. CSC 111D Fall, Department of Computer Science Wake Forest University. Introduction to Computer Science

Introduction to Java. Module 12: Networking (Java Sockets) Prepared by Costantinos Costa for EPL 233. ΕΠΛ233 Αντικειμενοστρεφής Προγραμματισμός 1

Serialization in Java (Binary and XML)

TP N 10 : Gestion des fichiers Langage JAVA

COSC 6397 Big Data Analytics. Distributed File Systems (II) Edgar Gabriel Spring HDFS Basics

CS 111 Classes I 1. Software Organization View to this point:

Chapter 2: Elements of Java

Java from a C perspective. Plan

Text file I/O and simple GUI dialogues

Learning Outcomes. Networking. Sockets. TCP/IP Networks. Hostnames and DNS TCP/IP

Application Development with TCP/IP. Brian S. Mitchell Drexel University

Question1-part2 What undesirable consequences might there be in having too long a DNS cache entry lifetime?

File class in Java. Scanner reminder. Files 10/19/2012. File Input and Output (Savitch, Chapter 10)

Lecture 7: Class design for security

Decorator Pattern [GoF]

Computer Programming I

IBM Tivoli Workload Scheduler Integration Workbench V8.6.: How to customize your automation environment by creating a custom Job Type plug-in

Introduction to Programming System Design. CSCI 455x (4 Units)

Division of Informatics, University of Edinburgh

AP Computer Science Java Subset

The Sun Certified Associate for the Java Platform, Standard Edition, Exam Version 1.0

Software Development in Java

Quick Introduction to Java

Lecture 5: Java Fundamentals III

CSE 1223: Introduction to Computer Programming in Java Chapter 7 File I/O

1.00 Lecture 1. Course information Course staff (TA, instructor names on syllabus/faq): 2 instructors, 4 TAs, 2 Lab TAs, graders

JDK 1.5 Updates for Introduction to Java Programming with SUN ONE Studio 4

C++ Wrapper Library for Firebird Embedded SQL

Illustration 1: Diagram of program function and data flow

Web Development in Java

Computing Concepts with Java Essentials

Network/Socket Programming in Java. Rajkumar Buyya

Introduction to Java

Continuous Integration Part 2

Java Network. Slides prepared by : Farzana Rahman

5 HDFS - Hadoop Distributed System

Storing Measurement Data

Using Files as Input/Output in Java 5.0 Applications

COSC 6397 Big Data Analytics. Mahout and 3 rd homework assignment. Edgar Gabriel Spring Mahout

Explain the relationship between a class and an object. Which is general and which is specific?

Advanced Java Client API

Course Intro Instructor Intro Java Intro, Continued

Word Count Code using MR2 Classes and API

Handout 1. Introduction to Java programming language. Java primitive types and operations. Reading keyboard Input using class Scanner.

ECE 122. Engineering Problem Solving with Java

Java CPD (I) Frans Coenen Department of Computer Science

Socket Programming in Java

Object-Oriented Design Lecture 4 CSU 370 Fall 2007 (Pucella) Tuesday, Sep 18, 2007

CS 106 Introduction to Computer Science I

Chapter 1 Java Program Design and Development

ResellerPlus - Bulk Http API Specification. (This Document gives details on how to send messages via the Bulk HTTP API for the RouteSms SMPP System)

core. Volume I - Fundamentals Seventh Edition Sun Microsystems Press A Prentice Hall Title ULB Darmstadt

HDFS - Java API coreservlets.com and Dima May coreservlets.com and Dima May

RARITAN VALLEY COMMUNITY COLLEGE ACADEMIC COURSE OUTLINE. CISY 105 Foundations of Computer Science

1. Writing Simple Classes:

Arrays. Atul Prakash Readings: Chapter 10, Downey Sun s Java tutorial on Arrays:

The Interface Concept

Transcription:

320341 Programming in Java Fall Semester 2014 Lecture 10: The Java I/O System Instructor: Slides: Jürgen Schönwälder Bendick Mahleko

Objectives This lecture introduces the following - Java Streams - Object Serialization in Java - File Management page 2

Introduction Storage of data in variables and collections is temporary - Data is lost when a local variables goes out of scope or when the program terminates - Computers use files for long term retention of large amounts of data - Data that is maintained in files is called persistent data - Computers store files on secondary storage devices such as hard disks page 3

Introduction Java I/O provides communication with devices - Example devices are files, console, networks, memory blocks etc. There are various types of communication - Examples include: sequential, random-access, binary, char, lines, words, objects,... Java provides a "mix and match" solution based on: - Byte-oriented I/O streams (ordered sequences of bytes) - Character-oriented I/O streams (ordered sequences of characters) page 4

Introduction Input stream - An object from which a sequence of bytes can be read () Output stream - An object to which a sequence of bytes can be written (OutputStream) Example - System streams System.in (out & err) are available to all Java programs - System.in is an instance of the Buffered class - System.out is an instance of PrintStream class I/O involves creating appropriate stream objects for your task page 5

Reading Bytes Streams read and write 8-bit values to/from various data sources: Examples of streams: Files, Network connections, Memory Blocks There are more than 60 different stream types Generality of Processing - Files, network connections, memory blocks, etc are handled in the same way page 6

Byte-Oriented vs Unicode-Oriented Streams that input and output bytes to files are called byte-oriented streams - The int value 5 would be stored using the binary format of 5: 00000000 00000000 00000000 00000101 - The numeric value can be used as an int in calculations - Files created using byte-oriented streams are called binary files - Binary files are read by a program that converts the data to a human-readable format page 7

Bytes-Oriented vs Unicode-Oriented Streams that input and output characters to files are called characteroriented (Unicode-oriented) streams - Ex. value 5 would be stored using the binary format of character 5 or 00000000 00110101 (character 5 in Unicode character set) - The character 5 is a character that can be used in a string - Files created using character-oriented streams are called text files - Text files can be read by text editors page 8

Bytes-Oriented vs Unicode-Oriented Byte-oriented streams versus Unicode-oriented characters - Byte-oriented streams are inconvenient for processing Unicode data - Classes inheriting from Reader and Writer abstract classes are used for processing Unicode data Byte-Oriented Streams Processing OutputStream Unicode-Character Oriented Processing Reader Writer The Java I/O system is based on these four classes - A zoo of classes inherit from the four abstract classes page 9

and OutputStream Classes The class has an abstract method abstract int read() throws IOException Returns one byte, or -1 if end of input source is encountered - The OutputStream class also has an abstract method abstract void write(int b) throws IOException Writes one byte to an output location page 10

Reader and Writer Classes Basic methods are similar to ones for the & OutputStream abstract int read() throws IOException Returns a Unicode code unit, or -1 if end of input source is encountered - The Writer class has an abstract method abstract void write(int b) throws IOException Writes one Unicode code unit to an output location page 11

Basic File Processing The typical pattern for processing a file is: 1. Open a file 2. Check if the file is opened 3. If the file is opened, read/write from/to the file 4. Close the file - Input & output streams have close() method (output also uses flush() ) Closing a File - Closing a file releases system resources - Closing a file also flushes the buffer to the output stream page 12

I/O Class Hierarchy Java I/O system is based on four abstract classes -, OutputStream, Reader, Writer Audio ByteArray File Filter Object Buffered Data File represents an input stream that is attached to a disk file page 13

I/O Class Hierarchy OutputStream ByteArray OutputStream File OutputStream Filter OutputStream Piped OutputStream Object OutputStream Buffered OutputStream PrintStream Data OutputStream FileOutputStream represents an output stream that is attached to a disk file page 14

Example 1 import java.io.file; import java.io.fileoutputstream; import java.io.ioexception; /** souce: The Java tutorial textbook, 5 th edition */ public class CopyBytes { public static void main(string [] argv) throws IOException { File in = null; FileOutputStream out = null; try { in = new File( 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(); } // end of finally } // end of main } // end of CopyBytes page 15

I/O Class Hierarchy Reader Hierarchy Reader Buffered Reader CharArray Reader Filter Reader Reader PipedReader StringReader FileReader page 16

I/O Class Hierarchy Writer Hierarchy Writer Buffered Writer CharArray Writer Filter Writer OutputStream Writer PipedWriter PrintWriter StringWriter FileWriter page 17

Example 2 import java.io.filereader; import java.io.filewriter; import java.io.ioexception; /** souce: The Java tutorial textbook, 5 th edition */ public class CopyCharacter { public static void main(string [] argv) 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); } finally { if (inputstream!= null) inputstream.close(); if (outputstream!= null) outputstream.close(); } // end of finally } // end of main } // end of CopyCharacter page 18

File Stream Processing File and FileOutputStream give I/O streams attached to a disk file Audio ByteArray File StringBuffer Object Give the filename or full path name of the file in a constructor - Use the constant string File.separator as a file separator File fin = new File("employee.dat"); - OR File f = new File("employee.dat"); File fin = new File(f); page 19

Buffered Streams Unbuffered I/O is inefficient because: Each read/write is handled directly by the operating system Each request triggers disk access, network activity etc which is expensive - Java implements buffered I/O streams to read data from a buffer With buffered I/O - The native API is called only when the buffer is full (writing) or the buffer is flushed or the buffered stream is closed - The native API is called only when the buffer is empty (reading) page 20

Example 3 import java.io.filereader; import java.io.filewriter; import java.io.ioexception; /** souce: The Java tutorial textbook, 5 th edition */ public class CopyBytesBuffered { public static void main(string [] argv) throws IOException { File in = null; FileOutputStream out = null; try { in = new Buffered new (File ( xanadu.txt ) ); out = new BufferedOutputStream ( new FileOutputStream ( Characteroutput.txt ) ); int c; while ( (c = in.read())!= -1) out.write(c); } finally { if (in!= null) in.close(); if (out!= null) out.close(); } // end of finally } // end of main } // end of CopyBytesBuffered page 21

Example 4 import java.io.filereader; import java.io.filewriter; import java.io.ioexception; /** souce: The Java tutorial textbook, 5 th edition */ public class CopyCharacterBuffered { public static void main(string [] argv) throws IOException { FileReader inputstream = null; FileWriter outputstream = null; try { inputstream = new BufferedReader new (FileReader( xanadu.txt ) ); outputstream = new BufferedWriter( new FileWriter( Characteroutput.txt ) ); int c; while ( (c = inputstream.read())!= -1) outputstream.write(c); } finally { if (inputstream!= null) inputstream.close(); if (outputstream!= null) outputstream.close(); } // end of finally } // end of main } // end of CopyCharacterBuffered page 22

Filtered Streams Java s IO package is built on the principal that - Each class should have a very focused responsibility (cohesion) - File interacts with files: its job is to get bytes, not to analyze them page 23

Filtered Streams - To read numbers, strings, objects etc., combine File with other classes whose responsibility is to group bytes or characters together - The combination is done by feeding an existing stream into the constructor of another to get the additional functionality - The combined streams are called filtered streams page 24

Filtered Streams We have seen examples in previous slides, here are more: - To be able to read numbers from a file, first create a File - Pass the File reference to the constructor of a Data File fin = new File("employee.dat"); Data din = new Data(fin); double s = din.readdouble(); New stream with more capable interface Data/ DataOutputStream - Has interface that allows to read/write all the basic Java types page 25

Filtered Streams Filtered Streams Cont Superclasses of several classes that add various functionality to process data Audio ByteArray File Filter Object Virtual input streams Buffered Data Decorator (Filter or Wrapper) design pattern OutputStream ByteArray OutputStream File OutputStream Filter OutputStream Piped OutputStream Object OutputStream Virtual output streams Buffered OutputStream PrintStream Data OutputStream page 26

Filtered Streams Filtered Streams Example - To support buffering and data input methods when reading files Data input methods Buffer stream Obtain stream Data din = new Data( new Buffered( new File("employee.dat"))); Reading numbers from compressed zip file Zip zin = new Zip( new File("employee.zip")); Data din = new Data(zin); page 27

Data Streams DataInput/ DataOutput interface - Data streams support binary I/O of primitive data values in Java - Data streams implement the DataInput and DataOutput interfaces - Data and DataOutputStream are the most widely used implementations of the DataInput and DataOutput interfaces respectively DataInput interface readdouble readshort readint readlong readfloat readboolean readchar DataOutput interface writedouble writeshort writeint writelong writefloat writeboolean writechar page 28

Text Streams We have so far looked at binary I/O - It is fast and efficient, but is not easily readable by humans - Humans can better comprehend text I/O Unicode - Java uses Unicode code units to represent texts - Local systems use their own encoding - Java provides stream filters that bridge the gap between local system and Unicode - The Unicode processing classes all inherit from abstract classes Reader and Writer classes page 29

Text Streams Example - An input reader that reads keystrokes from the console & converts to Unicode Reader in = new Reader(System.in); FileReader and FileWriter - Convenience classes for processing text strings from a file FileWriter out = new FileWriter("output.txt") - Is equivalent to FileWriter out = new FileWriter(new FileOutputStream("output.txt")); page 30

Text Streams The PrintWriter class - The class is used for text output - A print writer can print strings and numbers in text format - A print write must be combined with a destination writer page 31

Text Streams Writing to a PrintWriter - Use print and println used with System.out PrintWriter out = new PrintWriter(new FileWriter("employee.txt")); String name = "Harry Hacker"; double salary = 75000; out.print(name); out.print(' '); out.println(salary); Harry Hacker 75000 page 32

Text Streams Reading Text Input - Use the BufferedReader class and its readline method to read data - Use the Scanner class to read text data BufferedReader Example BufferedReader in = new BufferedReader(new FileReader("employee.txt")); String line; while ((line = in.readline())!= null) { do something with line } page 33

Use of Streams Writing Delimited Output - Delimited format imply each record is stored in a separate line - Instance fields are separated by delimiters #Here is a sample set of records (firstname lastname salary year month day): Harry Hacker 35500 1989 10 1 Carl Cracker 75000 1987 12 15 Tony Tester 38000 1990 3 15 page 34

Use of Streams Write records using PrintWriter class public void writedata(printwriter out) throws IOException { } GregorianCalendar calendar = new GregorianCalendar(); calendar.settime(hireday); out.println(name + " " + salary + " " + calendar.get(calendar.year) + " " + (calendar.get(calendar.month) + 1) + " " + calendar.get(calendar.day_of_month)); page 35

Example Reading Delimited Input - Read-in a line of text using the readline method of BufferedReader 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 y = Integer.parseInt(t.nextToken()); int m = Integer.parseInt(t.nextToken()); int d = Integer.parseInt(t.nextToken()); } GregorianCalendar calendar = new GregorianCalendar(y, m - 1, d); hireday = calendar.gettime(); page 36

Object Serialization Object serialization: - An object is represented as a sequence of bytes - The serialized representation includes the object s data as well as object type information and the types of data stored in the object A serialized object can be written to a file or send over a network Object deserialization: - After a serialized object has been written to a file, it can be read from the file and be deserialized - The type information and bytes that represent the object and its data can be used to recreate the object in memory page 37

Object Serialization Object - Enables an entire objects to be read from a stream (e.g., file) - Implements the ObjectInput interface which contains a method readobject - The method readobject reads and returns an Object from an - Use the File class to read from files page 38

Object Serialization ObjectOutputStream - Enables entire objects to be written to a stream (e.g., file) - Implements the ObjectOutput interface which contains a method writeobject - The method writeobject takes an Object as parameter and writes its information to an OutputStream - Use FileOutputStream to write serialized objects to files page 39

Object Serialization Serialization Process: 1. A class must implement the Serializable interface to be serialized 2. Open an ObjectOutputStream 3. Call the writeobject method of ObjectOutputStream to save the object page 40

Object Serialization Example: Saving an object // The class to be saved must implement the Serializable interface // The Serializable interface has no methods to be implemented class Employee implements Serializable { } // first open an ObjectOutputStream object ObjectOutputStream out = new ObjectOutputStream( new FileOutputStream( employee.dat )); //create the objects Employee harry = new Employee("Harry Hacker", 50000, 1989, 10, 1); Manager boss = new Manager("Carl Cracker", 80000, 1987, 12, 15); //save the objects by calling writeobject method out.writeobject(harry); out.writeobject(boss); page 41

Object Serialization To read the objects back 1. First get an Object object 2. Retrieve the objects in the order in which they were written Object in = new Object( new File("employee.dat")); Employee e1 = (Employee) in.readobject(); // Employee e2 = (Employee) in.readobject(); Note that saving a network of objects is a challenge see textbook, (Horstmann & Cornell, 2013, Core Java, Vol II, 9 th edition, Chapter 1) page 42

File Management The File class - A File can represent either a file or a directory - Example File constructors // associates name of file or directory to File object // name can contain path info absolute or relative public File (String name) // example File file = new File("test.txt"); public File (String pathtoname, String name) // locates directory // uses existing File object directory to locate file or directory public File (File directory, String name) public File (URI uri) // uses URI object to locate a file page 43

File Management Common File methods (see API for complete listing) Method boolean exists() boolean isfile() boolean isdirectory() String getabsolutepath() String getname() String getpath() String getparent() long length() String[] list() Description True if name specified as argument to File constructor is a file or directory; false otherwise True if name specified as argument to File constructor is a file;. false otherwise True if name specified as argument to File constructor is a directory;. false otherwise Returns absolute path of file or directory Returns name of file or directory Returns path of file or directory Returns parent directory of file or directory Returns length of file in bytes; 0 returned if object represents dir Returns array of strings representing the contents of a directory page 44

Reading Assignment - Core Java, Volume II, Chapter 1. Streams and Files by Horstmann and Cornell, 2013. page 45