chapter 9: objects and classes - cleveland state...

54
Chapter 17 Binary I/O CIS265/506 Cleveland State University Prof. Victor Matos Adapted from: Introduction to Java Programming: Comprehensive Version, Eighth Edition by Y. Daniel Liang

Upload: buidat

Post on 19-Jul-2018

231 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Chapter 9: Objects and Classes - Cleveland State Universitygrail.cba.csuohio.edu/.../lecture-notes/04-19slide-BinaryIO.pdf · Chapter 17 Binary I/O CIS265/506 Cleveland State University

Chapter 17

Binary I/O

CIS265/506 Cleveland State University – Prof. Victor Matos Adapted from: Introduction to Java Programming: Comprehensive Version, Eighth Edition by Y. Daniel Liang

Page 2: Chapter 9: Objects and Classes - Cleveland State Universitygrail.cba.csuohio.edu/.../lecture-notes/04-19slide-BinaryIO.pdf · Chapter 17 Binary I/O CIS265/506 Cleveland State University

Objectives

2

1. To discover how I/O is processed in Java.

2. To distinguish between text I/O and binary I/O.

3. To read and write bytes using FileInputStream and FileOutputStream .

4. To read and write primitive values and strings using DataInputStream/DataOutputStream.

5. To store and restore objects using ObjectOutputStream and ObjectInputStream, and to understand how objects are serialized and what kind of objects can be serialized.

6. To implement the Serializable interface to make objects serializable.

7. To serialize arrays.

8. To read and write the same file using the RandomAccessFile class.

Page 3: Chapter 9: Objects and Classes - Cleveland State Universitygrail.cba.csuohio.edu/.../lecture-notes/04-19slide-BinaryIO.pdf · Chapter 17 Binary I/O CIS265/506 Cleveland State University

Motivations

3

• Data stored in a text file is represented in human-readable form.

• Data stored in a binary file is represented in binary form.

• Binary files are designed to be read by programs.

• For example,

• Java source programs are stored in text files and can be read by a text

editor, but

• Java classes are stored in binary files and are read by the JVM.

The advantage of binary files is that they are more efficient to process than text files.

Page 4: Chapter 9: Objects and Classes - Cleveland State Universitygrail.cba.csuohio.edu/.../lecture-notes/04-19slide-BinaryIO.pdf · Chapter 17 Binary I/O CIS265/506 Cleveland State University

Program

Input object

created from an

input class

Output object

created from an

output class

Input stream

Output stream

File

File 01011…1001

11001…1011

How is I/O Handled in Java?

4

• A File object encapsulates the properties of a file or a path, but does not contain the

methods for reading/writing data from/to a file.

• To perform IO operations you need to create objects using appropriate Java I/O classes.

PrintWriter output = new PrintWriter("c:\\temp.txt");

output.println("Java 101");

output.close();

Scanner input = new Scanner( new File("c:\\temp.txt") );

System.out.println( input.nextLine() );

Page 5: Chapter 9: Objects and Classes - Cleveland State Universitygrail.cba.csuohio.edu/.../lecture-notes/04-19slide-BinaryIO.pdf · Chapter 17 Binary I/O CIS265/506 Cleveland State University

Text File vs. Binary File

5

Observation 1

Although it is not entirely technically correct,

• you can imagine that a text file consists of a sequence of characters and

• a binary file consists of a sequence of bits.

For example, the decimal

integer 199 is stored as

the sequence of three

characters: '1', '9', '9'

in a text file.

The same integer is stored

as a byte-type value C7 in

a binary file, because

decimal 199 equals to

hex C7

(Binary: 11000111).

Page 6: Chapter 9: Objects and Classes - Cleveland State Universitygrail.cba.csuohio.edu/.../lecture-notes/04-19slide-BinaryIO.pdf · Chapter 17 Binary I/O CIS265/506 Cleveland State University

Binary I/O

6

• Text I/O requires encoding and decoding.

The JVM converts a Unicode symbols to/from a file specific encoding

when reading and writing.

• Binary I/O does not require conversions.

When you write a byte to a file, the original byte is copied into the file.

When you read a byte from a file, the exact byte in the file is returned.

Page 7: Chapter 9: Objects and Classes - Cleveland State Universitygrail.cba.csuohio.edu/.../lecture-notes/04-19slide-BinaryIO.pdf · Chapter 17 Binary I/O CIS265/506 Cleveland State University

Binary I/O Classes

7

InputStream

OutputStream

Object

ObjectOutputStream

FilterOutputStream

FileOutputStream

BufferedInputStream

DataInputStream

BufferedOutputStream

DataOutputStream

PrintStream

ObjectInputStream

FilterInputStream

FileInputStream

• A stream is an abstraction that either produces

or consumes data.

• Streams are sequential in nature.

• In Java streams move two possible data types:

bytes and characters

Page 8: Chapter 9: Objects and Classes - Cleveland State Universitygrail.cba.csuohio.edu/.../lecture-notes/04-19slide-BinaryIO.pdf · Chapter 17 Binary I/O CIS265/506 Cleveland State University

InputStream

8

Page 9: Chapter 9: Objects and Classes - Cleveland State Universitygrail.cba.csuohio.edu/.../lecture-notes/04-19slide-BinaryIO.pdf · Chapter 17 Binary I/O CIS265/506 Cleveland State University

OutputStream

9

Page 10: Chapter 9: Objects and Classes - Cleveland State Universitygrail.cba.csuohio.edu/.../lecture-notes/04-19slide-BinaryIO.pdf · Chapter 17 Binary I/O CIS265/506 Cleveland State University

FileInputStream/FileOutputStream

10

FileInputStream/FileOutputStream

• Associates a binary input/output stream with an external file.

• All the methods in FileInputStream/FileOuptputStream are inherited from their superclasses.

InputStream

OutputStream

Object

ObjectOutputStream

FilterOutputStream

FileOutputStream

BufferedInputStream

DataInputStream

BufferedOutputStream

DataOutputStream

PrintStream

ObjectInputStream

FilterInputStream

FileInputStream

Page 11: Chapter 9: Objects and Classes - Cleveland State Universitygrail.cba.csuohio.edu/.../lecture-notes/04-19slide-BinaryIO.pdf · Chapter 17 Binary I/O CIS265/506 Cleveland State University

Example1: FileInputStream

11

FileInputStream is for reading/writing bytes from/to a disk file.

public static void main(String[] args) throws IOException {

FileInputStream fis1 = new FileInputStream("c:\\temp\\mydata.txt");

//alternatively you may also say...

File file = new File("c:\\temp\\mydata.txt");

FileInputStream fis2 = new FileInputStream( file );

int avail = fis1.available();

for(int i=0; i<avail; i++ ){

int data = fis1.read();

System.out.print(data );

}

}//main

A B C a b c 0 1 2

13 Carriage Return

10 Line Feed

available 27 CONSOLE

65 13 10 66 13 10 67 13 10 97 13 10 98 13 10 99 13 10 48 13 10 49 13 10 50 13 10

Disk

Page 12: Chapter 9: Objects and Classes - Cleveland State Universitygrail.cba.csuohio.edu/.../lecture-notes/04-19slide-BinaryIO.pdf · Chapter 17 Binary I/O CIS265/506 Cleveland State University

import java.io.*;

public class TestFileStream {

public static void main(String[] args) throws IOException {

// Create an output stream to the file

FileOutputStream output = new FileOutputStream("temp.dat");

output.write('A'); output.write('B');

output.write('a'); output.write('b');

output.write(255); output.write(256);

// Output values to the file

for (int i = 0; i <= 20; i++)

output.write(i);

// Close the output stream

output.close();

// Create an input stream for the file

FileInputStream input = new FileInputStream("temp.dat");

// Read values from the file

int value;

while ((value = input.read()) != -1)

System.out.print(value + " ");

// Close the output stream

input.close();

}

}

Example2: FileInputStream & FileOutputStream

12

CONSOLE

65 66 97 98 255 0 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

A, B, a, b, 255, 256 0, 1, 2, … , 20

Disk File

Page 13: Chapter 9: Objects and Classes - Cleveland State Universitygrail.cba.csuohio.edu/.../lecture-notes/04-19slide-BinaryIO.pdf · Chapter 17 Binary I/O CIS265/506 Cleveland State University

Continuation…

13

CONSOLE

65 66 97 98 255 0 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

A, B, a, b, 255, 256 0, 1, 2, … , 20

Viewing temp.dat file

with hexadecimal editor

HxD Hexedit.

Example2: FileInputStream & FileOutputStream

Viewing temp.dat file

with MS-Lister

Hex 41 = Binary 1000 0001 = int 65 = Char ‘A’ 4 1

Page 14: Chapter 9: Objects and Classes - Cleveland State Universitygrail.cba.csuohio.edu/.../lecture-notes/04-19slide-BinaryIO.pdf · Chapter 17 Binary I/O CIS265/506 Cleveland State University

FileOutputStream

14

To construct a FileOutputStream, use the following constructors:

public FileOutputStream ( String filename ) public FileOutputStream ( File file ) public FileOutputStream ( String filename, boolean append ) public FileOutputStream ( File file, boolean append )

• If the file does not exist, a new file would be created.

• If the file already exists, the first two constructors would delete the current contents in the file.

• To retain the current content and append new data into the file, use the last two constructors by passing true to the append parameter.

Page 15: Chapter 9: Objects and Classes - Cleveland State Universitygrail.cba.csuohio.edu/.../lecture-notes/04-19slide-BinaryIO.pdf · Chapter 17 Binary I/O CIS265/506 Cleveland State University

FilterInputStream/FilterOutputStream

15

• Using a filter class enables you to read integers, doubles, and strings instead of just bytes and characters.

• FilterInputStream and FilterOutputStream are the base classes for filtering data.

• When you need to process primitive numeric types, use DatInputStream and DataOutputStream to filter bytes.

InputStream

OutputStream

Object

ObjectOutputStream

FilterOutputStream

FileOutputStream

BufferedInputStream

DataInputStream

BufferedOutputStream

DataOutputStream

PrintStream

ObjectInputStream

FilterInputStream

FileInputStream

Page 16: Chapter 9: Objects and Classes - Cleveland State Universitygrail.cba.csuohio.edu/.../lecture-notes/04-19slide-BinaryIO.pdf · Chapter 17 Binary I/O CIS265/506 Cleveland State University

DataInputStream/DataOutputStream

16

DataInputStream reads bytes from the stream and converts them into appropriate primitive type values or strings.

InputStream

OutputStream

Object

ObjectOutputStream

FilterOutputStream

FileOutputStream

BufferedInputStream

DataInputStream

BufferedOutputStream

DataOutputStream

PrintStream

ObjectInputStream

FilterInputStream

FileInputStream

DataOutputStream converts primitive type values

or strings into bytes and output the bytes to the

stream.

Page 17: Chapter 9: Objects and Classes - Cleveland State Universitygrail.cba.csuohio.edu/.../lecture-notes/04-19slide-BinaryIO.pdf · Chapter 17 Binary I/O CIS265/506 Cleveland State University

DataInputStream

17

DataInputStream reads bytes from the stream and converts them into

appropriate primitive type values or strings.

Page 18: Chapter 9: Objects and Classes - Cleveland State Universitygrail.cba.csuohio.edu/.../lecture-notes/04-19slide-BinaryIO.pdf · Chapter 17 Binary I/O CIS265/506 Cleveland State University

DataOutputStream

18

DataOutputStream converts primitive type values or strings into bytes

and outputs the bytes to the stream.

Page 19: Chapter 9: Objects and Classes - Cleveland State Universitygrail.cba.csuohio.edu/.../lecture-notes/04-19slide-BinaryIO.pdf · Chapter 17 Binary I/O CIS265/506 Cleveland State University

Characters and Strings in Binary I/O

19

• Unicode is an industry standard for dealing with written text.

• It includes over 1,1Million Code Points (u+XXXXXX) and 100+ scripts such a Latin, Arabic, Hebrew, Greek, Cyrillic, Chinese, Thai, Indic, Cherokee, Braille, …

• Unicode can be implemented by different character encodings (such as UTF-8)

• writeChars(String s) writes the Unicode value for each character in the string s to the output (2-bytes for each symbol).

• writeUTF(String s) writes the Unicode value of each character in the string s to the output.

Interesting Links: http://farmdev.com/talks/unicode/

http://www.unicode.org/

Page 20: Chapter 9: Objects and Classes - Cleveland State Universitygrail.cba.csuohio.edu/.../lecture-notes/04-19slide-BinaryIO.pdf · Chapter 17 Binary I/O CIS265/506 Cleveland State University

Characters and Strings in Binary I/O

20

Plain Text Unicode

Hello

U+0048 U+0065 U+006C U+006C U+006F

U+8272 U+8377

Traditional Chinese

String str = "\u8272\u8377";

char c = '\u8377';

Character letter = new Character(

'\u0048');

Page 21: Chapter 9: Objects and Classes - Cleveland State Universitygrail.cba.csuohio.edu/.../lecture-notes/04-19slide-BinaryIO.pdf · Chapter 17 Binary I/O CIS265/506 Cleveland State University

Characters and Strings in Binary I/O

21

• You may use the MS-Windows charmap tool to manage Unicodes.

Scripts Code

Points

Page 22: Chapter 9: Objects and Classes - Cleveland State Universitygrail.cba.csuohio.edu/.../lecture-notes/04-19slide-BinaryIO.pdf · Chapter 17 Binary I/O CIS265/506 Cleveland State University

Characters and Strings in Binary I/O

22

What is UTF-8 ?

• Java, Microsoft.NET, XML and most modern Operating Systems and databases use Unicode (instead of ANSI).

• UTF-8 is an alternative encoding scheme that stores a character using 1, 2, or 3 bytes.

1. ASCII values (less than 0x7F) are coded in one byte.

2. Unicode values less than 0x7FF are coded in two bytes.

3. Other Unicode values are coded in three bytes.

0x7F 0x7FF > 0x7FF

127 2047 > 2047

See http://www.utf8-chartable.de/

Page 23: Chapter 9: Objects and Classes - Cleveland State Universitygrail.cba.csuohio.edu/.../lecture-notes/04-19slide-BinaryIO.pdf · Chapter 17 Binary I/O CIS265/506 Cleveland State University

Using DataInputStream/DataOutputStream

23

The statements given below create data streams.

• The first statement creates an input stream for file in.dat;

• the second statement creates an output stream for file out.dat.

DataInputStream infile =

new DataInputStream( new FileInputStream("in.dat"));

DataOutputStream outfile = new DataOutputStream ( new FileOutputStream("out.dat"));

Page 24: Chapter 9: Objects and Classes - Cleveland State Universitygrail.cba.csuohio.edu/.../lecture-notes/04-19slide-BinaryIO.pdf · Chapter 17 Binary I/O CIS265/506 Cleveland State University

// Create an output stream for file temp.dat

DataOutputStream output = new DataOutputStream( new FileOutputStream("temp2.dat") );

String str = "\u8272"; // character in traditional Chinese

output.writeUTF(str);

output.close();

// Create an input stream for file temp.dat

DataInputStream input = new DataInputStream( new FileInputStream("temp2.dat") );

String str2 = input.readUTF();

int codePoint = str2.codePointAt(0);

System.out.printf(" %s %d %x ", str2, codePoint, codePoint);

Example3: Using DataInputStream/DataOutputStream

24

Write then read a Chinese symbol in Unicode.

Plain Text (Chinese) Unicode – Code Point

(Hexadecimal)

Unicode – Code Point

(Decimal)

U + 8272

33394

Console 色 33394 8272

Page 25: Chapter 9: Objects and Classes - Cleveland State Universitygrail.cba.csuohio.edu/.../lecture-notes/04-19slide-BinaryIO.pdf · Chapter 17 Binary I/O CIS265/506 Cleveland State University

public class TestDataStream {

public static void main(String[] args) throws IOException {

// Create an output stream for file temp.dat

DataOutputStream output = new DataOutputStream( new FileOutputStream("temp2.dat") );

output.writeUTF("John"); output.writeDouble(85.5);

output.writeUTF("Jim"); output.writeDouble(185.5);

output.writeUTF("George"); output.writeDouble(105.25);

// Close output stream

output.close();

// Create an input stream for file temp.dat

DataInputStream input = new DataInputStream( new FileInputStream("temp2.dat") );

// Read student test scores from the file

System.out.println(input.readUTF() + " " + input.readDouble());

System.out.println(input.readUTF() + " " + input.readDouble());

System.out.println(input.readUTF() + " " + input.readDouble());

}

}

Example4: Using DataInputStream/DataOutputStream

25

Write then read student’s name & score.

Console John 85.5 Jim 185.5 George 105.25

Page 26: Chapter 9: Objects and Classes - Cleveland State Universitygrail.cba.csuohio.edu/.../lecture-notes/04-19slide-BinaryIO.pdf · Chapter 17 Binary I/O CIS265/506 Cleveland State University

Checking End of File

26

TIP:

• If you keep reading data at the end of a stream, an EOFException would occur.

• You can use input.available() to check for End-Of-File condition.

• Expression input.available() == 0 indicates that it is the end of a file.

Read/Write Ops. - Order and Format

CAUTION:

You have to read the data in the same order and same format in which they are stored.

For example, when names are written in UTF-8 using writeUTF, you must read names using readUTF.

Page 27: Chapter 9: Objects and Classes - Cleveland State Universitygrail.cba.csuohio.edu/.../lecture-notes/04-19slide-BinaryIO.pdf · Chapter 17 Binary I/O CIS265/506 Cleveland State University

27

Eclipse Console & Unicode Symbols

How to show Unicode symbols on your Eclipse Console? References:

http://bytecounts.com/blog/?p=28

http://paranoid-engineering.blogspot.com/2008/05/getting-unicode-output-in-eclipse.html

Three steps operation:

1. Install an appropriate Unicode Font/Script for your Console and

2. Change your JRE configuration.

3. Make sure the current Eclipse “Run Configuration” is set for UTF-8

Details 1. Go to Eclipse Menu: Window > Preferences > Java > Installed JREs > select JRE >

select Edit. Add to [Default VM Arguments]: -Dfile.encoding=UTF-8

2. Go to Eclipse Menu: Window > Preferences > General > Appearance > Colors & Fonts > Debug > Console font > Edit. Choose a Unicode font, like Lucinda Sans Unicode

Note:

You may use Microsoft-JhengHei for Chinese-Japanese-Korean (CJK) ideographs. By default fonts not in your language-base are disable. You may have to install/enable them.

Page 28: Chapter 9: Objects and Classes - Cleveland State Universitygrail.cba.csuohio.edu/.../lecture-notes/04-19slide-BinaryIO.pdf · Chapter 17 Binary I/O CIS265/506 Cleveland State University

28

Eclipse Console & Unicode Symbols

How to show Unicode symbols on your Eclipse Console?

Step 3: Fixing your: “Run Configuration” to use UTF-8 encoding instead of Cp1252 default encoding

Page 29: Chapter 9: Objects and Classes - Cleveland State Universitygrail.cba.csuohio.edu/.../lecture-notes/04-19slide-BinaryIO.pdf · Chapter 17 Binary I/O CIS265/506 Cleveland State University

29

Eclipse Console & Unicode Symbols

How to show Unicode symbols on your Eclipse Console?

After making changes suggested in previous page, we could add the following fragment to Example4 and obtain results listed below.

// Greek symbols: Koppa, Omega, alpha

System.out.println("\u03d8\u03a0\u03b1");

// CJK (Chinese, Japanese, Korean)

System.out.println("\u8272\u8377\u322F\u5193");

//exclamation, space, numeral

System.out.println("\u0021\u0020\u0023");

// numbers, lower, upper case Latin

System.out.println("\u0035\u0036\u0061\u0062\u0041\u0042");

CONSOLE ϘΩα 色荷㈯冓 ! # 56abAB

Page 30: Chapter 9: Objects and Classes - Cleveland State Universitygrail.cba.csuohio.edu/.../lecture-notes/04-19slide-BinaryIO.pdf · Chapter 17 Binary I/O CIS265/506 Cleveland State University

public class TestUnicode { public static void main(String[] args) throws IOException { // Create an output stream for file temp3.dat

String strGreek = "\u03d8\u03a9\u03b1"; String strCJK = "\u8272\u8377\u322F\u5193"; String strPunctuation = "\u0021\u0020\u0023"; String strLatin = "\u0035\u0036\u0061\u0062\u0041\u0042"; String strLatin2 = "Se\u00F1orita";

DataOutputStream output = new DataOutputStream(new FileOutputStream ( "temp3.dat“ ) ); output.writeUTF( strGreek ); output.writeUTF( strCJK ); output.writeUTF( strPunctuation ); output.writeUTF( strLatin ); output.writeUTF( strLatin2 );

// Close output stream output.close();

// Create an input stream for file temp3.dat DataInputStream input = new DataInputStream(new FileInputStream( "temp3.dat“ ) );

// Read UTF strings from file, show strings & CodePoints as char and bytes while (input.available() > 0 ){

String str = input.readUTF(); System.out.printf( "\n %s" , str ); for(int i=0; i<str.length(); i++) { int codePoint = str.codePointAt(i); System.out.printf( "\n %c %04d " , codePoint, codePoint ); }

} } }

30

Example5: Eclipse Console & Unicode Symbols Extending Example4. Using writeUTF() / readUTF() methods

CONSOLE

ϘΩα Ϙ 0984 Ω 0937 α 0945 色荷㈯冓 色 33394 荷 33655 ㈯ 12847 冓 20883 ! # ! 0033 0032 # 0035 56abAB 5 0053 6 0054 a 0097 b 0098 A 0065 B 0066 Señorita S 0083 e 0101 ñ 0241 o 0111 r 0114 i 0105 t 0116 a 0097

Page 31: Chapter 9: Objects and Classes - Cleveland State Universitygrail.cba.csuohio.edu/.../lecture-notes/04-19slide-BinaryIO.pdf · Chapter 17 Binary I/O CIS265/506 Cleveland State University

BufferedInputStream / BufferedOutputStream

31

WHY ?

BufferedInputStream/ BufferedOutputStream can be used to speed up

input and output by reducing the number of reads and writes.

When the BufferedInputStream is created, an internal buffer array is created. As bytes from the stream are read or skipped, the internal buffer is refilled as necessary from the contained input stream, many bytes at a time.

The mark operation remembers a point in the input stream and the reset operation causes all the bytes read since the most recent mark operation to be reread before new bytes are taken from the contained input stream.

See http://docs.oracle.com/javase/1.4.2/docs/api/java/io/BufferedInputStream.html

Page 32: Chapter 9: Objects and Classes - Cleveland State Universitygrail.cba.csuohio.edu/.../lecture-notes/04-19slide-BinaryIO.pdf · Chapter 17 Binary I/O CIS265/506 Cleveland State University

BufferedInputStream / BufferedOutputStream

32

Using buffers to speed up I/O

InputStream

OutputStream

Object

ObjectOutputStream

FilterOutputStream

FileOutputStream

BufferedInputStream

DataInputStream

BufferedOutputStream

DataOutputStream

PrintStream

ObjectInputStream

FilterInputStream

FileInputStream

All the methods BufferedInputStream/ BufferedOutputStream are

inherited from the InputStream/OutputStream classes.

Page 33: Chapter 9: Objects and Classes - Cleveland State Universitygrail.cba.csuohio.edu/.../lecture-notes/04-19slide-BinaryIO.pdf · Chapter 17 Binary I/O CIS265/506 Cleveland State University

1. If no buffer size is specified, the default size is 512 bytes.

2. A buffered input stream reads as many data as possible into its buffer in a

single read call.

3. By contrast, a buffered output stream calls the write method only when

its buffer fills up or when the flush() method is called.

Constructing

BufferedInputStream/BufferedOutputStream

33

// Create a BufferedInputStream

public BufferedInputStream ( InputStream in)

public BufferedInputStream ( InputStream in, int bufferSize )

// Create a BufferedOutputStream

public BufferedOutputStream ( OutputStream out)

public BufferedOutputStream ( OutputStreamr out, int bufferSize )

Page 34: Chapter 9: Objects and Classes - Cleveland State Universitygrail.cba.csuohio.edu/.../lecture-notes/04-19slide-BinaryIO.pdf · Chapter 17 Binary I/O CIS265/506 Cleveland State University

Case Study: Copy File Project The program copies a source file to a destination & displays the number of bytes read. If the source does not exist, it tells the user the file is not found. If the target file already exists, tell the user the file already exists. public class Copy { public static void main(String[] args) throws IOException { // Check command-line parameter usage // String [] argsTest = { "c:\\Windows\\explorer.exe", "c:\\temp\\explorer-COPY.exe" }; // args = argsTest; // use these values for testing if (args.length != 2) { System.out.println( "Usage: java Copy sourceFile targetfile"); System.exit(0); } // Check if source file exists File sourceFile = new File(args[0]); if (!sourceFile.exists()) { System.out.println("Source file " + args[0] + " not exist"); System.exit(0); } // Check if target file exists File targetFile = new File(args[1]); if (targetFile.exists()) { System.out.println("Target file " + args[1] + " already exists"); System.exit(0); } 34

Page 35: Chapter 9: Objects and Classes - Cleveland State Universitygrail.cba.csuohio.edu/.../lecture-notes/04-19slide-BinaryIO.pdf · Chapter 17 Binary I/O CIS265/506 Cleveland State University

Case Study: Copy File Project

35

2 of 2 . Continuation…

// Create an input stream BufferedInputStream input = new BufferedInputStream(new FileInputStream(sourceFile)); // Create an output stream BufferedOutputStream output = new BufferedOutputStream(new FileOutputStream(targetFile)); // Continuously read a byte from input and write it to output int r; int numberOfBytesCopied = 0; while ((r = input.read()) != -1) { output.write((byte)r); numberOfBytesCopied++; } // Close streams input.close(); output.close(); // Display the file size System.out.println(numberOfBytesCopied + " bytes copied"); } }

Page 36: Chapter 9: Objects and Classes - Cleveland State Universitygrail.cba.csuohio.edu/.../lecture-notes/04-19slide-BinaryIO.pdf · Chapter 17 Binary I/O CIS265/506 Cleveland State University

Object I/O

36

1. ObjectInputStream/ObjectOutputStream enables you to perform I/O for objects in addition to primitive type values and strings.

2. You can replace DataInputStream/DataOutputStream completely with ObjectInputStream/ObjectOutputStream.

InputStream

OutputStream

Object

ObjectOutputStream

FilterOutputStream

FileOutputStream

BufferedInputStream

DataInputStream

BufferedOutputStream

DataOutputStream

PrintStream

ObjectInputStream

FilterInputStream

FileInputStream

Page 37: Chapter 9: Objects and Classes - Cleveland State Universitygrail.cba.csuohio.edu/.../lecture-notes/04-19slide-BinaryIO.pdf · Chapter 17 Binary I/O CIS265/506 Cleveland State University

ObjectInputStream

37

• This class is typically used to serialize objects residing in volatile

memory and transfer them to persistent storage (such as disk)

• The method writeObject(…) is used to convert the object’s state into a

sequence of bytes.

Page 38: Chapter 9: Objects and Classes - Cleveland State Universitygrail.cba.csuohio.edu/.../lecture-notes/04-19slide-BinaryIO.pdf · Chapter 17 Binary I/O CIS265/506 Cleveland State University

ObjectInputStream

38

1. This class is typically used to de-serialize persistent objects residing in

disk and bring them back to memory.

2. The readObject(…) method is used to convert a sequence of bytes into

an object’s instance.

Page 39: Chapter 9: Objects and Classes - Cleveland State Universitygrail.cba.csuohio.edu/.../lecture-notes/04-19slide-BinaryIO.pdf · Chapter 17 Binary I/O CIS265/506 Cleveland State University

Using Object Streams (Output)

39

public class TestObjectOutputStream { public static void main(String[] args) throws IOException { // Create an output stream for file object.dat ObjectOutputStream output = new ObjectOutputStream( new FileOutputStream ( "object.dat“ ) ); // Write a string, double value, and object to the file output.writeUTF ( "John" ); output.writeDouble ( 85.5 ); output.writeObject ( new java.util.Date() ); // Close output stream output.close(); } }

Page 40: Chapter 9: Objects and Classes - Cleveland State Universitygrail.cba.csuohio.edu/.../lecture-notes/04-19slide-BinaryIO.pdf · Chapter 17 Binary I/O CIS265/506 Cleveland State University

Using Object Streams (Input)

40

public class TestObjectInputStream { public static void main(String[] args) throws ClassNotFoundException, IOException { // Create an input stream for file object.dat ObjectInputStream input = new ObjectInputStream(new FileInputStream("object.dat")); // Write a string, double value, and object to the file String name = input.readUTF(); double score = input.readDouble(); java.util.Date date = (java.util.Date)(input.readObject()); System.out.println(name + " " + score + " " + date); // Close output stream input.close(); } }

CONSOLE John 85.5 Fri Feb 24 20:51:25 EST 2012

Page 41: Chapter 9: Objects and Classes - Cleveland State Universitygrail.cba.csuohio.edu/.../lecture-notes/04-19slide-BinaryIO.pdf · Chapter 17 Binary I/O CIS265/506 Cleveland State University

Example Person p1 = new Person(“Maria Macarena”, 28, “123 Salsa Rd. Cleveland OH”); . . . outputFile.writeObject ( p1 ); // this is simpler than writing to disk // each data item inside the p1 object.

The Serializable Interface

41

1. Objects that can be written to an object stream are said to be serializable.

2. Some classes such as Thread, Socket, and Streams are not serializable.

3. The class of a serializable object must implement (or inherit) Serializable.

4. The Serializable interface is a marker interface. It has no methods.

5. Fortunately most Java API classes, utilities, lists, and GUI components

implement the Serializable interface

Page 42: Chapter 9: Objects and Classes - Cleveland State Universitygrail.cba.csuohio.edu/.../lecture-notes/04-19slide-BinaryIO.pdf · Chapter 17 Binary I/O CIS265/506 Cleveland State University

The transient Keyword

42

• Question: If an object is an instance of Serializable, but it contains

non-serializable fields, can the object be serialized?

The answer is no !

• To enable the object to be serialized, you can use the transient

keyword to mark these data fields to tell the JVM to ignore these

fields when writing the object to an object stream.

Page 43: Chapter 9: Objects and Classes - Cleveland State Universitygrail.cba.csuohio.edu/.../lecture-notes/04-19slide-BinaryIO.pdf · Chapter 17 Binary I/O CIS265/506 Cleveland State University

The transient Keyword, cont.

43

Consider the following class:

public class Foo implements java.io.Serializable {

private int v1;

private static double v2;

private transient A v3 = new A();

}

class A { ... } // assume A is not serializable

1. When an object of the Foo class is serialized, only variable v1 is serialized.

2. Variable v2 is not serialized because it is a static variable, and

3. variable v3 is not serialized because it is marked transient.

4. If v3 were not marked transient, a java.io.NotSerializableException would occur.

Page 44: Chapter 9: Objects and Classes - Cleveland State Universitygrail.cba.csuohio.edu/.../lecture-notes/04-19slide-BinaryIO.pdf · Chapter 17 Binary I/O CIS265/506 Cleveland State University

public class TestSerializableArray implements Serializable { public static void main(String[] args) throws FileNotFoundException, IOException, ClassNotFoundException { // create list, populate & save it to disk ArrayList<Person> list = new ArrayList<Person>();

list.add(new Person("Miles VorKossigan", 28)); list.add(new Person("Honor Harrington", 29)); list.add(new Person("Albin Maker", 20)); list.add(new Person("Ender Higgins", 10));

ObjectOutputStream outputFile = new ObjectOutputStream( new FileOutputStream( "temp5.dat“ ) ); outputFile.writeObject(list); outputFile.close(); list.clear(); // read list back from disk ObjectInputStream inputFile = new ObjectInputStream( new FileInputStream( "temp5.dat“ ) ); list = (ArrayList<Person>) inputFile.readObject(); for (Person p : list){ System.out.println( p.toString() ); } inputFile.close();

} }

Example6: Serializing Arrays 1 of 2

44

1. An array is serializable if all its elements are serializable.

2. So an entire array can be saved using a single writeObject into a file and

later restored using readObject.

Page 45: Chapter 9: Objects and Classes - Cleveland State Universitygrail.cba.csuohio.edu/.../lecture-notes/04-19slide-BinaryIO.pdf · Chapter 17 Binary I/O CIS265/506 Cleveland State University

public class Person implements Serializable {

// class variables private String mPersonName; private int mPersonAge;

// constructor(s) public Person(String mPersonName, int mPersonAge) { this.mPersonName = mPersonName; this.mPersonAge = mPersonAge; }

// mutators public String getmPersonName() { return mPersonName; }

public void setmPersonName(String mPersonName) { this.mPersonName = mPersonName; }

public int getmPersonAge() { return mPersonAge; } public void setmPersonAge(int mPersonAge) { this.mPersonAge = mPersonAge; }

// custom methods public String toString(){ String result = " Name: " + this.getmPersonName() + " Age: " + this.getmPersonAge(); return result; }

}

Example6: Serializing Arrays 2 of 2

45

Page 46: Chapter 9: Objects and Classes - Cleveland State Universitygrail.cba.csuohio.edu/.../lecture-notes/04-19slide-BinaryIO.pdf · Chapter 17 Binary I/O CIS265/506 Cleveland State University

The RandomAccessFile Class

46

• All of the streams we have used so far are known as

read- only or write- only streams.

• The external container of these streams are sequential

files that cannot be updated without creating a new file.

• It is often necessary to change data in the files by

inserting, deleting, or re-writing records.

• Java provides the RandomAccessFile class to allow a file

to be read from and written to at random locations.

Page 47: Chapter 9: Objects and Classes - Cleveland State Universitygrail.cba.csuohio.edu/.../lecture-notes/04-19slide-BinaryIO.pdf · Chapter 17 Binary I/O CIS265/506 Cleveland State University

File Pointer

47

1. A random access file consists of a sequence of bytes.

2. There is a special marker called file pointer that is positioned at one of these bytes.

3. A read or write operation takes place at the location of the file pointer.

4. When a file is opened, the file pointer sets at the beginning of the file.

5. When you read or write data to the file, the file pointer moves forward to the next data.

byte

file

byte

byte

byte

byte

byte

byte

byte

byte

byte

byte

byte

file pointer

byte

file

byte

byte

byte

byte

byte

byte

byte

byte

byte

byte

byte

file pointer

(A) Before readInt()

(B) Before readInt()

Page 48: Chapter 9: Objects and Classes - Cleveland State Universitygrail.cba.csuohio.edu/.../lecture-notes/04-19slide-BinaryIO.pdf · Chapter 17 Binary I/O CIS265/506 Cleveland State University

RandomAccessFile

48

Page 49: Chapter 9: Objects and Classes - Cleveland State Universitygrail.cba.csuohio.edu/.../lecture-notes/04-19slide-BinaryIO.pdf · Chapter 17 Binary I/O CIS265/506 Cleveland State University

RandomAccessFile Methods

49

void seek(long pos) throws IOException;

Sets the offset from the beginning of the RandomAccessFile

stream to where the next read or write occurs.

long getFilePointer() throws IOException;

Returns the current offset, in bytes, from the beginning of the

file to where the next read or write occurs.

Page 50: Chapter 9: Objects and Classes - Cleveland State Universitygrail.cba.csuohio.edu/.../lecture-notes/04-19slide-BinaryIO.pdf · Chapter 17 Binary I/O CIS265/506 Cleveland State University

RandomAccessFile Methods, cont.

50

long length()IOException

Returns the length of the file.

final void writeChar(int v) throws IOException

Writes a character to the file as a two-byte Unicode, with the

high byte written first.

final void writeChars(String s)throws IOException

Writes a string to the file as a sequence of characters.

Page 51: Chapter 9: Objects and Classes - Cleveland State Universitygrail.cba.csuohio.edu/.../lecture-notes/04-19slide-BinaryIO.pdf · Chapter 17 Binary I/O CIS265/506 Cleveland State University

RandomAccessFile

Constructor

51

// allows read and write

RandomAccessFile raf = new RandomAccessFile( "test.dat", "rw" );

// read only

RandomAccessFile raf = new RandomAccessFile( "test.dat", "r" );

Page 52: Chapter 9: Objects and Classes - Cleveland State Universitygrail.cba.csuohio.edu/.../lecture-notes/04-19slide-BinaryIO.pdf · Chapter 17 Binary I/O CIS265/506 Cleveland State University

Example7: A Simple RandomAccessFile

52

public class TestRandomAccessFile { public static void main(String[] args) throws IOException { // Create a random access file RandomAccessFile inout = new RandomAccessFile("inout.dat", "rw"); // Clear the file to destroy the old contents if exists inout.setLength(0); // Write new integers to the file for (int i = 0; i < 200; i++) inout.writeInt(i); // Display the current length of the file System.out.println("Current file length is " + inout.length()); // Retrieve the first number inout.seek(0); // Move the file pointer to the beginning System.out.println("The first number is " + inout.readInt()); // Retrieve the second number inout.seek(1 * 4); // Move the file pointer to the second number System.out.println("The second number is " + inout.readInt()); // Retrieve the tenth number inout.seek(9 * 4); // Move the file pointer to the tenth number System.out.println("The tenth number is " + inout.readInt());

Console Current file length is 800 The first number is 0 The second number is 1 The tenth number is 9

Page 53: Chapter 9: Objects and Classes - Cleveland State Universitygrail.cba.csuohio.edu/.../lecture-notes/04-19slide-BinaryIO.pdf · Chapter 17 Binary I/O CIS265/506 Cleveland State University

Example7: A Simple RandomAccessFile

53

// Modify the eleventh number inout.writeInt(555); // Append a new number inout.seek(inout.length()); // Move the file pointer to the end inout.writeInt(999); // Display the new length System.out.println("The new length is " + inout.length()); // Retrieve the new eleventh number inout.seek(10 * 4); // Move the file pointer to the eleventh number System.out.println("The eleventh number is " + inout.readInt()); inout.close(); } } Console

Current file length is 800 The first number is 0 The second number is 1 The tenth number is 9 The new length is 804 The eleventh number is 555

Page 54: Chapter 9: Objects and Classes - Cleveland State Universitygrail.cba.csuohio.edu/.../lecture-notes/04-19slide-BinaryIO.pdf · Chapter 17 Binary I/O CIS265/506 Cleveland State University

Appendix 1: Fixed Length String I/O

54

1. Random access files are often used to process files of records. 2. For convenience, fixed-length records are used in random access files

so that a record can be located easily. 3. A record consists of a fixed number of fields. 4. A field can be a string or a primitive data type. 5. A string in a fixed-length record has a maximum size. 6. If a string is smaller than the maximum size, the rest of the string is

padded with blanks.

Record 1

Record 2

Record n

Field1 Field 2 … Field k

file e.g.,

Student 1

Student 2

Student n

name street city state zip