top io hierarchy

15
2003 Prentice Hall, Inc. All rights reserved. 1 Top IO Hierarchy

Upload: palma

Post on 05-Jan-2016

14 views

Category:

Documents


0 download

DESCRIPTION

Top IO Hierarchy. Input Stream. Output Stream. Reader. Writer. Streams. InputStream Reader. BufferedReader std in = new BufferedReader ( new InputStreamReader ( System.in ) );. System.in is an object of InputStream. PrintWriter out = new PrintWriter ( new BufferedWriter ( - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Top IO  Hierarchy

2003 Prentice Hall, Inc. All rights reserved.

1

Top IO Hierarchy

Page 2: Top IO  Hierarchy

2003 Prentice Hall, Inc. All rights reserved.

2

Input Stream

Page 3: Top IO  Hierarchy

2003 Prentice Hall, Inc. All rights reserved.

3

Output Stream

Page 4: Top IO  Hierarchy

2003 Prentice Hall, Inc. All rights reserved.

4

Reader

Page 5: Top IO  Hierarchy

2003 Prentice Hall, Inc. All rights reserved.

5

Writer

Page 6: Top IO  Hierarchy

2003 Prentice Hall, Inc. All rights reserved.

6

Streams

Page 7: Top IO  Hierarchy

2003 Prentice Hall, Inc. All rights reserved.

7

InputStreamReader

BufferedReader stdin = new BufferedReader (

new InputStreamReader ( System.in ) );

System.in is an object of InputStream

Page 8: Top IO  Hierarchy

2003 Prentice Hall, Inc. All rights reserved.

8FileReader & FileWriter

BufferedReader in = new BufferedReader (

new FileReader ( “foo.in” ) );

PrintWriter out = new PrintWriter (

new BufferedWriter (

new FileWriter (“foo.out” ) ) );

Note: System.out and System.err are PrintWriter objects

Page 9: Top IO  Hierarchy

2003 Prentice Hall, Inc. All rights reserved.

9

Reading from binary file

FileInputStream fis =

new FileInputStream(myFileObj);

ObjectInputStream ois =

new ObjectInputStream(fis);

int i = ois.readInt();

String today = (String) ois.readObject();

Date date = (Date) ois.readObject();

ois.close();

Page 10: Top IO  Hierarchy

2003 Prentice Hall, Inc. All rights reserved.

10

How to tell end of file

try { read from file}catch ( EOFException endOfFileException ) { do what you need to do when

end of file is reached}

Other ways possible. This is the recommended method.

Page 11: Top IO  Hierarchy

2003 Prentice Hall, Inc. All rights reserved.

11

Writing to binary file

FileOutputStream fos = new FileOutputStream(myFileObj);

ObjectOutputStream oos =

new ObjectOutputStream(fos);

oos.writeInt(12345);

oos.writeObject("Today");

oos.writeObject(new Date());

oos.close();

Page 12: Top IO  Hierarchy

2003 Prentice Hall, Inc. All rights reserved.

12

Reading from text file

BufferedReader input =

new BufferedReader(

new FileReader( myFile ) );

String text = input.readLine();

input.read(myCharArray, off, len);

input.close();

Page 13: Top IO  Hierarchy

2003 Prentice Hall, Inc. All rights reserved.

13

Writing to text file

BufferedWriter output =

new BufferedWriter(

new FileWriter( myFile ) );

String text = “hello \n”;

output.write(text);

output.write(myCharArray,off,len);

output.flush() ;

output.close();

Page 14: Top IO  Hierarchy

2003 Prentice Hall, Inc. All rights reserved.

14

Random Access Files

– Use fixed length for every record• Easy to calculate record locations

100bytes

100bytes

100bytes

100bytes

100bytes

100bytes

0 100 200 300 400 500

byte offsets

Page 15: Top IO  Hierarchy

2003 Prentice Hall, Inc. All rights reserved.

15

Random Access File Methods

RandomAccessFile raFile = new RandomAccessFile( fileObj, "rw" );raFile.seek( ( recordNumber - 1 ) *

RandomAccessAccountRecord.SIZE );raFile.writeInt( 12345 );raFile.writeDouble( getBalance() );raFile.writeChars( buffer.toString() );int i = raFile.readInt();double d = raFile.readDouble();char c = raFile.readChar();

raFile.close();