Docsity

Java I/O and Serialization: File Writing, Object Streams, and Tokenizing, Slides of Java Programming

Partial preview of the text

Download Java I/O and Serialization: File Writing, Object Streams, and Tokenizing and more Slides Java Programming in PDF only on Docsity!

Java File Output

 PrintWriter

 composed from several objects

PrintWriter out = new PrintWriter( new FileWriter( dstFileName, false ), true );

 requires throws FileNotFoundException,

which is a sub class of IOException

 Methods

 print() , println(): buffers data to write

 flush(): sends buffered output to destination

 close(): flushes and closes stream docsity.com

Java File Output

// With append to an existing file PrintWriter outFile1 = new PrintWriter( new FileWriter(dstFileName,true),false);

// With autoflush on println PrintWriter outFile2 = new PrintWriter( new FileWriter(dstFileName,false),true);

outFile1.println( “appended w/out flush” ); outFile2.println( “overwrite with flush” );

Caeser Cipher

 Encryption key – the function to change the

value

 Simple key – shift each letter over by 1 to 25

characters

 If key = 3, A  D B  E etc.

 Decryption = reversing the encryption

 Here we just subtract the key value

Object Streams

 Last example read BankAccount field individually

 Easier way to deal with whole object

 ObjectOutputStream class can save a entire

objects to disk

 ObjectOutputStream class can read objects back

in from disk

 Objects are saved in binary format; hence, you

use streams and not writers

Write out an object

 The object output stream saves all instance variables

BankAccount b =.. .;

ObjectOutputStream out = new ObjectOutputStream( new FileOutputStream("bank.dat"));

out.writeObject(b);

Exceptions

 readObject method can throw a

ClassNotFoundException

 It is a checked exception

 You must catch or declare it

Writing an Array

 Usually want to write out a collection of objects:

BankAccount[] arr = new BankAccount[size];

// Now add size BankAccount objects into arr

out.writeObject(arr);

Object Streams

 Very powerful features

 Especially considering how little we have to do

 The BankAccount class as is actually will not

work with the stream

 Must implement Serializable interface in order

for the formatting to work

Object Streams

class BankAccount implements Serializable

{

...

}

 IMPORTANT: Serializable interface has no

methods.

 No effort required

Serialization

 Why isn’t everything serializable?

 Security reasons – may not want contents of objects

printed out to disk, then anyone can print out

internal structure and analyze it

 Example: Don’t want SSN ever being accessed

 Could also have temporary variables that are useless

once the program is done running

Tokenizing

 Often several text values are in a single line in a

file to be compact

 The line must be broken into parts (i.e. tokens )

 tokens then can be parsed as needed

“25” can be turned into the integer 25