meljun cortes jedi slides intro2 chapter12 advanced io streams

69
Introduction to Programming 2 1 12 Advanced I/O Streams

Upload: meljun-cortes-mbampa

Post on 05-Apr-2018

234 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: MELJUN CORTES JEDI Slides Intro2 Chapter12 Advanced IO Streams

7/31/2019 MELJUN CORTES JEDI Slides Intro2 Chapter12 Advanced IO Streams

http://slidepdf.com/reader/full/meljun-cortes-jedi-slides-intro2-chapter12-advanced-io-streams 1/69

Introduction to Programming 2 1

12 Advanced I/O Streams

Page 2: MELJUN CORTES JEDI Slides Intro2 Chapter12 Advanced IO Streams

7/31/2019 MELJUN CORTES JEDI Slides Intro2 Chapter12 Advanced IO Streams

http://slidepdf.com/reader/full/meljun-cortes-jedi-slides-intro2-chapter12-advanced-io-streams 2/69

Introduction to Programming 2 2

Topics

● General Stream Types

 – Character and Byte Streams

 – Input and Output Streams

 – Node and Filter Streams

● The File Class

Reader Classes – Reader Methods

 – Node Reader Classes

 – Filter Reader Classes

Page 3: MELJUN CORTES JEDI Slides Intro2 Chapter12 Advanced IO Streams

7/31/2019 MELJUN CORTES JEDI Slides Intro2 Chapter12 Advanced IO Streams

http://slidepdf.com/reader/full/meljun-cortes-jedi-slides-intro2-chapter12-advanced-io-streams 3/69

Introduction to Programming 2 3

Topics

● Writer Classes

 – Writer Methods

 – Node Writer Classes

 – Filter Writer Classes

● InputStream Classes

 – InputStream Methods

 – Node InputStream Classes

 – Filter InputStream Classes

Page 4: MELJUN CORTES JEDI Slides Intro2 Chapter12 Advanced IO Streams

7/31/2019 MELJUN CORTES JEDI Slides Intro2 Chapter12 Advanced IO Streams

http://slidepdf.com/reader/full/meljun-cortes-jedi-slides-intro2-chapter12-advanced-io-streams 4/69

Introduction to Programming 2 4

Topics

● OutputStream Classes

 – OutputStream Methods

 – Node OutputStream Classes

 – Filter OutputStream Classes

● Serialization

 – The transient Keyword

 – Serialization: Writing an Object Stream

 – Deserialization: Reading an Object Stream

Page 5: MELJUN CORTES JEDI Slides Intro2 Chapter12 Advanced IO Streams

7/31/2019 MELJUN CORTES JEDI Slides Intro2 Chapter12 Advanced IO Streams

http://slidepdf.com/reader/full/meljun-cortes-jedi-slides-intro2-chapter12-advanced-io-streams 5/69

Page 6: MELJUN CORTES JEDI Slides Intro2 Chapter12 Advanced IO Streams

7/31/2019 MELJUN CORTES JEDI Slides Intro2 Chapter12 Advanced IO Streams

http://slidepdf.com/reader/full/meljun-cortes-jedi-slides-intro2-chapter12-advanced-io-streams 6/69

Introduction to Programming 2 6

Character and Byte Streams

● Character streams

 – File or device abstractions for Unicode characters

 – Superclass of all classes for character streams:

● The Reader class

● TheWriter class

● Both classes are abstract 

● Byte streams

 – For binary data

 – Root classes for byte streams:

● The InputStream Class

● The OutputStream Class

● Both classes are abstract 

Page 7: MELJUN CORTES JEDI Slides Intro2 Chapter12 Advanced IO Streams

7/31/2019 MELJUN CORTES JEDI Slides Intro2 Chapter12 Advanced IO Streams

http://slidepdf.com/reader/full/meljun-cortes-jedi-slides-intro2-chapter12-advanced-io-streams 7/69

Introduction to Programming 2 7

Input and Output Streams

● Input or source streams

 – Can read from these streams

 – Superclasses of all input streams:

● The InputStream Class

● The Reader Class

● Output or sink streams

 – Can write to these streams

 –

Root classes of all output streams:● The OutputStream Class

● TheWriter Class

Page 8: MELJUN CORTES JEDI Slides Intro2 Chapter12 Advanced IO Streams

7/31/2019 MELJUN CORTES JEDI Slides Intro2 Chapter12 Advanced IO Streams

http://slidepdf.com/reader/full/meljun-cortes-jedi-slides-intro2-chapter12-advanced-io-streams 8/69

Introduction to Programming 2 8

Node and Filter Streams

● Node streams

 – Contain the basic functionality of reading or writing from a specificlocation

 – Types of node streams include files, memory and pipes

● Filter streams

 – Layered onto node streams between threads or processes

 – For additional functionalities

 –  Adding layers to a node stream is called stream chaining

Page 9: MELJUN CORTES JEDI Slides Intro2 Chapter12 Advanced IO Streams

7/31/2019 MELJUN CORTES JEDI Slides Intro2 Chapter12 Advanced IO Streams

http://slidepdf.com/reader/full/meljun-cortes-jedi-slides-intro2-chapter12-advanced-io-streams 9/69

Introduction to Programming 2 9

The File Class

● Not a stream class

● Important since stream classes manipulate File objects

●  Abstract representation of actual files and directorypathnames

Page 10: MELJUN CORTES JEDI Slides Intro2 Chapter12 Advanced IO Streams

7/31/2019 MELJUN CORTES JEDI Slides Intro2 Chapter12 Advanced IO Streams

http://slidepdf.com/reader/full/meljun-cortes-jedi-slides-intro2-chapter12-advanced-io-streams 10/69

Introduction to Programming 2 10

The File Class: Constructors

● Has four constructors

Page 11: MELJUN CORTES JEDI Slides Intro2 Chapter12 Advanced IO Streams

7/31/2019 MELJUN CORTES JEDI Slides Intro2 Chapter12 Advanced IO Streams

http://slidepdf.com/reader/full/meljun-cortes-jedi-slides-intro2-chapter12-advanced-io-streams 11/69

Introduction to Programming 2 11

The File Class: Methods

Page 12: MELJUN CORTES JEDI Slides Intro2 Chapter12 Advanced IO Streams

7/31/2019 MELJUN CORTES JEDI Slides Intro2 Chapter12 Advanced IO Streams

http://slidepdf.com/reader/full/meljun-cortes-jedi-slides-intro2-chapter12-advanced-io-streams 12/69

Introduction to Programming 2 12

The File Class: Methods

Page 13: MELJUN CORTES JEDI Slides Intro2 Chapter12 Advanced IO Streams

7/31/2019 MELJUN CORTES JEDI Slides Intro2 Chapter12 Advanced IO Streams

http://slidepdf.com/reader/full/meljun-cortes-jedi-slides-intro2-chapter12-advanced-io-streams 13/69

Introduction to Programming 2 13

The File Class: Example

1 import java.io.*;

2

3 public class FileInfoClass {

4 public static void main(String args[]) {

5 String fileName = args[0];

6 File fn = new File(fileName);

7 System.out.println("Name: " + fn.getName());

8 if (!fn.exists()) {

9 System.out.println(fileName

10 + " does not exists.");

11 //continued...

Page 14: MELJUN CORTES JEDI Slides Intro2 Chapter12 Advanced IO Streams

7/31/2019 MELJUN CORTES JEDI Slides Intro2 Chapter12 Advanced IO Streams

http://slidepdf.com/reader/full/meljun-cortes-jedi-slides-intro2-chapter12-advanced-io-streams 14/69

Page 15: MELJUN CORTES JEDI Slides Intro2 Chapter12 Advanced IO Streams

7/31/2019 MELJUN CORTES JEDI Slides Intro2 Chapter12 Advanced IO Streams

http://slidepdf.com/reader/full/meljun-cortes-jedi-slides-intro2-chapter12-advanced-io-streams 15/69

Introduction to Programming 2 15

The File Class: Example

24 System.out.println(fileName +

25 (fn.exists()? "exists": "does not exist"));

26 return;

27 } //end of: if (!fn.exists())

28

29 System.out.println(fileName + " is a " +

30 (fn.isFile()? "file." :"directory."));

31 if (fn.isDirectory()) {

32 String content[] = fn.list();

33 System.out.println("The content of this

34 directory:");

35 //continued...

Page 16: MELJUN CORTES JEDI Slides Intro2 Chapter12 Advanced IO Streams

7/31/2019 MELJUN CORTES JEDI Slides Intro2 Chapter12 Advanced IO Streams

http://slidepdf.com/reader/full/meljun-cortes-jedi-slides-intro2-chapter12-advanced-io-streams 16/69

Page 17: MELJUN CORTES JEDI Slides Intro2 Chapter12 Advanced IO Streams

7/31/2019 MELJUN CORTES JEDI Slides Intro2 Chapter12 Advanced IO Streams

http://slidepdf.com/reader/full/meljun-cortes-jedi-slides-intro2-chapter12-advanced-io-streams 17/69

Introduction to Programming 2 17

The File Class: Example

47 System.out.println(fileName + " is " + fn.length()

48 + " bytes long.");

49 System.out.println(fileName + " was last " +

50 "modified on " + fn.lastModified() +

51 ".");

52 if (!fn.canWrite()) {

53 System.out.println(fileName

54 + " is not writable.");

55 }

56 }

57 }

Page 18: MELJUN CORTES JEDI Slides Intro2 Chapter12 Advanced IO Streams

7/31/2019 MELJUN CORTES JEDI Slides Intro2 Chapter12 Advanced IO Streams

http://slidepdf.com/reader/full/meljun-cortes-jedi-slides-intro2-chapter12-advanced-io-streams 18/69

Introduction to Programming 2 18

The File Class: Example

Content of temp.txt:

what a wonderful world

1, 2, step

Page 19: MELJUN CORTES JEDI Slides Intro2 Chapter12 Advanced IO Streams

7/31/2019 MELJUN CORTES JEDI Slides Intro2 Chapter12 Advanced IO Streams

http://slidepdf.com/reader/full/meljun-cortes-jedi-slides-intro2-chapter12-advanced-io-streams 19/69

Introduction to Programming 2 19

The File Class: Example

Output:

Name: temp.txt

temp.txt is a file.

temp.txt is 34 bytes long.

temp.txt was last modified on 1149150489177.

Page 20: MELJUN CORTES JEDI Slides Intro2 Chapter12 Advanced IO Streams

7/31/2019 MELJUN CORTES JEDI Slides Intro2 Chapter12 Advanced IO Streams

http://slidepdf.com/reader/full/meljun-cortes-jedi-slides-intro2-chapter12-advanced-io-streams 20/69

Introduction to Programming 2 20

The Reader Class: Methods

Page 21: MELJUN CORTES JEDI Slides Intro2 Chapter12 Advanced IO Streams

7/31/2019 MELJUN CORTES JEDI Slides Intro2 Chapter12 Advanced IO Streams

http://slidepdf.com/reader/full/meljun-cortes-jedi-slides-intro2-chapter12-advanced-io-streams 21/69

Introduction to Programming 2 21

The Reader Class: Methods

Page 22: MELJUN CORTES JEDI Slides Intro2 Chapter12 Advanced IO Streams

7/31/2019 MELJUN CORTES JEDI Slides Intro2 Chapter12 Advanced IO Streams

http://slidepdf.com/reader/full/meljun-cortes-jedi-slides-intro2-chapter12-advanced-io-streams 22/69

Introduction to Programming 2 22

Node Reader Classes

Page 23: MELJUN CORTES JEDI Slides Intro2 Chapter12 Advanced IO Streams

7/31/2019 MELJUN CORTES JEDI Slides Intro2 Chapter12 Advanced IO Streams

http://slidepdf.com/reader/full/meljun-cortes-jedi-slides-intro2-chapter12-advanced-io-streams 23/69

Introduction to Programming 2 23

Filter Reader Classes

Page 24: MELJUN CORTES JEDI Slides Intro2 Chapter12 Advanced IO Streams

7/31/2019 MELJUN CORTES JEDI Slides Intro2 Chapter12 Advanced IO Streams

http://slidepdf.com/reader/full/meljun-cortes-jedi-slides-intro2-chapter12-advanced-io-streams 24/69

Introduction to Programming 2 24

The Writer Class: Methods

Page 25: MELJUN CORTES JEDI Slides Intro2 Chapter12 Advanced IO Streams

7/31/2019 MELJUN CORTES JEDI Slides Intro2 Chapter12 Advanced IO Streams

http://slidepdf.com/reader/full/meljun-cortes-jedi-slides-intro2-chapter12-advanced-io-streams 25/69

Introduction to Programming 2 25

Node Writer Classes

Page 26: MELJUN CORTES JEDI Slides Intro2 Chapter12 Advanced IO Streams

7/31/2019 MELJUN CORTES JEDI Slides Intro2 Chapter12 Advanced IO Streams

http://slidepdf.com/reader/full/meljun-cortes-jedi-slides-intro2-chapter12-advanced-io-streams 26/69

Page 27: MELJUN CORTES JEDI Slides Intro2 Chapter12 Advanced IO Streams

7/31/2019 MELJUN CORTES JEDI Slides Intro2 Chapter12 Advanced IO Streams

http://slidepdf.com/reader/full/meljun-cortes-jedi-slides-intro2-chapter12-advanced-io-streams 27/69

Page 28: MELJUN CORTES JEDI Slides Intro2 Chapter12 Advanced IO Streams

7/31/2019 MELJUN CORTES JEDI Slides Intro2 Chapter12 Advanced IO Streams

http://slidepdf.com/reader/full/meljun-cortes-jedi-slides-intro2-chapter12-advanced-io-streams 28/69

Introduction to Programming 2 28

Basic Reader  /Writer Example

12 while ((data = reader.read()) != -1) {

13   writer.write(data);

14 }

15   reader.close();

16 writer.close();

17 } catch (IOException ie) {

18 ie.printStackTrace();

19 }

20 }

21 //continued...

Page 29: MELJUN CORTES JEDI Slides Intro2 Chapter12 Advanced IO Streams

7/31/2019 MELJUN CORTES JEDI Slides Intro2 Chapter12 Advanced IO Streams

http://slidepdf.com/reader/full/meljun-cortes-jedi-slides-intro2-chapter12-advanced-io-streams 29/69

Introduction to Programming 2 29

Basic Reader  /Writer Example

22 public static void main(String args[]) {

23 String inputFile = args[0];

24 String outputFile = args[1];

25 CopyFile cf = new CopyFile();

26 cf.copy(inputFile, outputFile);

27 }

28 }

Page 30: MELJUN CORTES JEDI Slides Intro2 Chapter12 Advanced IO Streams

7/31/2019 MELJUN CORTES JEDI Slides Intro2 Chapter12 Advanced IO Streams

http://slidepdf.com/reader/full/meljun-cortes-jedi-slides-intro2-chapter12-advanced-io-streams 30/69

Introduction to Programming 2 30

Basic Reader  /Writer Example

Page 31: MELJUN CORTES JEDI Slides Intro2 Chapter12 Advanced IO Streams

7/31/2019 MELJUN CORTES JEDI Slides Intro2 Chapter12 Advanced IO Streams

http://slidepdf.com/reader/full/meljun-cortes-jedi-slides-intro2-chapter12-advanced-io-streams 31/69

Page 32: MELJUN CORTES JEDI Slides Intro2 Chapter12 Advanced IO Streams

7/31/2019 MELJUN CORTES JEDI Slides Intro2 Chapter12 Advanced IO Streams

http://slidepdf.com/reader/full/meljun-cortes-jedi-slides-intro2-chapter12-advanced-io-streams 32/69

Introduction to Programming 2 32

Modified Reader  /Writer  

Example13 while ((data = reader.readLine()) != null) {

14   writer.write(data, 0, data.length);

15 }

16 reader.close();

17 writer.close();

18 } catch (IOException ie) {

19 ie.printStackTrace();

20 }

21 }

22 //continued...

Page 33: MELJUN CORTES JEDI Slides Intro2 Chapter12 Advanced IO Streams

7/31/2019 MELJUN CORTES JEDI Slides Intro2 Chapter12 Advanced IO Streams

http://slidepdf.com/reader/full/meljun-cortes-jedi-slides-intro2-chapter12-advanced-io-streams 33/69

Introduction to Programming 233

Modified Reader  /Writer  

Example23 public static void main(String args[]) {

24 String inputFile = args[0];

25 String outputFile = args[1];

26 CopyFile cf = new CopyFile();

27 cf.copy(inputFile, outputFile);

28 }

29 }

Page 34: MELJUN CORTES JEDI Slides Intro2 Chapter12 Advanced IO Streams

7/31/2019 MELJUN CORTES JEDI Slides Intro2 Chapter12 Advanced IO Streams

http://slidepdf.com/reader/full/meljun-cortes-jedi-slides-intro2-chapter12-advanced-io-streams 34/69

Introduction to Programming 234

Modified Reader  /Writer  

Example

Page 35: MELJUN CORTES JEDI Slides Intro2 Chapter12 Advanced IO Streams

7/31/2019 MELJUN CORTES JEDI Slides Intro2 Chapter12 Advanced IO Streams

http://slidepdf.com/reader/full/meljun-cortes-jedi-slides-intro2-chapter12-advanced-io-streams 35/69

Page 36: MELJUN CORTES JEDI Slides Intro2 Chapter12 Advanced IO Streams

7/31/2019 MELJUN CORTES JEDI Slides Intro2 Chapter12 Advanced IO Streams

http://slidepdf.com/reader/full/meljun-cortes-jedi-slides-intro2-chapter12-advanced-io-streams 36/69

Introduction to Programming 236

The InputStream Class:

Methods

Page 37: MELJUN CORTES JEDI Slides Intro2 Chapter12 Advanced IO Streams

7/31/2019 MELJUN CORTES JEDI Slides Intro2 Chapter12 Advanced IO Streams

http://slidepdf.com/reader/full/meljun-cortes-jedi-slides-intro2-chapter12-advanced-io-streams 37/69

Introduction to Programming 237

Node InputStream Classes

Filt I tSt Cl

Page 38: MELJUN CORTES JEDI Slides Intro2 Chapter12 Advanced IO Streams

7/31/2019 MELJUN CORTES JEDI Slides Intro2 Chapter12 Advanced IO Streams

http://slidepdf.com/reader/full/meljun-cortes-jedi-slides-intro2-chapter12-advanced-io-streams 38/69

Introduction to Programming 2 38

Filter InputStream Classes

The OutputStream Class:

Page 39: MELJUN CORTES JEDI Slides Intro2 Chapter12 Advanced IO Streams

7/31/2019 MELJUN CORTES JEDI Slides Intro2 Chapter12 Advanced IO Streams

http://slidepdf.com/reader/full/meljun-cortes-jedi-slides-intro2-chapter12-advanced-io-streams 39/69

Introduction to Programming 2 39

The OutputStream Class:Methods

Page 40: MELJUN CORTES JEDI Slides Intro2 Chapter12 Advanced IO Streams

7/31/2019 MELJUN CORTES JEDI Slides Intro2 Chapter12 Advanced IO Streams

http://slidepdf.com/reader/full/meljun-cortes-jedi-slides-intro2-chapter12-advanced-io-streams 40/69

Introduction to Programming 2 40

Node OutputStream Classes

Page 41: MELJUN CORTES JEDI Slides Intro2 Chapter12 Advanced IO Streams

7/31/2019 MELJUN CORTES JEDI Slides Intro2 Chapter12 Advanced IO Streams

http://slidepdf.com/reader/full/meljun-cortes-jedi-slides-intro2-chapter12-advanced-io-streams 41/69

Introduction to Programming 2 41

Filter OutputStream Classes

B i I tSt /

Page 42: MELJUN CORTES JEDI Slides Intro2 Chapter12 Advanced IO Streams

7/31/2019 MELJUN CORTES JEDI Slides Intro2 Chapter12 Advanced IO Streams

http://slidepdf.com/reader/full/meljun-cortes-jedi-slides-intro2-chapter12-advanced-io-streams 42/69

Introduction to Programming 2 42

Basic InputStream /OutputStream Example

1 import java.io.*;

2

3 class CopyFile {

4 void copy(String input, String output) {

5 FileInputStream inputStr;

6 FileOutputStream outputStr;

7 int data;

8 try {

9 inputStr = new FileInputStream(input);

10 outputStr = new FileOutputStream(output);

11 //continued...

B i I tSt /

Page 43: MELJUN CORTES JEDI Slides Intro2 Chapter12 Advanced IO Streams

7/31/2019 MELJUN CORTES JEDI Slides Intro2 Chapter12 Advanced IO Streams

http://slidepdf.com/reader/full/meljun-cortes-jedi-slides-intro2-chapter12-advanced-io-streams 43/69

Introduction to Programming 2 43

Basic InputStream /OutputStream Example

12 while ((data = inputStr.read()) != -1) {

13   outputStr.write(data);

14 }

15   inputStr.close();

16 outputStr.close();

17 } catch (IOException ie) {

18 ie.printStackTrace();

19 }

20 }

21 //continued...

B i I tSt /

Page 44: MELJUN CORTES JEDI Slides Intro2 Chapter12 Advanced IO Streams

7/31/2019 MELJUN CORTES JEDI Slides Intro2 Chapter12 Advanced IO Streams

http://slidepdf.com/reader/full/meljun-cortes-jedi-slides-intro2-chapter12-advanced-io-streams 44/69

Introduction to Programming 2 44

Basic InputStream /OutputStream Example

22 public static void main(String args[]) {

23 String inputFile = args[0];

24 String outputFile = args[1];

25 CopyFile cf = new CopyFile();

26 cf.copy(inputFile, outputFile);

27 }

28 }

Basic InputStream/

Page 45: MELJUN CORTES JEDI Slides Intro2 Chapter12 Advanced IO Streams

7/31/2019 MELJUN CORTES JEDI Slides Intro2 Chapter12 Advanced IO Streams

http://slidepdf.com/reader/full/meljun-cortes-jedi-slides-intro2-chapter12-advanced-io-streams 45/69

Introduction to Programming 2 45

Basic InputStream /OutputStream Example

Modified InputStream/

Page 46: MELJUN CORTES JEDI Slides Intro2 Chapter12 Advanced IO Streams

7/31/2019 MELJUN CORTES JEDI Slides Intro2 Chapter12 Advanced IO Streams

http://slidepdf.com/reader/full/meljun-cortes-jedi-slides-intro2-chapter12-advanced-io-streams 46/69

Introduction to Programming 2 46

Modified InputStream /OutputStream Example

1 import java.io.*;

2

3 class CopyFile {

4 void copy(String input) {

5   PushbackInputStream inputStr;

6 PrintStream outputStr;

7 int data;

8 try {

9   inputStr = new PushbackInputStream(new10 FileInputStream(input));

11 outputStr = new PrintStream(System.out);

12 //continued...

Modified InputStream/

Page 47: MELJUN CORTES JEDI Slides Intro2 Chapter12 Advanced IO Streams

7/31/2019 MELJUN CORTES JEDI Slides Intro2 Chapter12 Advanced IO Streams

http://slidepdf.com/reader/full/meljun-cortes-jedi-slides-intro2-chapter12-advanced-io-streams 47/69

Introduction to Programming 2 47

Modified InputStream /OutputStream Example

13 while ((data = inputStr.read()) != -1) {

14   outputStr.println("read data: " +

15 (char) data);

16 inputStr.unread(data);

17 data = inputStr.read();

18 outputStr.println("unread data: " +

19 (char) data);

20 }

21   inputStr.close();22 outputStr.close();

23 //continued...

Modified InputStream/

Page 48: MELJUN CORTES JEDI Slides Intro2 Chapter12 Advanced IO Streams

7/31/2019 MELJUN CORTES JEDI Slides Intro2 Chapter12 Advanced IO Streams

http://slidepdf.com/reader/full/meljun-cortes-jedi-slides-intro2-chapter12-advanced-io-streams 48/69

Introduction to Programming 2 48

Modified InputStream /OutputStream Example

24 } catch (IOException ie) {

25 ie.printStackTrace();

26 }

27 }

28

29 public static void main(String args[]) {

30 String inputFile = args[0];

31 CopyFile cf = new CopyFile();

32 cf.copy(inputFile);33 }

34 }

Modified InputStream/

Page 49: MELJUN CORTES JEDI Slides Intro2 Chapter12 Advanced IO Streams

7/31/2019 MELJUN CORTES JEDI Slides Intro2 Chapter12 Advanced IO Streams

http://slidepdf.com/reader/full/meljun-cortes-jedi-slides-intro2-chapter12-advanced-io-streams 49/69

Introduction to Programming 2 49

Modified InputStream /OutputStream Example

Content of passed argument tempo.txt:

one 1

two

Modified InputStream/

Page 50: MELJUN CORTES JEDI Slides Intro2 Chapter12 Advanced IO Streams

7/31/2019 MELJUN CORTES JEDI Slides Intro2 Chapter12 Advanced IO Streams

http://slidepdf.com/reader/full/meljun-cortes-jedi-slides-intro2-chapter12-advanced-io-streams 50/69

Introduction to Programming 2 50

Modified InputStream /OutputStream Example

Output:

read data: o

unread data: oread data: n

unread data: n

read data: e

unread data: eread data:

unread data:

/* continued on next slide */

Modified InputStream/

Page 51: MELJUN CORTES JEDI Slides Intro2 Chapter12 Advanced IO Streams

7/31/2019 MELJUN CORTES JEDI Slides Intro2 Chapter12 Advanced IO Streams

http://slidepdf.com/reader/full/meljun-cortes-jedi-slides-intro2-chapter12-advanced-io-streams 51/69

Introduction to Programming 2 51

Modified InputStream /OutputStream Example

read data: 1

unread data: 1

read data:

unread data:read data:

unread data:

/* continued onnext column */

read data: t

unread data: t

read data: wunread data: w

read data: o

unread data: o

Page 52: MELJUN CORTES JEDI Slides Intro2 Chapter12 Advanced IO Streams

7/31/2019 MELJUN CORTES JEDI Slides Intro2 Chapter12 Advanced IO Streams

http://slidepdf.com/reader/full/meljun-cortes-jedi-slides-intro2-chapter12-advanced-io-streams 52/69

Serialization

Page 53: MELJUN CORTES JEDI Slides Intro2 Chapter12 Advanced IO Streams

7/31/2019 MELJUN CORTES JEDI Slides Intro2 Chapter12 Advanced IO Streams

http://slidepdf.com/reader/full/meljun-cortes-jedi-slides-intro2-chapter12-advanced-io-streams 53/69

Introduction to Programming 2 53

Serialization

● Streams for serialization

 – ObjectInputStream

● For deserializing

 – ObjectOutputStream

● For serializing

● To allow an object to be serializable:

 – Its class should implement the Serializable interface

 – Its class should also provide a default constructor or a constructor 

with no arguments – Serializability is inherited

● Don't have to implement Serializable on every class

● Can just implement Serializable once along the class heirarchy

Non-Serializable Objects

Page 54: MELJUN CORTES JEDI Slides Intro2 Chapter12 Advanced IO Streams

7/31/2019 MELJUN CORTES JEDI Slides Intro2 Chapter12 Advanced IO Streams

http://slidepdf.com/reader/full/meljun-cortes-jedi-slides-intro2-chapter12-advanced-io-streams 54/69

Introduction to Programming 2 54

Non Serializable Objects

● When an object is serialized:

 – Only the object's data are preserved

 – Methods and constructors are not part of the serialized stream

Some objects are not serializable – Because the data they represent constantly changes

 – Examples:

● FileInputStream objects

● Thread objects

●  A NotSerializableException is thrown if the serialization fails

The transient Keyword

Page 55: MELJUN CORTES JEDI Slides Intro2 Chapter12 Advanced IO Streams

7/31/2019 MELJUN CORTES JEDI Slides Intro2 Chapter12 Advanced IO Streams

http://slidepdf.com/reader/full/meljun-cortes-jedi-slides-intro2-chapter12-advanced-io-streams 55/69

Introduction to Programming 2 55

The transient Keyword

●  A class containing a non-serializable object can still beserialized

 – Reference to non-serializable object is marked with the transientkeyword

 – Example:

1 class MyClass implements Serializable {2   transient Thread thread;

3 //try removing transient

4 int data;

5 /* some other data */

6 }

 – The transient keyword prevents the data from being serialized

Serialization: Writing anObj t St

Page 56: MELJUN CORTES JEDI Slides Intro2 Chapter12 Advanced IO Streams

7/31/2019 MELJUN CORTES JEDI Slides Intro2 Chapter12 Advanced IO Streams

http://slidepdf.com/reader/full/meljun-cortes-jedi-slides-intro2-chapter12-advanced-io-streams 56/69

Introduction to Programming 2 56

Object Stream● Use the ObjectOutputStream class

● Use its writeObject method

public final void writeObject(Object obj)

throws IOException

where,

 – obj  is the object to be written to the stream

Serialization: Writing anObj t St

Page 57: MELJUN CORTES JEDI Slides Intro2 Chapter12 Advanced IO Streams

7/31/2019 MELJUN CORTES JEDI Slides Intro2 Chapter12 Advanced IO Streams

http://slidepdf.com/reader/full/meljun-cortes-jedi-slides-intro2-chapter12-advanced-io-streams 57/69

Introduction to Programming 2 57

Object Stream1 import java.io.*;

2 public class SerializeBoolean {

3 SerializeBoolean() {

4 Boolean booleanData = new Boolean("true");

5 try {

6 FileOutputStream fos = new

7 FileOutputStream("boolean.ser");

8   ObjectOutputStream oos = new

9 ObjectOutputStream(fos);

10

oos.writeObject(booleanData);11 oos.close();

12 //continued...

Serialization: Writing anObj t St

Page 58: MELJUN CORTES JEDI Slides Intro2 Chapter12 Advanced IO Streams

7/31/2019 MELJUN CORTES JEDI Slides Intro2 Chapter12 Advanced IO Streams

http://slidepdf.com/reader/full/meljun-cortes-jedi-slides-intro2-chapter12-advanced-io-streams 58/69

Introduction to Programming 2 58

Object Stream13 } catch (IOException ie) {

14 ie.printStackTrace();

15 }

16 }

17

18 public static void main(String args[]) {

19 SerializeBoolean sb = new SerializeBoolean();

20 }

21 }

Deserialization: Reading anObj t St

Page 59: MELJUN CORTES JEDI Slides Intro2 Chapter12 Advanced IO Streams

7/31/2019 MELJUN CORTES JEDI Slides Intro2 Chapter12 Advanced IO Streams

http://slidepdf.com/reader/full/meljun-cortes-jedi-slides-intro2-chapter12-advanced-io-streams 59/69

Introduction to Programming 2 59

Object Stream● Use the ObjectInputStream class

● Use its readObject method

public final Object readObject()

throws IOException, ClassNotFoundException

where,

 – obj  is the object to be read from the stream

The Object  type returned should be typecasted to theappropriate class name before methods on that class can beexecuted

Deserialization: Reading anObj t St

Page 60: MELJUN CORTES JEDI Slides Intro2 Chapter12 Advanced IO Streams

7/31/2019 MELJUN CORTES JEDI Slides Intro2 Chapter12 Advanced IO Streams

http://slidepdf.com/reader/full/meljun-cortes-jedi-slides-intro2-chapter12-advanced-io-streams 60/69

Introduction to Programming 2 60

Object Stream1 import java.io.*;

2 public class UnserializeBoolean {

3 UnserializeBoolean() {

4 Boolean booleanData = null;

5 try {

6 FileInputStream fis = new

7 FileInputStream("boolean.ser");

8   ObjectInputStream ois = new

9 ObjectInputStream(fis);

10 booleanData = (Boolean) ois.readObject();

11 ois.close();

12 //continued...

Deserialization: Reading anObject Stream

Page 61: MELJUN CORTES JEDI Slides Intro2 Chapter12 Advanced IO Streams

7/31/2019 MELJUN CORTES JEDI Slides Intro2 Chapter12 Advanced IO Streams

http://slidepdf.com/reader/full/meljun-cortes-jedi-slides-intro2-chapter12-advanced-io-streams 61/69

Introduction to Programming 2 61

Object Stream13 } catch (Exception e) {

14 e.printStackTrace();

15 }

16 System.out.println("Unserialized Boolean from "

17 + "boolean.ser");

18 System.out.println("Boolean data: " +

19   booleanData);

20 System.out.println("Compare data with true: " +

21   booleanData.equals(new Boolean("true")));

22 }

23 //continued...

Deserialization: Reading anObject Stream

Page 62: MELJUN CORTES JEDI Slides Intro2 Chapter12 Advanced IO Streams

7/31/2019 MELJUN CORTES JEDI Slides Intro2 Chapter12 Advanced IO Streams

http://slidepdf.com/reader/full/meljun-cortes-jedi-slides-intro2-chapter12-advanced-io-streams 62/69

Introduction to Programming 2 62

Object Stream13 public static void main(String args[]) {

14 UnserializeBoolean usb =

15 new UnserializeBoolean();

16 }

17 }

Deserialization: Reading anObject Stream

Page 63: MELJUN CORTES JEDI Slides Intro2 Chapter12 Advanced IO Streams

7/31/2019 MELJUN CORTES JEDI Slides Intro2 Chapter12 Advanced IO Streams

http://slidepdf.com/reader/full/meljun-cortes-jedi-slides-intro2-chapter12-advanced-io-streams 63/69

Introduction to Programming 2 63

Object Stream

Output:

Unserialized Boolean from boolean.ser

Boolean data: trueCompare data with true: true

Summary

Page 64: MELJUN CORTES JEDI Slides Intro2 Chapter12 Advanced IO Streams

7/31/2019 MELJUN CORTES JEDI Slides Intro2 Chapter12 Advanced IO Streams

http://slidepdf.com/reader/full/meljun-cortes-jedi-slides-intro2-chapter12-advanced-io-streams 64/69

Introduction to Programming 2 64

Summary

● General Stream Types

 – Character and Byte Streams

 – Input and Output Streams

 – Node and Filter Streams

● The File Class

 – Constructor 

File(String pathname)

 – Methods

Summary

Page 65: MELJUN CORTES JEDI Slides Intro2 Chapter12 Advanced IO Streams

7/31/2019 MELJUN CORTES JEDI Slides Intro2 Chapter12 Advanced IO Streams

http://slidepdf.com/reader/full/meljun-cortes-jedi-slides-intro2-chapter12-advanced-io-streams 65/69

Introduction to Programming 2 65

Summary

● Reader Classes

 – Methods

● read , close, mark , markSupported , reset 

 – Node Reader Classes

● FileReader , CharArrayReader , StringReader , PipedReader 

 – Filter Reader Classes

● BufferedReader , FilterReader , InputStreamReader ,

  LineNumberReader , PushbackReader 

Summary

Page 66: MELJUN CORTES JEDI Slides Intro2 Chapter12 Advanced IO Streams

7/31/2019 MELJUN CORTES JEDI Slides Intro2 Chapter12 Advanced IO Streams

http://slidepdf.com/reader/full/meljun-cortes-jedi-slides-intro2-chapter12-advanced-io-streams 66/69

Introduction to Programming 2 66

Summary

● Writer Classes

 – Methods

● write, close, flush

 – Node Writer Classes

● FileWriter , CharArrayWriter , StringWriter , PipedWriter 

 – Filter Writer Classes

● BufferedWriter , FilterWriter , OutputStreamWriter , PrintWriter 

Summary

Page 67: MELJUN CORTES JEDI Slides Intro2 Chapter12 Advanced IO Streams

7/31/2019 MELJUN CORTES JEDI Slides Intro2 Chapter12 Advanced IO Streams

http://slidepdf.com/reader/full/meljun-cortes-jedi-slides-intro2-chapter12-advanced-io-streams 67/69

Introduction to Programming 2 67

Summary

● InputStream Classes

 – Methods

● read , close, mark , markSupported , reset 

 – Node InputStream Classes

● FileInputStream, BufferedArrayInputStream, PipedInputStream

 – Filter InputStream Classes

● BufferedInputStream, FilterInputStream, ObjectInputStream,DataInputStream, LineNumberInputStream, PushbackInputStream

Summary

Page 68: MELJUN CORTES JEDI Slides Intro2 Chapter12 Advanced IO Streams

7/31/2019 MELJUN CORTES JEDI Slides Intro2 Chapter12 Advanced IO Streams

http://slidepdf.com/reader/full/meljun-cortes-jedi-slides-intro2-chapter12-advanced-io-streams 68/69

Introduction to Programming 2 68

Summary

● OutputStream Classes

 – Methods

● write, close, flush

 – Node OutputStream Classes

● FileOutputStream, BufferedArrayOutputStream , PipedOutputStream

 – Filter OutputStream Classes

● BufferedOutputStream, FilterOutputStream, ObjectOutputStream,DataOutputStream, PrintStream

Page 69: MELJUN CORTES JEDI Slides Intro2 Chapter12 Advanced IO Streams

7/31/2019 MELJUN CORTES JEDI Slides Intro2 Chapter12 Advanced IO Streams

http://slidepdf.com/reader/full/meljun-cortes-jedi-slides-intro2-chapter12-advanced-io-streams 69/69