객체지향프로그래밍강원대학교 1/58 제 14 일 파일과 스트림 (Files and Streams) 제 14 일 목표 텍스트 파일을 읽고 텍스트 파일에 쓰는 법을 배움 텍스트 형식과 이진데이터 형식에 대해 이해하게 됨 순차 파일과 임의접근 파일에 대해 이해하게 됨 직렬화를.

Slides:



Advertisements
Similar presentations
The Package Statement Group related interfaces and classes together Purpose: encapsulation and reduces name conflicts –private package classes not visible.
Advertisements

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.
Graohics CSC 171 FALL 2001 LECTURE 16. History: COBOL Conference on Data System Languages (CODASYL) - led by Joe Wegstein of NBS developed the.
Streams Dwight Deugo Nesa Matic Portions of the notes for this lecture include excerpts from.
III. Streams. Introduction Often a program needs to bring in information from an external source or to send out information to an external destination.
When you use an input or output file that does not exist, what will happen? −The compiler insists that we tell it what the program should do in such case.
Chapter 11.  Data is often stored in files such as a text file  We need to read that data into our program  Simplest mechanism  Scanner class  First.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 12 File Input and Output.
Our BankAccount class should define three methods  deposit  withdraw  getBalance How a programmer will carry out these operations?  We assume that.
©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.
Chapter 11  I/O and Exception Handling 1 Chapter 11 I/O and Exception Handling.
Lecture 7 File I/O (and a little bit about exceptions)‏
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 12 File Input and Output.
Files and Streams CS 21a Chapter 11 of Horstmann.
1 CS2200 Software Development Lecture 36: File Processing A. O’Riordan, 2008 (Includes slides by Lewis/Loftus 2205 and K. Brown )
Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. Chapter Eleven: Input/Ouput and Exception Handling.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 1 Chapter 12 File Input and Output.
Java I/O classes – case study in an OO library Flexible and somewhat slick, but a bit of a mess (though improving)
Streams CSC 171 FALL 2004 LECTURE 22. Make up exam Friday 12/3 11AM-12:10PM.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L08 (Chapter 18) Binary I/O.
7/2/2015CS2621 OO Design and Programming II I/O: Reading and Writing.
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.
Chapter 20 – Streams and Binary Input/Output Big Java Early Objects by Cay Horstmann Copyright © 2014 by John Wiley & Sons. All rights reserved.
Using java’s Scanner class To read from input and from a file. (horstmann ch04 and ch 17)
Chapter 16 – Files and Streams. Announcements Only responsible for 16.1,16.3 Only responsible for 16.1,16.3 Other sections “encouraged” Other sections.
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.
Files and Streams (part 2) 1 -Based on slides from Deitel & Associates, Inc. - Revised by T. A. Yang.
Input/Ouput and Exception Handling. 2 Exceptions  An exception is an object that describes an unusual or erroneous situation  Exceptions are thrown.
Handling errors Exception handling and throwing Simple file processing.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Input/Output and Exception Handling.
Two Ways to Store Data in a File Text format Binary format.
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 Chapter 15 – Files I/O basics File I/O Classes File I/O Basic Operations Text File Output PrintWriter import Statement with a * Text File Input Scanner,
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.
Working with files By the end of this lecture you should be able to: explain the principles of input and output and identify a number of different input.
Prepared by : A.Alzubair Hassan Kassala university Dept. Computer Science Lecture 2 I/O Streams 1.
Chapter 17 Input and Output F Stream Classes F Processing External Files F Data Streams F Print Streams F Buffered Streams  Use JFileChooser F Text Input.
Chapter 15 – Files I/O basics File I/O Classes File I/O Basic Operations Text File Output PrintWriter import Statement with a * Text File Input Scanner,
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.
Two Ways to Store Data in a File  Text format  Binary format.
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.
5-Dec-15 Sequential Files and Streams. 2 File Handling. File Concept.
CS 116 OBJECT ORIENTED PROGRAMMING II LECTURE 11 GEORGE KOUTSOGIANNAKIS Copyright: 2015 / Illinois Institute of Technology/George Koutsogiannakis 1.
Chapter 10 – Input/Output and Exception Handling Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.
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.
CSI 3125, Preliminaries, page 1 Java I/O. CSI 3125, Preliminaries, page 2 Java I/O Java I/O (Input and Output) is used to process the input and produce.
Spring 2008 Mark Fontenot CSE Honors Principles of Computer Science I Note Set 20.
Files and Serialization. Files Used to transfer data to and from secondary storage.
©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e 1 Chapter 13: Streams and Exceptions 1 Chapter 13 Streams and Exceptions.
Recitation File I/O. File I/O File inFile = new File("data.txt"); File inFile = new File (“/Users/sunil/test.txt");
java.io supports console and file I/O
Chapter 17 Input and Output
OO Design and Programming II I/O: Reading and Writing
Object Writing in files
Accessing Files in Java
Chapter 11 – Input/Output and Exception Handling
CSS161: Fundamentals of Computing
Working with files.
Chapter 12 File Input and Output
Chapter 16 Streams.
OBJECT ORIENTED PROGRAMMING II LECTURE 20 GEORGE KOUTSOGIANNAKIS
CS 240 – Advanced Programming Concepts
David Davenport Spring 2005
Presentation transcript:

객체지향프로그래밍강원대학교 1/58 제 14 일 파일과 스트림 (Files and Streams) 제 14 일 목표 텍스트 파일을 읽고 텍스트 파일에 쓰는 법을 배움 텍스트 형식과 이진데이터 형식에 대해 이해하게 됨 순차 파일과 임의접근 파일에 대해 이해하게 됨 직렬화를 통해 객체를 저장하는 법을 배움

객체지향프로그래밍강원대학교 2/58 텍스트 파일을 읽는 법 String in.next String in.nextLine int in.nextInt double in.nextDouble FileReader reader = new FileReader("input.txt"); Scanner in = new Scanner(reader); in.close();

객체지향프로그래밍강원대학교 3/58 텍스트 파일에 쓰는 법 파일이 이미 존재하는 경우 기존 내용이 지워짐 파일이 존재하지 않는 경우 새 파일이 만들어짐 PrintWriter out = new PrintWriter("output.txt"); out.println(29.95); out.println(new Rectangle(5, 10, 15, 25)); out.println("Hello, World!"); out.close();

객체지향프로그래밍강원대학교 4/58 A Sample Program 내 마음은 호수요 그대 저어오오 나는 그대의 흰 그림자를 안고 옥같은 그대의 뱃전에 부서지리라 내 마음은 촛불이요 그대 저 문을 닫아주오 나는 그대의 비단 옷자락에 떨며 최후의 한방울도 남김없이 타오리다 /* 1 */ 내 마음은 호수요 /* 2 */ 그대 저어오오 /* 3 */ 나는 그대의 흰 그림자를 안고 /* 4 */ 옥같은 그대의 뱃전에 부서지리라 /* 5 */ /* 6 */ 내 마음은 촛불이요 ···

객체지향프로그래밍강원대학교 5/58 File LineNumberer.java 01: import java.io.FileReader; 02: import java.io.IOException; 03: import java.io.PrintWriter; 04: import java.util.Scanner; 05: 06: public class LineNumberer 07: { 08: public static void main(String[] args) 09: { 10: Scanner console = new Scanner(System.in); 11: System.out.print("Input file: "); 12: String inputFileName = console.next(); 13: System.out.print("Output file: "); 14: String outputFileName = console.next(); 15: 16: try 17: {

객체지향프로그래밍강원대학교 6/58 File LineNumberer.java 18: FileReader reader = new FileReader(inputFileName); 19: Scanner in = new Scanner(reader); 20: PrintWriter out = new PrintWriter(outputFileName); 21: int lineNumber = 1; 22: 23: while (in.hasNextLine()) 24: { 25: String line = in.nextLine(); 26: out.println("/* " + lineNumber + " */ " + line); 27: lineNumber++; 28: } 29: 30: out.close(); 31: } 32: catch (IOException exception) 33: {

객체지향프로그래밍강원대학교 7/58 File LineNumberer.java 34: System.out.println("Error processing file:" + exception); 35: } 36: } 37: }

객체지향프로그래밍강원대학교 8/58 File Dialog Boxes

객체지향프로그래밍강원대학교 9/58 File Dialog Boxes JFileChooser chooser = new JFileChooser(); FileReader in = null; if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) { File selectedFile = chooser.getSelectedFile(); reader = new FileReader(selectedFile);... } API

객체지향프로그래밍강원대학교 10/58 명령줄 인자 (Command line arguments) java LineNumberer input.txt numbered.txt args[0] 는 “input.txt”, args[1] 은 “numbered.txt” if(args.length >= 1) inputFileName = args[0];

객체지향프로그래밍강원대학교 11/58 텍스트 형식과 이진데이터 형식 (Text and Binary Formats) 데이터를 저장하는 두 가지 방법 – 텍스트 형식 (Text format) – 이진데이터 형식 (Binary format) 텍스트를 한 글자씩 입출력하기 위해서는 –java.io.Reader, java.io.Writer 를 사용 이진데이터를 한 바이트씩 입출력하기 위해서는 –java.io.InputStream, java.io.OutputStream 을 사용

객체지향프로그래밍강원대학교 12/58 텍스트 형식과 이진데이터 형식 (Text and Binary Formats)

객체지향프로그래밍강원대학교 13/58 텍스트 형식 사람이 읽을 수 있는 형식 정수 는 ' 1 ' ' 2 ' ' 3 ' ' 4 ' ' 5 ' 의 문자들로 표현됨 텍스트 형식 데이터의 입출력을 위해서는 Reader 와 Writer 클래스를 사용 FileReader reader = new FileReader("input.txt"); FileWriter writer = new FileWriter("output.txt"); * FileReader, FileWriter 는 각각 Reader 와 Writer 의 서브클래스임

객체지향프로그래밍강원대학교 14/58 텍스트 형식 파일에서 한 글자 읽기 read 메소드 – 다음 문자를 int 타입으로 반환 –end_of_file 의 경우 정수 -1 반환 Reader reader =...; int next = reader.read(); char c; if (next != -1) c = (char) next;

객체지향프로그래밍강원대학교 15/58 텍스트 형식 파일에 한 글자 쓰기 write 메소드 void write(int c) Writes a single character Writer writer =...; char c = ‘a’; writer.write((int)c); // 붉은 색 부분 생략 가능 // 생략하는 경우 자동형변환이 일어남 // 자동형변환 = implicite type conversion // byte -> short -> int -> long -> float -> double // char -> int

객체지향프로그래밍강원대학교 16/58 이진데이터 형식 (Binary Format) 데이터가 이진수 바이트들로 표현됨 정수 는 네 바이트 ( ) 로 나타내짐 (48X256+57=12345)  256=2^8 메모리 공간을 더 효율적으로 사용함 입출력을 위해 InputStream 과 OutputStream 클래 스를 사용함 FileInputStream inputStream = new FileInputStream("input.bin"); FileOutputStream outputStream = new FileOutputStream("output.bin");

객체지향프로그래밍강원대학교 17/58 이진데이터 파일에서 한 바이트 읽기 read 메소드 – 다음 바이트를 int 타입으로 반환 –end_of_file 의 경우 정수 -1 반환 InputStream in =...; int next = in.read(); byte b; if (next != -1) b = (byte) next;

객체지향프로그래밍강원대학교 18/58 암호화 프로그램 (An Encryption Program) Caesar cipher –1 에서 25 사이 숫자 중에서 암호화 키를 선택 – 암호화 키만큼 각 글자를 이동 – 예 : 만약 키가 3 이라면 A 는 D 로, B 는 E 로,...

객체지향프로그래밍강원대학교 19/58 An Encryption Program  암호를 풀기 위해서는 음수의 암호화 키를 사용

객체지향프로그래밍강원대학교 20/58 To Encrypt Binary Data int next = in.read(); if (next == -1) done = true; else { byte b = (byte) next; byte c = encrypt(b); //call the method to encrypt the byte out.write(c); }

객체지향프로그래밍강원대학교 21/58 File Encryptor.java 01: import java.io.File; 02: import java.io.FileInputStream; 03: import java.io.FileOutputStream; 04: import java.io.InputStream; 05: import java.io.OutputStream; 06: import java.io.IOException; 07: 08: /** 09: An encryptor encrypts files using the Caesar cipher. 10: For decryption, use an encryptor whose key is the 11: negative of the encryption key. 12: */ 13: public class Encryptor 14: { 15: /** 16: Constructs an encryptor. aKey the encryption key 18: */

객체지향프로그래밍강원대학교 22/58 File Encryptor.java 19: public Encryptor(int aKey) 20: { 21: key = aKey; 22: } 23: 24: /** 25: Encrypts the contents of a file. inFile the input file outFile the output file 28: */ 29: public void encryptFile(String inFile, String outFile) 30: throws IOException 31: { 32: InputStream in = null; 33: OutputStream out = null; 34: 35: try 36: {

객체지향프로그래밍강원대학교 23/58 File Encryptor.java 37: in = new FileInputStream(inFile); 38: out = new FileOutputStream(outFile); 39: encryptStream(in, out); 40: } 41: finally 42: { 43: if (in != null) in.close(); 44: if (out != null) out.close(); 45: } 46: } 47: 48: /** 49: Encrypts the contents of a stream. in the input stream out the output stream 52: */

객체지향프로그래밍강원대학교 24/58 File Encryptor.java 53: public void encryptStream(InputStream in, OutputStream out) 54: throws IOException 55: { 56: boolean done = false; 57: while (!done) 58: { 59: int next = in.read(); 60: if (next == -1) done = true; 61: else 62: { 63: byte b = (byte) next; 64: byte c = encrypt(b); 65: out.write(c); 66: } 67: } 68: } 69:

객체지향프로그래밍강원대학교 25/58 File Encryptor.java 70: /** 71: Encrypts a byte. b the byte to encrypt the encrypted byte 74: */ 75: public byte encrypt(byte b) 76: { 77: return (byte) (b + key); 78: } 79: 80: private int key; 81: }

객체지향프로그래밍강원대학교 26/58 File EncryptorTester.java 01: import java.io.IOException; 02: import java.util.Scanner; 03: 04: /** 05: A program to test the Caesar cipher encryptor. 06: */ 07: public class EncryptorTester 08: { 09: public static void main(String[] args) 10: { 11: Scanner in = new Scanner(System.in); 12: try 13: { 14: System.out.print("Input file: "); 15: String inFile = in.next(); 16: System.out.print("Output file: "); 17: String outFile = in.next();

객체지향프로그래밍강원대학교 27/58 File EncryptorTester.java 18: System.out.print("Encryption key: "); 19: int key = in.nextInt(); 20: Encryptor crypt = new Encryptor(key); 21: crypt.encryptFile(inFile, outFile); 22: } 23: catch (IOException exception) 24: { 25: System.out.println("Error processing file: " + exception); 26: } 27: } 28: } 29: 30:

객체지향프로그래밍강원대학교 28/58 임의 접근과 순차 접근 (Random Access vs. Sequential Access) file pointer

객체지향프로그래밍강원대학교 29/58 임의접근파일 (RandomAccessFile) 파일을 열 때 모드를 지정함 –Reading only (" r ") –Reading and writing (" rw ") file pointer 를 옮기는 법 RandomAccessFile f = new RandomAcessFile("bank.dat","rw"); f.seek(n);

객체지향프로그래밍강원대학교 30/58 RandomAccessFile file pointer 의 현재 위치를 알아보는 법 파일의 크기를 알아보는 법 ( 바이트 단위로 ) long n = f.getFilePointer(); // of type "long" because files can be very large fileLength = f.length();

객체지향프로그래밍강원대학교 31/58 A Sample Program bank account 들을 임의접근파일에 저장 텍스트 형식으로 저장한다면 ? 1001 번 계좌에 100 원을 저축하면

객체지향프로그래밍강원대학교 32/58 A Sample Program 이진테이터 형식으로 저장한다면 ? – 각 저장소는 고정 길이를 가짐 – 데이터 저장이 간단함 – 특정 계좌 위치를 찾기가 쉬움

객체지향프로그래밍강원대학교 33/58 A Sample Program 계좌정보를 RandomAccessFile 에 저장 ! Account number: readInt and writeInt: 4 bytes Balance: readDouble and writeDouble: 8 bytes 파일에 들어 있는 계좌의 수를 알아내는 법 public int size() throws IOException { return (int) (file.length() / RECORD_SIZE); // RECORD_SIZE is 12 bytes: // 4 bytes for the account number and // 8 bytes for the balance }

객체지향프로그래밍강원대학교 34/58 A Sample Program n 번째 계좌를 읽어 내는 법 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); }

객체지향프로그래밍강원대학교 35/58 A Sample Program n 번째 계좌를 파일에 적어 넣는 법 public void write(int n, BankAccount account) throws IOException { file.seek(n * RECORD_SIZE); file.writeInt(account.getAccountNumber()); file.writeDouble(account.getBalance()); }

객체지향프로그래밍강원대학교 36/58 File BankData.java 001: import java.io.IOException; 002: import java.io.RandomAccessFile; 003: 004: /** 005: This class is a conduit to a random access file 006: containing savings account data. 007: */ 008: public class BankData 009: { 010: /** 011: Constructs a BankData object that is not associated 012: with a file. 013: */ 014: public BankData() 015: { 016: file = null; 017: }

객체지향프로그래밍강원대학교 37/58 File BankData.java 018: 019: /** 020: Opens the data file. filename the name of the file containing savings 022: account information 023: */ 024: public void open(String filename) 025: throws IOException 026: { 027: if (file != null) file.close(); 028: file = new RandomAccessFile(filename, "rw"); 029: } 030: 031: /** 032: Gets the number of accounts in the file. the number of accounts 034: */

객체지향프로그래밍강원대학교 38/58 File BankData.java 035: public int size() 036: throws IOException 037: { 038: return (int) (file.length() / RECORD_SIZE); 039: } 040: 041: /** 042: Closes the data file. 043: */ 044: public void close() 045: throws IOException 046: { 047: if (file != null) file.close(); 048: file = null; 049: } 050:

객체지향프로그래밍강원대학교 39/58 File BankData.java 051: /** 052: Reads a savings account record. n the index of the account in the data file a savings account object initialized with // the file data 055: */ 056: public BankAccount read(int n) 057: throws IOException 058: { 059: file.seek(n * RECORD_SIZE); 060: int accountNumber = file.readInt(); 061: double balance = file.readDouble(); 062: return new BankAccount(accountNumber, balance); 063: } 064: 065: /** 066: Finds the position of a bank account with a given // number

객체지향프로그래밍강원대학교 40/58 File BankData.java accountNumber the number to find the position of the account with the given // number, 069: or -1 if there is no such account 070: */ 071: public int find(int accountNumber) 072: throws IOException 073: { 074: for (int i = 0; i < size(); i++) 075: { 076: file.seek(i * RECORD_SIZE); 077: int a = file.readInt(); 078: if (a == accountNumber) // Found a match 079: return i; 080: } 081: return -1; // No match in the entire file 082: }

객체지향프로그래밍강원대학교 41/58 File BankData.java 083: 084: /** 085: Writes a savings account record to the data file n the index of the account in the data file account the account to write 088: */ 089: public void write(int n, BankAccount account) 090: throws IOException 091: { 092: file.seek(n * RECORD_SIZE); 093: file.writeInt(account.getAccountNumber()); 094: file.writeDouble(account.getBalance()); 095: } 096: 097: private RandomAccessFile file; 098:

객체지향프로그래밍강원대학교 42/58 File BankData.java 099: public static final int INT_SIZE = 4; 100: public static final int DOUBLE_SIZE = 8; 101: public static final int RECORD_SIZE 102: = INT_SIZE + DOUBLE_SIZE; 103: }

객체지향프로그래밍강원대학교 43/58 File BankDatatester.java 01: import java.io.IOException; 02: import java.io.RandomAccessFile; 03: import java.util.Scanner; 04: 05: /** 06: This program tests random access. You can access existing 07: accounts and deposit money, or create new accounts. The 08: accounts are saved in a random access file. 09: */ 10: public class BankDataTester 11: { 12: public static void main(String[] args) 13: throws IOException 14: { 15: Scanner in = new Scanner(System.in); 16: BankData data = new BankData(); 17: try

객체지향프로그래밍강원대학교 44/58 File BankDatatester.java 18: { 19: data.open("bank.dat"); 20: 21: boolean done = false; 22: while (!done) 23: { 24: System.out.print("Account number: "); 25: int accountNumber = in.nextInt(); 26: System.out.print("Amount to deposit: "); 27: double amount = in.nextDouble(); 28: 29: int position = data.find(accountNumber); 30: BankAccount account; 31: if (position >= 0) // 기존 계좌인 경우 32: { 33: account = data.read(position); 34: account.deposit(amount);

객체지향프로그래밍강원대학교 45/58 File BankDatatester.java 35: System.out.println("new balance=" 36: + account.getBalance()); 37: } 38: else // 새 계좌를 추가하는 경우 39: { 40: account = new BankAccount(accountNumber, 41: amount); 42: position = data.size(); 43: System.out.println("adding new account"); 44: } 45: data.write(position, account); 46: 47: System.out.print("Done? (Y/N) "); 48: String input = in.next(); 49: if (input.equalsIgnoreCase("Y")) done = true; 50: } 51: }

객체지향프로그래밍강원대학교 46/58 File BankDatatester.java 52: finally 53: { 54: data.close(); 55: } 56: } 57: } 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69:

객체지향프로그래밍강원대학교 47/58 Output 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

객체지향프로그래밍강원대학교 48/58 객체 스트림 (Object Streams) 객체를 있는 그대로 입출력하기 위해 사용 객체 내부의 데이터를 일일이 저장하고 읽어들이 는 작업을 하지 않아도 됨 객체 전체를 ArrayList 에 넣어 한꺼번에 저장할 수 도 있음 이진데이터 형식으로 저장하므로 stream 을 사용 함

객체지향프로그래밍강원대학교 49/58 BankAccount 객체를 파일에 쓰기 object output stream 은 각 객체의 모든 인스턴스 필드를 저장함 BankAccount b =...; ObjectOutputStream out = new ObjectOutputStream( new FileOutputStream("bank.dat")); out.writeObject(b);

객체지향프로그래밍강원대학교 50/58 BankAccount 객체 읽어오기 readObject 메소드는 Object 타입을 반환함 캐스팅이 필요함 readObject 메소드는 확인예외인 ClassNotFoundException 을 던짐 ObjectInputStream in = new ObjectInputStream( new FileInputStream("bank.dat")); BankAccount b = (BankAccount) in.readObject();

객체지향프로그래밍강원대학교 51/58 객체들을 모아서 저장하기 Write Read ArrayList a = new ArrayList (); // BankAccount 객체들을 a 에 넣음 out.writeObject(a); ArrayList a = (ArrayList ) in.readObject();

객체지향프로그래밍강원대학교 52/58 Serializable Serializable 인터페이스를 구현한 객체만이 object stream 을 통해 입출력될 수 있음 Serializable 인터페이스는 아무런 메소드도 갖지 않음 표준 라이브러리의 많은 클래스들은 Serializable 인터페이스를 구현하고 있음 class BankAccount implements Serializable {... }

객체지향프로그래밍강원대학교 53/58 Serializable 직렬화 (Serialization) – 스트림 안에서 각 객체는 일련번호를 부여받음 – 만약 한 객체가 두 번 저장되면 일련 번호만 두 번 기록됨

객체지향프로그래밍강원대학교 54/58 File Serialtester.java 01: import java.io.File; 02: import java.io.IOException; 03: import java.io.FileInputStream; 04: import java.io.FileOutputStream; 05: import java.io.ObjectInputStream; 06: import java.io.ObjectOutputStream; 07: 08: /** 09: This program tests serialization of a Bank object. 10: If a file with serialized data exists, then it is 11: loaded. Otherwise the program starts with a new bank. 12: Bank accounts are added to the bank. Then the bank 13: object is saved. 14: */ 15: public class SerialTester 16: {

객체지향프로그래밍강원대학교 55/58 File Serialtester.java 17: public static void main(String[] args) 18: throws IOException, ClassNotFoundException 19: { 20: Bank firstBankOfJava; 21: 22: File f = new File("bank.dat"); 23: if (f.exists()) 24: { 25: ObjectInputStream in = new ObjectInputStream 26: (new FileInputStream(f)); 27: firstBankOfJava = (Bank) in.readObject(); 28: in.close(); 29: } 30: else 31: { 32: firstBankOfJava = new Bank(); 33: firstBankOfJava.addAccount(new BankAccount(1001, 20000));

객체지향프로그래밍강원대학교 56/58 File Serialtester.java 34: firstBankOfJava.addAccount(new BankAccount(1015, 10000)); 35: } 36: 37: // Deposit some money 38: BankAccount a = firstBankOfJava.find(1001); 39: a.deposit(100); 40: System.out.println(a.getAccountNumber() + ":" + a.getBalance()); 41: a = firstBankOfJava.find(1015); 42: System.out.println(a.getAccountNumber() + ":" + a.getBalance()); 43: 44: ObjectOutputStream out = new ObjectOutputStream 45: (new FileOutputStream(f)); 46: out.writeObject(firstBankOfJava); 47: out.close(); 48: } 49: }

객체지향프로그래밍강원대학교 57/58 Bank.java public class Bank implements Serializable { public Bank() { accounts = new ArrayList (); } public void addAccount(BankAccount a) public double getTotalBalance() public int count(double atLeast) public BankAccount find(int accountNumber) public BankAccount getMaximum() private ArrayList accounts; }

객체지향프로그래밍강원대학교 58/58 Output First Program Run Second Program Run 1001: : : :