Java Programming: Advanced Topics 1 Input/Output and Serialization.

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

I/O Basics 12 January 2014Smitha N. Pai, CSE Dept.1.
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.
Lecture 15: I/O and Parsing
Streams Dwight Deugo Nesa Matic Portions of the notes for this lecture include excerpts from.
Slide 10.1 Advanced Programming 2004, based on LY Stefanus’s Slides Object Serialization Object serialization: the process of converting an object into.
COMP201 Java Programming Topic 5: Input and Output Reading: Chapter 12.
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.
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.
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.
Advanced Java Class Serialization. Serialization – what and why? What? –Translating the contents of an Object to a series of bytes that represent it,
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L08 (Chapter 18) Binary I/O.
Exceptions and IO Dr. Andrew Wallace PhD BEng(hons) EurIng
Io package as Java’s basic I/O system continue’d.
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.
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
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.
Java How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Chapter 17 Input and Output F Stream Classes F Processing External Files F Data Streams F Print Streams F Buffered Streams  Use JFileChooser F Text Input.
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.
Object Persistence and Object serialization CSNB534 Asma Shakil.
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.
Presented by Bartosz Sakowicz Java Input/Output (I/O)
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.
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 OOP Lecture 17 I/O and Graphics Signe Ellegård Borch Carsten Schuermann IT University Copenhagen.
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.
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.
©SoftMoore ConsultingSlide 1 Serialization. ©SoftMoore ConsultingSlide 2 Serialization Allows objects to be written to a stream Can be used for persistence.
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.
DEPARTMENT OF COMPUTER SCIENCE N.HARIKA. Contents Overview of I/O Streams Character Streams Byte Streams Using the Streams 2.
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.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 13 Java Fundamentals File I/O Serializing an.
CPRG 215 Introduction to Object-Oriented Programming with Java Module 2- Using Java Built-in Classes Topic 2.6 Reading Input with java.io Produced by Harvey.
 Pearson Education, Inc. All rights reserved Files and Streams.
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 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.
Java Programming Language (3) - Input/Output Stream –
1 CSE 331 Memento Pattern and Serialization slides created by Marty Stepp based on materials by M. Ernst, S. Reges, D. Notkin, R. Mercer, Wikipedia
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.
The Java IO System Different kinds of IO Different kinds of operations
Keerthi Nelaturu Url: site.uottawa.ca/~knela006
Sequential files creation & writing
CSG2H3 Object Oriented Programming
Java Text I/O CS140 Dick Steflik. Reader Abstract super class for character based input Subclasses: – BufferedReader – CharArrayReader – FilterReader.
Ch14 Files and Streams OBJECTIVES
Chapter 8: Input and Output
Object Writing in files
University of Central Florida COP 3330 Object Oriented Programming
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.
I/O Streams- Basics Byte Streams and Character Streams
Java Programming Course
Stream Oriented I/O Computer Programming II Dr. Tim Margush
CSE 331 Memento Pattern slides created by Marty Stepp based on materials by M. Ernst, S. Reges, D. Notkin, R. Mercer, Wikipedia
OBJECT ORIENTED PROGRAMMING II LECTURE 20 GEORGE KOUTSOGIANNAKIS
Files and Streams in Java
14 Files and Streams.
CS 240 – Advanced Programming Concepts
Presentation transcript:

Java Programming: Advanced Topics 1 Input/Output and Serialization

Java Programming: Advanced Topics 2 How the Java Platform Supports I/O The package java.io supports console I/O and file I/O Console I/O is character keyboard input and output without the mouse graphics of a graphical user interface File I/O involves reading and writing data to and from a mass storage device, typically the computer’s hard drive The new I/O (NIO) package java.nio supplements the original I/O package

Java Programming: Advanced Topics 3 Programming I/O In stream I/O, characters or bytes are read or written sequentially The Java platform includes two dual hierarchies of classes that support streams: –byte-oriented input and output –character-oriented input and output The class RandomAccessFile is used to read and write arbitrary locations within a file without first having to read or write all the bytes or characters that precede that location

Java Programming: Advanced Topics 4 Byte-Oriented Stream Classes

Java Programming: Advanced Topics 5 Predefined Stream Objects All Java programs can use three stream objects that are defined in the System class of the java.lang package: –System.in The field System.in is a java.io.BufferedInputStream object –System.out The field System.out is a java.io.PrintStream object –System.err The field System.err is a java.io.PrintStream object

Java Programming: Advanced Topics 6 The print and println Methods

Java Programming: Advanced Topics 7 Input Methods Input methods of the java.io.InputStream class: –int available() –void close() –void mark( int readlimit) –boolean markSupported() –int read() –int read(byte[] buffer) –int read( byte[] buffer, int offset, int length) –void reset() –long skip(long bytecount)

Java Programming: Advanced Topics 8 Output Methods Output methods of the java.io.OutputStream class: –void close() –void flush() –void write( int b) –void write(byte[] buffer) –void write( byte[] buffer, int offset, int length)

Java Programming: Advanced Topics 9 Byte-Oriented Filter Streams

Java Programming: Advanced Topics 10 Other Byte I/O Classes Extensions of InputStream include the following classes: The class ByteArrayInputStream lets you read an array of bytes as though it were an InputStream object The class SequenceInputStream provides a mechanism for concatenating the data from two or more InputStream objects into a single, seamless stream The class PipedInputStream implements half of a pipe to connect the input stream of one thread or process to the output stream of another thread process

Java Programming: Advanced Topics 11 Other Byte I/O Classes Extensions of OutputStream include the following classes : –The class ByteArrayOutputStream sends its output into an object of type byte –The class PipedOutputStream is the complementary class to PipedInputStream

Java Programming: Advanced Topics 12 Console I/O Sample Program

Java Programming: Advanced Topics 13 Parsing an Input Stream The next step after reading a line of input is to parse it, or break it into separate tokens A token usually consists of a sequence of characters that does not include a space, newline, tab, or other nonprinting character To tokenize a string, you can use the StringTokenizer class provided by the java.util package

Java Programming: Advanced Topics 14 File I/O Basics To use a file associate the file with a FileInputStream or FileOutputStream object To access the data in a file in random-access order, open it as a RandomAccessFile object, not as a FileInputStream object In a network environment, applets can perform I/O only on files that reside on the server from which the applet originated

Java Programming: Advanced Topics 15 File Navigation Methods

Java Programming: Advanced Topics 16 An Example of File Input and Output

Java Programming: Advanced Topics 17 An Example of File Input and Output (Cont.)

Java Programming: Advanced Topics 18 An Example of File Input and Output (Cont.)

Java Programming: Advanced Topics 19 Random-Access File I/O The class RandomAccessFile supports byte- oriented I/O to and from random-access files The class RandomAccessFile extends Object RandomAccessFile combines input and output operations in one class The seek method selects the position within the file where the next I/O operation will begin

Java Programming: Advanced Topics 20 Character Streams

Java Programming: Advanced Topics 21 Connecting Byte and Character I/O Classes The Java platform includes adapter classes that bridge between character I/O classes and byte I/O classes: –InputStreamReader –OutputStreamWriter

Java Programming: Advanced Topics 22 Using Other Character I/O Classes Extensions of the Reader object include the following: –The class CharArrayReader lets you read an array of characters as though it were a Reader object –The class StringReader lets you read a String object as though it were a Reader object –The class PipedReader implements half of a pipe and is especially useful for communication between threads.

Java Programming: Advanced Topics 23 Using Other Character I/O Classes Extensions of the Writer object include the following: –The class CharArrayWriter sends its output into an object of type char[] –The class StringWriter lets you write to a StringBuffer object as though it were a Writer object –The class PipedWriter is the complementary class to PipedReader –The class PrintWriter is the character I/O equivalent of the PrintStream class

Java Programming: Advanced Topics 24 The New I/O (NIO) Programming Interface The java.nio package provides new features : –Support for buffer management –A new primitive I/O abstraction called a channel –File locking at the process level –Memory mapping

Java Programming: Advanced Topics 25 Buffers Capacity: the maximum number of data elements that the buffer can contain Limit: a reflection of the amount of data that the buffer currently contains and is defined as the index of the first element in the buffer that should not be read or written Mark: the index to which the position value will be set if the buffer is reset Position: the index of the next element to be read or written and can never exceed the limit

Java Programming: Advanced Topics 26 Buffer Classes in java.nio

Java Programming: Advanced Topics 27 Channels Channel: a flexible concept that includes any open connection to a program entity that is capable of I/O operations such as read or write

Java Programming: Advanced Topics 28 Channel Classes in java.nio.channels

Java Programming: Advanced Topics 29 An Example of File Channel I/O

Java Programming: Advanced Topics 30 An Example of File Channel I/O (Cont.)

Java Programming: Advanced Topics 31 An Example of File Channel I/O (Cont.)

Java Programming: Advanced Topics 32 Object Serialization Object serialization: a general solution that lets you write objects to I/O streams and then read them, without defining any additional methods Enables transmission of objects over a network or save objects to files between runs of your program The object serialization feature is an important component of the Java Remote Method Invocation (RMI) enterprise API

Java Programming: Advanced Topics 33 Using Object Streams Two stream classes support object serialization: –ObjectOutputStream - a subclass of OutputStream –ObjectInputStream - a subclass of InputStream To create an ObjectOutputStream object, provide an existing OutputStream object as the argument of the constructor To create an ObjectInputStream object, provide an existing InputStream object as the argument of the constructor

Java Programming: Advanced Topics 34 Suppressing Serialization of Fields Include the qualifier transient when you declare fields to indicate that they should not be serialized with instances of the class Fields that have the transient qualifier are not output when the object is serialized When the object is deserialized later, transient fields are given the default value normally used for fields of their type

Java Programming: Advanced Topics 35 The readObject and writeObject Methods

Java Programming: Advanced Topics 36 The validateObject Method

Java Programming: Advanced Topics 37 Forcing Changed Objects to Be Serialized Subsequent serialization operations for the same object copy only the object reference into the stream, even if the object has changed A simple solution to this problem: –invoke the reset method for the ObjectOutputStream object, which causes the next serialization of an object to be treated as if it were the first

Java Programming: Advanced Topics 38 Specifying the Version Number A version number is calculated using a formula that takes the name of the class and its interfaces, fields, and methods Determine the value of the version number with the serialver tool supplied with the SDK Define a serialVersionUID field and let the JVM generate one for you

Java Programming: Advanced Topics 39 Compatibility of Serialization Formats To identify the serialization stream format used, Java 2 defines two new constants: PROTOCOL_VERSION_1 and PROTOCOL_VERSION_2, in the java.io.ObjectStreamConstants interface The useProtocolVersion method takes a protocol version constant as input and updates the ObjectOutputStream object to use the corresponding serialization stream version