ch19 - Chapter Nineteen: Files and Streams Chapter Goals To...

Info iconThis preview shows pages 1–12. Sign up to view the full content.

View Full Document Right Arrow Icon
Chapter Nineteen: Files and Streams
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Chapter Goals To become familiar with the concepts of text and binary formats To learn about encryption To understand when to use sequential and random file access To be able to read and write objects using serialization
Background image of page 2
Text and Binary Formats Two ways to store data: Text format Binary format
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Text Format Human-readable form Sequence of characters Integer 12,345 stored as characters '1' '2' '3' '4' '5' Use Reader and Writer and their subclasses to process input and output To read: FileReader reader = new FileReader("input.txt"); To write FileWriter writer = new FileWriter("output.txt");
Background image of page 4
Binary Format Data items are represented in bytes Integer 12,345 stored as a sequence of four bytes 0 0 48 57 Use InputStream and OutputStream and their subclasses More compact and more efficient To read: FileInputStream inputStream = new FileInputStream("input.bin"); To write FileOutputStream outputStream = new FileOutputStream("output.bin");
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Reading a Single Character from a File in Text Format Use read method of Reader class to read a single character returns the next character as an int or the integer -1 at end of file Reader reader = . . .; int next = reader.read(); char c; if (next != -1) c = (char) next;
Background image of page 6
Reading a Single Character from a File in Binary Format Use read method of InputStream class to read a single byte returns the next byte as an int or the integer -1 at end of file InputStream in = . . .; int next = in.read(); byte b; if (next != -1) b = (byte) next;
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Text and Binary Format Use write method to write a single character or byte read and write are the only input and output methods provided by the file input and output classes Java stream package principle: each class should have a very focused responsibility Job of FileInputStream : interact with files and get bytes To read numbers, strings, or other objects, combine class with other classes \
Background image of page 8
Self Check 19.1 Suppose you need to read an image file that contains color values for each pixel in the image. Will you use a Reader or an InputStream ? Answer: Image data is stored in a binary format – try loading an image file into a text editor, and you won't see much text. Therefore, you should use an InputStream .
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Self Check 19.2 Why do the read methods of the Reader and InputStream classes return an int and not a char or byte ? Answer: They return a special value of -1 to indicate that no more input is available. If the return type had been char or byte , no special value would have been available that is distinguished from a legal data value.
Background image of page 10
File encryption To scramble it so that it is readable only to those who know the encryption method and secret keyword To use Caesar cipher Choose an encryption key – a number between 1 and 25 Example: If the key is 3, replace A with D, B with E, . . . To decrypt, use the negative of the encryption key
Background image of page 11

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 12
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 02/27/2009 for the course SC 418262 taught by Professor Chuleerut during the Spring '08 term at Kasetsart University.

Page1 / 49

ch19 - Chapter Nineteen: Files and Streams Chapter Goals To...

This preview shows document pages 1 - 12. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online