Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Java Programming: Advanced Topics 1 Input/Output and Serialization Chapter 3."— Presentation transcript:

1 Java Programming: Advanced Topics 1 Input/Output and Serialization Chapter 3

2 Java Programming: Advanced Topics 2 Objectives Learn how the Java platform supports I/O Understand file I/O basics Understand character streams Use the new I/O (NIO) programming interface Understand object serialization Write appropriate code to read, write, and update files

3 Java Programming: Advanced Topics 3 Objectives (Cont.) Describe the permanent effects on the file system of constructing and using FileInputStream, FileOutputStream, and RandomAccessFile objects Describe the connection between object serialization and I/O streams Distinguish between classes that can be serialized and those that cannot Write code to define a class that can be serialized Learn about the compatibility of serialization formats

4 Java Programming: Advanced Topics 4 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

5 Java Programming: Advanced Topics 5 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

6 Java Programming: Advanced Topics 6 Byte-Oriented Stream Classes

7 Java Programming: Advanced Topics 7 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

8 Java Programming: Advanced Topics 8 The print and println Methods

9 Java Programming: Advanced Topics 9 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)

10 Java Programming: Advanced Topics 10 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)

11 Java Programming: Advanced Topics 11 Byte-Oriented Filter Streams

12 Java Programming: Advanced Topics 12 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

13 Java Programming: Advanced Topics 13 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

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

15 Java Programming: Advanced Topics 15 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

16 Java Programming: Advanced Topics 16 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

17 Java Programming: Advanced Topics 17 File Navigation Methods

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

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

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

21 Java Programming: Advanced Topics 21 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

22 Java Programming: Advanced Topics 22 Character Streams

23 Java Programming: Advanced Topics 23 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

24 Java Programming: Advanced Topics 24 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.

25 Java Programming: Advanced Topics 25 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

26 Java Programming: Advanced Topics 26 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

27 Java Programming: Advanced Topics 27 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

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

29 Java Programming: Advanced Topics 29 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

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

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

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

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

34 Java Programming: Advanced Topics 34 An Example of Mapped Buffers and File Locking

35 Java Programming: Advanced Topics 35 An Example of Mapped Buffers and File Locking (Cont.)

36 Java Programming: Advanced Topics 36 An Example of Mapped Buffers and File Locking (Cont.)

37 Java Programming: Advanced Topics 37 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

38 Java Programming: Advanced Topics 38 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

39 Java Programming: Advanced Topics 39 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

40 Java Programming: Advanced Topics 40 The readObject and writeObject Methods

41 Java Programming: Advanced Topics 41 The validateObject Method

42 Java Programming: Advanced Topics 42 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

43 Java Programming: Advanced Topics 43 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

44 Java Programming: Advanced Topics 44 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

45 Java Programming: Advanced Topics 45 Summary Support for I/O is provided by the core classes in the package java.io and is supplemented by java.nio The Java platform supports byte-oriented streams and the character-oriented streams The predefined stream objects are System.in, System.out and System.err FileInputStream and FileOutputStream are the classes used for reading and writing files Filter-stream classes are designed to wrap an InputStream or an OutputStream class

46 Java Programming: Advanced Topics 46 Summary (Cont.) The class RandomAccessFile is used to read and write information at arbitrary locations within a file To read and write objects as a whole, Java provides object serialization Objects are written to and read from ObjectOutputStream and ObjectInputStream objects Only classes that implement the marker interface Serializable can be serialized Transient fields are not serialized


Download ppt "Java Programming: Advanced Topics 1 Input/Output and Serialization Chapter 3."

Similar presentations


Ads by Google