Chapter 20 – Streams and Binary Input/Output Big Java Early Objects by Cay Horstmann Copyright © 2014 by John Wiley & Sons. All rights reserved.

Slides:



Advertisements
Similar presentations
Chapter 19 Binary I/O.
Advertisements

The Package Statement Group related interfaces and classes together Purpose: encapsulation and reduces name conflicts –private package classes not visible.
Java File I/O. File I/O is important! Being able to write and read from files is necessary and is also one common practice of a programmer. Examples include.
Streams Dwight Deugo Nesa Matic Portions of the notes for this lecture include excerpts from.
Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. It is common to use two nested loops when filling or searching: for.
Chapter - 12 File and Streams (continued) This chapter includes -  DataOutputStream  DataInputStream  Object Serialization  Serializing Objects 
Geoff Holmes Overview IO Zoo Stream I/O File I/O Buffering Random-Access Text Streams Examples Serialization Java IO – programs that start with import.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 12 File Input and Output.
© 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--WuChapter Chapter 11 File Input and Output.
Chapter 9 – Inheritance Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 1 Chapter 12 File Input and Output.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 12  File Input and Output Stream Classes Text Input and Output.
Lecture 7 File I/O (and a little bit about exceptions)‏
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 18 Binary I/O.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 1 Chapter 12 File Input and Output.
Slides prepared by Rose Williams, Binghamton University Chapter 10 File I/O.
Chapter 91 Streams and File I/O Chapter 9. 2 Reminders Project 6 released: due Nov 10:30 pm Project 4 regrades due by midnight tonight Discussion.
CS102--Object Oriented Programming Lecture 14: – File I/O BufferedReader The File class Write to /read from Binary files Copyright © 2008 Xiaoyan Li.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L08 (Chapter 18) Binary I/O.
Chapter 12 File Input and Output. Topics Stream Classes Files Text Input and Output JFileChooser for GUI programs Binary files.
Chapter 16 – Files and Streams. Goals To be able to read and write text files To be able to read and write text files To become familiar with the concepts.
Java File I/O (Continued). File I/O in Java Like every other programming language, Java supports the writing to and reading from different files with.
CS203 Programming with Data Structures Input and Output California State University, Los Angeles.
Files and Streams (part 2) 1 -Based on slides from Deitel & Associates, Inc. - Revised by T. A. Yang.
Handling errors Exception handling and throwing Simple file processing.
Chapter 16 Streams. Chapter Goals To be able to read and write text files To become familiar with the concepts of text and binary formats To learn about.
객체지향프로그래밍강원대학교 1/58 제 14 일 파일과 스트림 (Files and Streams) 제 14 일 목표 텍스트 파일을 읽고 텍스트 파일에 쓰는 법을 배움 텍스트 형식과 이진데이터 형식에 대해 이해하게 됨 순차 파일과 임의접근 파일에 대해 이해하게 됨 직렬화를.
Input / Output Chapter 13.  We use files all the time  Programs are files  Documents are files  We want them to be “permanent”  To last beyond execution.
Prepared by : A.Alzubair Hassan Kassala university Dept. Computer Science Lecture 2 I/O Streams 1.
Streams Reading: 2 nd Ed: , rd Ed: 11.1, 19.1, 19.4
Files and Streams. Midterm exam Time: Wednesday, October 31, 2007 Format: Multiple choices (about 15 to 20 questions) Determine the results of the code.
Chapter 9 1 Chapter 9 – Part 1 l Overview of Streams and File I/O l Text File I/O l Binary File I/O l File Objects and File Names Streams and File I/O.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Working with files. RHS – SOC 2 Motivation All our programs so far have only worked with data stored in primary storage (RAM) Data is lost when program.
Fall 2006Adapted from Java Concepts Companion Slides1 Files and Streams Advanced Programming ICOM 4015 Lecture 16 Reading: Java Concepts Chapter 16.
Object Persistence and Object serialization CSNB534 Asma Shakil.
Two Ways to Store Data in a File  Text format  Binary format.
© Amir Kirsh Files and Streams in Java Written by Amir Kirsh.
CHAPTER 15 STREAMS. CHAPTER GOALS To be able to read and write files To become familiar with the concepts of text and binary files To be able to read.
CIS 270—App Dev II Big Java Chapter 19 Files and Streams.
CSE 501N Fall ‘09 18: Files and Streams 06 November 2009 Nick Leidenfrost.
Fall 2002CS 150: Intro. to Computing1 Streams and File I/O (That is, Input/Output) OR How you read data from files and write data to files.
CS 116 OBJECT ORIENTED PROGRAMMING II LECTURE 11 GEORGE KOUTSOGIANNAKIS Copyright: 2015 / Illinois Institute of Technology/George Koutsogiannakis 1.
Week 12 – Text Files Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.
Java Programming Week 9: Input/Ouput and Exception Handling, Files and Streams (Chapter 11 and Chapter 19)
Object Serialization.  When the data was output to disk, certain information was lost, such as the type of each value.  If the value "3" is read from.
Using the while-statement to process data files. General procedure to access a data file General procedure in computer programming to read data from a.
Outline  Basic IO  File class  The Stream Zoo  Serialization  Regular Expressions.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 19 Binary I/O.
1 Chapter 19 Binary I/O. 2 Motivations F Data stored in a text file – is represented in human-readable form –Text file –Readable –Java source programs.
CS 116 OBJECT ORIENTED PROGRAMMING II LECTURE 12 GEORGE KOUTSOGIANNAKIS Copyright: 2015 Illinois Institute of Technology/ George Koutsogiannakis 1.
Spring 2008 Mark Fontenot CSE Honors Principles of Computer Science I Note Set 20.
Chapter 7 – Arrays and Array Lists Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. 1.
When constructing a two-dimensional array, specify how many rows and columns are needed: final int ROWS = 3; final int COLUMNS = 3; String[][] board =
Files and Serialization. Files Used to transfer data to and from secondary storage.
Java Input / Output l a modular approach to input/output: - different stream objects are connected/wrapped to handle I/O l a data stream object: a flow.
1 CSE 331 Memento Pattern and Serialization slides created by Marty Stepp based on materials by M. Ernst, S. Reges, D. Notkin, R. Mercer, Wikipedia
OBJECT ORIENTED PROGRAMMING II LECTURE 21 GEORGE KOUTSOGIANNAKIS
Chapter 17 Binary I/O.
I/O Basics.
Chapter 17 Binary I/O 1.
File.
CSS161: Fundamentals of Computing
Working with files.
Chapter 16 Streams.
CSE 331 Memento Pattern slides created by Marty Stepp based on materials by M. Ernst, S. Reges, D. Notkin, R. Mercer, Wikipedia
OBJECT ORIENTED PROGRAMMING II LECTURE 22 GEORGE KOUTSOGIANNAKIS
OBJECT ORIENTED PROGRAMMING II LECTURE 20 GEORGE KOUTSOGIANNAKIS
CS 240 – Advanced Programming Concepts
Presentation transcript:

Chapter 20 – Streams and Binary Input/Output Big Java Early Objects by Cay Horstmann Copyright © 2014 by John Wiley & Sons. All rights reserved.

Chapter Goals  To become familiar with text and binary file formats  To learn about encryption  To understand when to use sequential and random file access  To read and write objects using serialization Copyright © 2014 by John Wiley & Sons. All rights reserved. Page 2

Contents  Readers, Writers, and Streams  Binary Input and Output  Random Access  Object Streams Copyright © 2014 by John Wiley & Sons. All rights reserved. Page 3

20.1 Readers, Writers, and Streams  Two ways to store data:  Text format: human-readable form, as a sequence of characters  E.g. Integer 12,345 stored as characters '1' '2' '3' '4' '5'  More convenient for humans: easier to produce input and to check output  Readers and writers handle data in text form  Binary format: data items are represented in bytes  E.g. Integer 12,345 stored as sequence of four bytes  More compact and more efficient  Streams handle binary data Copyright © 2014 by John Wiley & Sons. All rights reserved. Page 4

Java Classes for Input and Output Copyright © 2014 by John Wiley & Sons. All rights reserved. Page 5

Text Data  Reader and Writer and their subclasses were designed to process text input and output  PrintWriter was used in Chapter 7  Scanner class is more convenient than Reader class  By default, these classes use the character encoding of the computer executing the program  OK, when only exchanging data with users from same country  Otherwise, good idea to use UTF-8 encoding: Scanner in = new Scanner(input, "UTF-8"); // Input can be a File or InputStream PrintWriter out = new PrintWriter(output, "UTF-8"); // Output can be a File or OutputStream Copyright © 2014 by John Wiley & Sons. All rights reserved. Page 6

20.2 Binary Input and Output  Use InputStream and OutputStream and their subclasses to process binary input and output  To read: FileInputStream inputStream = new FileInputStream("input.bin");  To write: FileOutputStream outputStream = new FileOutputStream("output.bin");  System.out is a PrintStream object Copyright © 2014 by John Wiley & Sons. All rights reserved. Page 7

Binary Input  Use read method of InputStream class to read a single byte  returns the next byte as an int between 0 and 255  or, the integer -1 at end of file InputStream in =...; int next = in.read(); if (next != -1) { Process next // a value between 0 and 255 } Copyright © 2014 by John Wiley & Sons. All rights reserved. Page 8

Binary Output  Use write method of OutputStream class to write a single byte: OutputStream out =...; int value=...; // should be between 0 and 255 out.write(value);  When finished writing to the file, close it: out.close(); Copyright © 2014 by John Wiley & Sons. All rights reserved. Page 9

An Encryption Program (1)  File encryption: To scramble file so that it is readable only to those who know the encryption method and secret keyword  To use Caesar cipher:  Choose encryption key – a number between 1 and 25 that indicates the shift to be used in encrypting each byte  Example: If the key is 3, replace A with D, B with E,...  To decrypt, use the negative of the encryption key Copyright © 2014 by John Wiley & Sons. All rights reserved. Page 10

An Encryption Program (2)  Encryption: int next = in.read(); if (next == -1) } done = true; } else { int encrypted = encrypt(next); out.write(encrypted); } Copyright © 2014 by John Wiley & Sons. All rights reserved. Page 11

CaesarCipher.java Copyright © 2014 by John Wiley & Sons. All rights reserved. Page 12 Continued 1 import java.io.InputStream; 2 import java.io.OutputStream; 3 import java.io.IOException; 4 5 /** 6 This class encrypts files using the Caesar cipher. 7 For decryption, use an encryptor whose key is the 8 negative of the encryption key. 9 */ 10 public class CaesarCipher 11 { 12 private int key; /** 15 Constructs a cipher object with a given key. aKey the encryption key 17 */ 18 public CaesarCipher(int aKey) 19 { 20 key = aKey; 21 } 22

CaesarCipher.java (cont.) Copyright © 2014 by John Wiley & Sons. All rights reserved. Page 13 Continued 23 /** 24 Encrypts the contents of a stream. in the input stream out the output stream 27 */ 28 public void encryptStream(InputStream in, OutputStream out) 29 throws IOException 30 { 31 boolean done = false; 32 while (!done) 33 { 34 int next = in.read(); 35 if (next == -1) 36 { 37 done = true; 38 } 39 else 40 { 41 int encrypted = encrypt(next); 42 out.write(encrypted); 43 } 44 } 45 } 46

CaesarCipher.java (cont.) Copyright © 2014 by John Wiley & Sons. All rights reserved. Page /** 48 Encrypts a value. b the value to encrypt (between 0 and 255) the encrypted value 51 */ 52 public int encrypt(int b) 53 { 54 return (b + key) % 256; 55 } 56 }

CaesarEncryptor.java Copyright © 2014 by John Wiley & Sons. All rights reserved. Page 15 Continued 1 import java.io.File; 2 import java.io.FileInputStream; 3 import java.io.FileOutputStream; 4 import java.io.InputStream; 5 import java.io.IOException; 6 import java.io.OutputStream; 7 import java.util.Scanner; 8 9 /** 10 This program encrypts a file, using the Caesar cipher. 11 */ 12 public class CaesarEncryptor 13 { 14 public static void main(String[] args) 15 { 16 Scanner in = new Scanner(System.in); 17 try 18 { 19 System.out.print("Input file: "); 20 String inFile = in.next(); 21 System.out.print("Output file: "); 22 String outFile = in.next();

CaesarEncryptor.java (cont.) Copyright © 2014 by John Wiley & Sons. All rights reserved. Page System.out.print("Encryption key: "); 24 int key = in.nextInt(); InputStream inStream = new FileInputStream(inFile); 27 OutputStream outStream = new FileOutputStream(outFile); CaesarCipher cipher = new CaesarCipher(key); 30 cipher.encryptStream(inStream, outStream); inStream.close(); 33 outStream.close(); 34 } 35 catch (IOException exception) 36 { 37 System.out.println("Error processing file: " + exception); 38 } 39 } 40 }

20.3 Random Access  Sequential access: process file one byte at a time  Random access: access file at arbitrary locations  Only disk files support random access System.in and System.out do not  Each disk file has a special file pointer position Read or write at pointer position Copyright © 2014 by John Wiley & Sons. All rights reserved. Page 17

RandomAccessFile Class  Open a file with open mode:  Reading only ( "r" )  Reading and writing ( "rw" ) RandomAccessFile f = new RandomAcessFile("bank.dat","rw");  To move the file pointer to a specific byte: f.seek(position);  To get the current position of the file pointer: long position = f.getFilePointer(); // of type "long" because files can be very large  To find the number of bytes in a file: long fileLength = f.length(); Copyright © 2014 by John Wiley & Sons. All rights reserved. Page 18

Bank Account Program (1)  Use a random access file to store a set of bank accounts  Program lets you pick an account and deposit money into it  To manipulate a data set in a file, pay special attention to data formatting  Suppose we store the data as text Say account 1001 has a balance of $900, and account 1015 has a balance of 0: Want to deposit $100 into account 1001: Writing out the new value: Copyright © 2014 by John Wiley & Sons. All rights reserved. Page 19

Bank Account Program (2)  Better way to manipulate a data set in a file:  Give each value a fixed size that is sufficiently large  Every record has the same size  Easy to skip quickly to a given record  To store numbers, it is easier to store them in binary format Copyright © 2014 by John Wiley & Sons. All rights reserved. Page 20

Bank Account Program (3)  RandomAccessFile class stores binary data  readInt and writeInt methods read/write integers as four-byte quantities  readDouble and writeDouble methods use eight-byte quantities  To find out how many bank accounts are in the file: public int size() throws IOException { return (int) (file.length() / RECORD_SIZE); // RECORD_SIZE is 12 bytes: // 4 bytes for account number plus // 8 bytes for balance } Copyright © 2014 by John Wiley & Sons. All rights reserved. Page 21

Bank Account Program (4)  To read the n th account in the file: public BankAccount read(int n) throws IOException { file.seek(n * RECORD_SIZE); int accountNumber = file.readInt(); double balance = file.readDouble(); return new BankAccount(accountNumber, balance); } Copyright © 2014 by John Wiley & Sons. All rights reserved. Page 22

Bank Account Program (5)  To write the n th account in the file: public void write(int n, BankAccount account) throws IOException { file.seek(n * RECORD_SIZE); file.writeInt(account.getAccountNumber()); file.writeDouble(account.getBalance()); } Copyright © 2014 by John Wiley & Sons. All rights reserved. Page 23

BankSimulator.java Copyright © 2014 by John Wiley & Sons. All rights reserved. Page 24 Continued 1 import java.io.IOException; 2 import java.util.Scanner; 3 4 /** 5 This program demonstrates random access. You can access existing 6 accounts and deposit money, or create new accounts. The 7 accounts are saved in a random access file. 8 */ 9 public class BankSimulator 10 { 11 public static void main(String[] args) throws IOException 12 { 13 Scanner in = new Scanner(System.in); 14 BankData data = new BankData(); 15 try 16 { 17 data.open("bank.dat"); 18

BankSimulator.java (cont.) Copyright © 2014 by John Wiley & Sons. All rights reserved. Page 25 Continued 19 boolean done = false; 20 while (!done) 21 { 22 System.out.print("Account number: "); 23 int accountNumber = in.nextInt(); 24 System.out.print("Amount to deposit: "); 25 double amount = in.nextDouble(); int position = data.find(accountNumber); 28 BankAccount account; 29 if (position >= 0) 30 { 31 account = data.read(position); 32 account.deposit(amount); 33 System.out.println("New balance: " + account.getBalance()); 34 }

BankSimulator.java (cont.) Copyright © 2014 by John Wiley & Sons. All rights reserved. Page else // Add account 36 { 37 account = new BankAccount(accountNumber, amount); 38 position = data.size(); 39 System.out.println("Adding new account."); 40 } 41 data.write(position, account); System.out.print("Done? (Y/N) "); 44 String input = in.next(); 45 if (input.equalsIgnoreCase("Y")) done = true; 46 } 47 } 48 finally 49 { 50 data.close(); 51 } 52 } 53 }

BankData.java Copyright © 2014 by John Wiley & Sons. All rights reserved. Page 27 Continued 1 import java.io.IOException; 2 import java.io.RandomAccessFile; 3 4 /** 5 This class is a conduit to a random access file 6 containing bank account records. 7 */ 8 public class BankData 9 { 10 private RandomAccessFile file; public static final int INT_SIZE = 4; 13 public static final int DOUBLE_SIZE = 8; 14 public static final int RECORD_SIZE = INT_SIZE + DOUBLE_SIZE; /** 17 Constructs a BankData object that is not associated with a file. 18 */ 19 public BankData() 20 { 21 file = null; 22 } 23

BankData.java (cont.) Copyright © 2014 by John Wiley & Sons. All rights reserved. Page 28 Continued 24 /** 25 Opens the data file. filename the name of the file containing bank 27 account information 28 */ 29 public void open(String filename) 30 throws IOException 31 { 32 if (file != null) { file.close(); } 33 file = new RandomAccessFile(filename, "rw"); 34 } /** 37 Gets the number of accounts in the file. the number of accounts 39 */ 40 public int size() 41 throws IOException 42 { 43 return (int) (file.length() / RECORD_SIZE); 44 } 45

BankData.java (cont.) Page 29 Continued 46 /** 47 Closes the data file. 48 */ 49 public void close() 50 throws IOException 51 { 52 if (file != null) { file.close(); } 53 file = null; 54 } /** 57 Reads a bank account record. n the index of the account in the data file a bank account object initialized with the file data 60 */ 61 public BankAccount read(int n) 62 throws IOException 63 { 64 file.seek(n * RECORD_SIZE); 65 int accountNumber = file.readInt(); 66 double balance = file.readDouble(); 67 return new BankAccount(accountNumber, balance); 68 } 69

BankData.java (cont.) Copyright © 2014 by John Wiley & Sons. All rights reserved. Page 30 Continued 70 /** 71 Finds the position of a bank account with a given number accountNumber the number to find the position of the account with the given number, 74 or -1 if there is no such account 75 */ 76 public int find(int accountNumber) 77 throws IOException 78 { 79 for (int i = 0; i < size(); i++) 80 { 81 file.seek(i * RECORD_SIZE); 82 int a = file.readInt(); 83 if (a == accountNumber) {return i; } 84 // Found a match 85 } 86 return -1; // No match in the entire file 87 } 88

BankData.java (cont.) Copyright © 2014 by John Wiley & Sons. All rights reserved. Page /** 90 Writes a bank account record to the data file n the index of the account in the data file account the account to write 93 */ 94 public void write(int n, BankAccount account) 95 throws IOException 96 { 97 file.seek(n * RECORD_SIZE); 98 file.writeInt(account.getAccountNumber()); 99 file.writeDouble(account.getBalance()); 100 } 101 } Continued

BankData.java (cont.) Copyright © 2014 by John Wiley & Sons. All rights reserved. Page 32 Program Run: Account number: 1001 Amount to deposit: 100 Adding new account. Done? (Y/N) N Account number: 1018 Amount to deposit: 200 Adding new account. Done? (Y/N) N Account number: 1001 Amount to deposit: 1000 New balance: Done? (Y/N) Y

20.4 Object Streams  ObjectOutputStream class can save entire objects to disk  ObjectInputStream class can read them back in  Use streams, not writers because objects are saved in binary format Copyright © 2014 by John Wiley & Sons. All rights reserved. Page 33

Writing an Object to File  The object output stream saves all instance variables: BankAccount b =...; ObjectOutputStream out = new ObjectOutputStream( new FileOutputStream("bank.dat")); out.writeObject(b); Copyright © 2011 by John Wiley & Sons. All rights reserved. Page 34

Reading an Object From File  readObject method returns an Object reference  Need to remember the types of the objects that you saved and use a cast: ObjectInputStream in = new ObjectInputStream( new FileInputStream("bank.dat")); BankAccount b =(BankAccount) in.readObject();  readObject method can throw ClassNotFoundException  Checked exception ⇒ you must catch or declare it Copyright © 2014 by John Wiley & Sons. All rights reserved. Page 35

Write and Read Array List  Write: ArrayList a = new ArrayList (); // Now add many BankAccount objects into a out.writeObject(a);  Read: ArrayList a = (ArrayList ) in.readObject(); Copyright © 2014 by John Wiley & Sons. All rights reserved. Page 36

Serializable Interface  Objects that are written to an object stream must belong to a class that implements the Serializable interface: class BankAccount implements Serializable { … }  Serializable interface has no methods  Serialization: Process of saving objects to a stream  Each object is assigned a serial number on the stream  If the same object is saved twice, only serial number is written out the second time  When reading, duplicate serial numbers are restored as references to the same object Copyright © 2014 by John Wiley & Sons. All rights reserved. Page 37

Bank.java Copyright © 2014 by John Wiley & Sons. All rights reserved. Page 38 Continued

Bank.java (cont.) Copyright © 2014 by John Wiley & Sons. All rights reserved. Page 39

SerialDemo.java Copyright © 2014 by John Wiley & Sons. All rights reserved. Page 40 Continued

SerialDemo.java (cont.) Copyright © 2014 by John Wiley & Sons. All rights reserved. Page 41 Continued

SerialDemo.java (cont.) Copyright © 2014 by John Wiley & Sons. All rights reserved. Page 42

Summary: Java Class Hierarchy for Handling Input and Output  Streams access sequences of bytes. Readers and writers access sequences of characters. Copyright © 2014 by John Wiley & Sons. All rights reserved. Page 43

Summary: Input and Output of Binary Data  Use FileInputStream and FileOutputStream classes to read and write binary data from and to disk files.  The InputStream.read method returns an integer, either -1 to indicate end of input, or a byte between 0 and 255.  The OutputStream.write method writes a single byte. Copyright © 2014 by John Wiley & Sons. All rights reserved. Page 44

Summary: Random Access  In sequential file access, a file is processed one byte at a time.  Random access allows access at arbitrary locations in the file, without first reading the bytes preceding the access location.  A file pointer is a position in a random access file. Because files can be very large, the file pointer is of type long.  The RandomAccessFile class reads and writes numbers in binary form. Copyright © 2014 by John Wiley & Sons. All rights reserved. Page 45

Summary: Object Streams  Use object streams to save and restore all instance variables of an object automatically.  Objects saved to an object stream must belong to classes that implement the Serializable interface. Copyright © 2014 by John Wiley & Sons. All rights reserved. Page 46