Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

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

An overview of java input/output (i/o) techniques, focusing on file writing using printwriter and object streams, as well as tokenizing strings. Topics include printwriter methods, binary file encryption, object streams, and serialization. The document also covers the use of stringtokenizer for tokenizing strings and reading data from a file.

Typology: Slides

2011/2012

Uploaded on 07/07/2012

proo
proo 🇮🇳

4.4

(26)

97 documents

1 / 26

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
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
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a

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