Top Banner
객객객객객객객객객 객객객객객 1/58 객 14 객 객객객 객객객 (Files and Streams) 제 14 제 제제 제제제 제제제 제제 제제제 제제제 제제 제제 제제 제제제 제제제 제제제제제 제제제 제제 제제제제 제 제제 제제제 제제제제 제제제 제제 제제제제 제 제제제제 제제 제제제 제제제제 제제 제제
58

객체지향프로그래밍강원대학교 1/58 제 14 일 파일과 스트림 (Files and Streams) 제 14 일 목표 텍스트 파일을 읽고 텍스트 파일에 쓰는 법을 배움

Dec 27, 2015

Download

Documents

Henry Lester
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: 객체지향프로그래밍강원대학교 1/58 제 14 일 파일과 스트림 (Files and Streams) 제 14 일 목표 텍스트 파일을 읽고 텍스트 파일에 쓰는 법을 배움

객체지향프로그래밍 강원대학교 1/58

제 14 일 파일과 스트림(Files and Streams)

제 14 일 목표•텍스트 파일을 읽고 텍스트 파일에 쓰는 법을 배움•텍스트 형식과 이진데이터 형식에 대해 이해하게 됨•순차 파일과 임의접근 파일에 대해 이해하게 됨•직렬화를 통해 객체를 저장하는 법을 배움

Page 2: 객체지향프로그래밍강원대학교 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();

Page 3: 객체지향프로그래밍강원대학교 1/58 제 14 일 파일과 스트림 (Files and Streams) 제 14 일 목표 텍스트 파일을 읽고 텍스트 파일에 쓰는 법을 배움

객체지향프로그래밍 강원대학교 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();

Page 4: 객체지향프로그래밍강원대학교 1/58 제 14 일 파일과 스트림 (Files and Streams) 제 14 일 목표 텍스트 파일을 읽고 텍스트 파일에 쓰는 법을 배움

객체지향프로그래밍 강원대학교 4/58

A Sample Program내 마음은 호수요 그대 저어오오나는 그대의 흰 그림자를 안고옥같은 그대의 뱃전에 부서지리라

내 마음은 촛불이요그대 저 문을 닫아주오나는 그대의 비단 옷자락에 떨며최후의 한방울도 남김없이 타오리다

/* 1 */ 내 마음은 호수요 /* 2 */ 그대 저어오오/* 3 */ 나는 그대의 흰 그림자를 안고/* 4 */ 옥같은 그대의 뱃전에 부서지리라/* 5 */ /* 6 */ 내 마음은 촛불이요···

Page 5: 객체지향프로그래밍강원대학교 1/58 제 14 일 파일과 스트림 (Files and Streams) 제 14 일 목표 텍스트 파일을 읽고 텍스트 파일에 쓰는 법을 배움

객체지향프로그래밍 강원대학교 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 LineNumberer07: {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: try17: {

Page 6: 객체지향프로그래밍강원대학교 1/58 제 14 일 파일과 스트림 (Files and Streams) 제 14 일 목표 텍스트 파일을 읽고 텍스트 파일에 쓰는 법을 배움

객체지향프로그래밍 강원대학교 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: {

Page 7: 객체지향프로그래밍강원대학교 1/58 제 14 일 파일과 스트림 (Files and Streams) 제 14 일 목표 텍스트 파일을 읽고 텍스트 파일에 쓰는 법을 배움

객체지향프로그래밍 강원대학교 7/58

File LineNumberer.java

34: System.out.println("Error processing file:" + exception);35: }36: }37: }

Page 8: 객체지향프로그래밍강원대학교 1/58 제 14 일 파일과 스트림 (Files and Streams) 제 14 일 목표 텍스트 파일을 읽고 텍스트 파일에 쓰는 법을 배움

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

File Dialog Boxes

Page 9: 객체지향프로그래밍강원대학교 1/58 제 14 일 파일과 스트림 (Files and Streams) 제 14 일 목표 텍스트 파일을 읽고 텍스트 파일에 쓰는 법을 배움

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

Page 10: 객체지향프로그래밍강원대학교 1/58 제 14 일 파일과 스트림 (Files and Streams) 제 14 일 목표 텍스트 파일을 읽고 텍스트 파일에 쓰는 법을 배움

객체지향프로그래밍 강원대학교 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];

Page 11: 객체지향프로그래밍강원대학교 1/58 제 14 일 파일과 스트림 (Files and Streams) 제 14 일 목표 텍스트 파일을 읽고 텍스트 파일에 쓰는 법을 배움

객체지향프로그래밍 강원대학교 11/58

텍스트 형식과 이진데이터 형식(Text and Binary Formats)

• 데이터를 저장하는 두 가지 방법– 텍스트 형식 (Text format)– 이진데이터 형식 (Binary format)

• 텍스트를 한 글자씩 입출력하기 위해서는– java.io.Reader, java.io.Writer 를 사용

• 이진데이터를 한 바이트씩 입출력하기 위해서는– java.io.InputStream,

java.io.OutputStream 을 사용

Page 12: 객체지향프로그래밍강원대학교 1/58 제 14 일 파일과 스트림 (Files and Streams) 제 14 일 목표 텍스트 파일을 읽고 텍스트 파일에 쓰는 법을 배움

객체지향프로그래밍 강원대학교 12/58

텍스트 형식과 이진데이터 형식(Text and Binary Formats)

Page 13: 객체지향프로그래밍강원대학교 1/58 제 14 일 파일과 스트림 (Files and Streams) 제 14 일 목표 텍스트 파일을 읽고 텍스트 파일에 쓰는 법을 배움

객체지향프로그래밍 강원대학교 13/58

텍스트 형식

• 사람이 읽을 수 있는 형식• 정수 12345 는 '1' '2' '3' '4' '5' 의 문자들로

표현됨• 텍스트 형식 데이터의 입출력을 위해서는 Reader 와 Writer 클래스를 사용

FileReader reader = new FileReader("input.txt");

FileWriter writer = new FileWriter("output.txt");

* FileReader, FileWriter 는 각각 Reader 와 Writer 의 서브클래스임

Page 14: 객체지향프로그래밍강원대학교 1/58 제 14 일 파일과 스트림 (Files and Streams) 제 14 일 목표 텍스트 파일을 읽고 텍스트 파일에 쓰는 법을 배움

객체지향프로그래밍 강원대학교 14/58

텍스트 형식 파일에서 한 글자 읽기

• read 메소드– 다음 문자를 int 타입으로 반환 – end_of_file 의 경우 정수 -1 반환

Reader reader = . . .; int next = reader.read(); char c; if (next != -1) c = (char) next;

Page 15: 객체지향프로그래밍강원대학교 1/58 제 14 일 파일과 스트림 (Files and Streams) 제 14 일 목표 텍스트 파일을 읽고 텍스트 파일에 쓰는 법을 배움

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

Page 16: 객체지향프로그래밍강원대학교 1/58 제 14 일 파일과 스트림 (Files and Streams) 제 14 일 목표 텍스트 파일을 읽고 텍스트 파일에 쓰는 법을 배움

객체지향프로그래밍 강원대학교 16/58

이진데이터 형식(Binary Format)

• 데이터가 이진수 바이트들로 표현됨• 정수 12345 는 네 바이트 (0 0 48 57) 로 나타내짐

(48X256+57=12345) 256=2^8• 메모리 공간을 더 효율적으로 사용함

• 입출력을 위해 InputStream 과 OutputStream 클래스를 사용함

FileInputStream inputStream = new FileInputStream("input.bin");

FileOutputStream outputStream = new FileOutputStream("output.bin");

Page 17: 객체지향프로그래밍강원대학교 1/58 제 14 일 파일과 스트림 (Files and Streams) 제 14 일 목표 텍스트 파일을 읽고 텍스트 파일에 쓰는 법을 배움

객체지향프로그래밍 강원대학교 17/58

이진데이터 파일에서 한 바이트 읽기

• read 메소드– 다음 바이트를 int 타입으로 반환 – end_of_file 의 경우 정수 -1 반환

InputStream in = . . .; int next = in.read(); byte b; if (next != -1) b = (byte) next;

Page 18: 객체지향프로그래밍강원대학교 1/58 제 14 일 파일과 스트림 (Files and Streams) 제 14 일 목표 텍스트 파일을 읽고 텍스트 파일에 쓰는 법을 배움

객체지향프로그래밍 강원대학교 18/58

암호화 프로그램(An Encryption Program)

• Caesar cipher – 1 에서 25 사이 숫자 중에서 암호화 키를 선택– 암호화 키만큼 각 글자를 이동 – 예 : 만약 키가 3 이라면 A 는 D 로 , B 는 E

로 , . . .

Page 19: 객체지향프로그래밍강원대학교 1/58 제 14 일 파일과 스트림 (Files and Streams) 제 14 일 목표 텍스트 파일을 읽고 텍스트 파일에 쓰는 법을 배움

객체지향프로그래밍 강원대학교 19/58

An Encryption Program

암호를 풀기 위해서는 음수의 암호화 키를 사용

Page 20: 객체지향프로그래밍강원대학교 1/58 제 14 일 파일과 스트림 (Files and Streams) 제 14 일 목표 텍스트 파일을 읽고 텍스트 파일에 쓰는 법을 배움

객체지향프로그래밍 강원대학교 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); }

Page 21: 객체지향프로그래밍강원대학교 1/58 제 14 일 파일과 스트림 (Files and Streams) 제 14 일 목표 텍스트 파일을 읽고 텍스트 파일에 쓰는 법을 배움

객체지향프로그래밍 강원대학교 21/58

File Encryptor.java01: 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 Encryptor14: {15: /**16: Constructs an encryptor.17: @param aKey the encryption key18: */

Page 22: 객체지향프로그래밍강원대학교 1/58 제 14 일 파일과 스트림 (Files and Streams) 제 14 일 목표 텍스트 파일을 읽고 텍스트 파일에 쓰는 법을 배움

객체지향프로그래밍 강원대학교 22/58

File Encryptor.java19: public Encryptor(int aKey)20: {21: key = aKey;22: }23: 24: /**25: Encrypts the contents of a file.26: @param inFile the input file27: @param outFile the output file28: */29: public void encryptFile(String inFile, String outFile)30: throws IOException31: {32: InputStream in = null;33: OutputStream out = null;34: 35: try36: {

Page 23: 객체지향프로그래밍강원대학교 1/58 제 14 일 파일과 스트림 (Files and Streams) 제 14 일 목표 텍스트 파일을 읽고 텍스트 파일에 쓰는 법을 배움

객체지향프로그래밍 강원대학교 23/58

File Encryptor.java

37: in = new FileInputStream(inFile);38: out = new FileOutputStream(outFile);39: encryptStream(in, out);40: }41: finally42: {43: if (in != null) in.close();44: if (out != null) out.close();45: } 46: }47: 48: /**49: Encrypts the contents of a stream.50: @param in the input stream51: @param out the output stream52: */

Page 24: 객체지향프로그래밍강원대학교 1/58 제 14 일 파일과 스트림 (Files and Streams) 제 14 일 목표 텍스트 파일을 읽고 텍스트 파일에 쓰는 법을 배움

객체지향프로그래밍 강원대학교 24/58

File Encryptor.java

53: public void encryptStream(InputStream in, OutputStream out)54: throws IOException55: {56: boolean done = false;57: while (!done)58: {59: int next = in.read();60: if (next == -1) done = true;61: else62: {63: byte b = (byte) next;64: byte c = encrypt(b);65: out.write(c);66: }67: }68: }69:

Page 25: 객체지향프로그래밍강원대학교 1/58 제 14 일 파일과 스트림 (Files and Streams) 제 14 일 목표 텍스트 파일을 읽고 텍스트 파일에 쓰는 법을 배움

객체지향프로그래밍 강원대학교 25/58

File Encryptor.java

70: /**71: Encrypts a byte.72: @param b the byte to encrypt73: @return the encrypted byte74: */75: public byte encrypt(byte b)76: {77: return (byte) (b + key);78: }79: 80: private int key;81: }

Page 26: 객체지향프로그래밍강원대학교 1/58 제 14 일 파일과 스트림 (Files and Streams) 제 14 일 목표 텍스트 파일을 읽고 텍스트 파일에 쓰는 법을 배움

객체지향프로그래밍 강원대학교 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 EncryptorTester08: { 09: public static void main(String[] args)10: { 11: Scanner in = new Scanner(System.in);12: try13: { 14: System.out.print("Input file: ");15: String inFile = in.next();16: System.out.print("Output file: ");17: String outFile = in.next();

Page 27: 객체지향프로그래밍강원대학교 1/58 제 14 일 파일과 스트림 (Files and Streams) 제 14 일 목표 텍스트 파일을 읽고 텍스트 파일에 쓰는 법을 배움

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

Page 28: 객체지향프로그래밍강원대학교 1/58 제 14 일 파일과 스트림 (Files and Streams) 제 14 일 목표 텍스트 파일을 읽고 텍스트 파일에 쓰는 법을 배움

객체지향프로그래밍 강원대학교 28/58

임의 접근과 순차 접근(Random Access vs. Sequential Access)

file pointer

Page 29: 객체지향프로그래밍강원대학교 1/58 제 14 일 파일과 스트림 (Files and Streams) 제 14 일 목표 텍스트 파일을 읽고 텍스트 파일에 쓰는 법을 배움

객체지향프로그래밍 강원대학교 29/58

임의접근파일(RandomAccessFile)

• 파일을 열 때 모드를 지정함– Reading only ("r") – Reading and writing ("rw")

• file pointer 를 옮기는 법

RandomAccessFile f = new RandomAcessFile("bank.dat","rw");

f.seek(n);

Page 30: 객체지향프로그래밍강원대학교 1/58 제 14 일 파일과 스트림 (Files and Streams) 제 14 일 목표 텍스트 파일을 읽고 텍스트 파일에 쓰는 법을 배움

객체지향프로그래밍 강원대학교 30/58

RandomAccessFile

• file pointer 의 현재 위치를 알아보는 법

• 파일의 크기를 알아보는 법 ( 바이트 단위로 )

long n = f.getFilePointer(); // of type "long" because files can be very large

fileLength = f.length();

Page 31: 객체지향프로그래밍강원대학교 1/58 제 14 일 파일과 스트림 (Files and Streams) 제 14 일 목표 텍스트 파일을 읽고 텍스트 파일에 쓰는 법을 배움

객체지향프로그래밍 강원대학교 31/58

A Sample Program

• bank account 들을 임의접근파일에 저장

• 텍스트 형식으로 저장한다면 ?

• 1001 번 계좌에 100 원을 저축하면

Page 32: 객체지향프로그래밍강원대학교 1/58 제 14 일 파일과 스트림 (Files and Streams) 제 14 일 목표 텍스트 파일을 읽고 텍스트 파일에 쓰는 법을 배움

객체지향프로그래밍 강원대학교 32/58

A Sample Program

• 이진테이터 형식으로 저장한다면 ?– 각 저장소는 고정 길이를 가짐– 데이터 저장이 간단함– 특정 계좌 위치를 찾기가 쉬움

Page 33: 객체지향프로그래밍강원대학교 1/58 제 14 일 파일과 스트림 (Files and Streams) 제 14 일 목표 텍스트 파일을 읽고 텍스트 파일에 쓰는 법을 배움

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

}

Page 34: 객체지향프로그래밍강원대학교 1/58 제 14 일 파일과 스트림 (Files and Streams) 제 14 일 목표 텍스트 파일을 읽고 텍스트 파일에 쓰는 법을 배움

객체지향프로그래밍 강원대학교 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); }

Page 35: 객체지향프로그래밍강원대학교 1/58 제 14 일 파일과 스트림 (Files and Streams) 제 14 일 목표 텍스트 파일을 읽고 텍스트 파일에 쓰는 법을 배움

객체지향프로그래밍 강원대학교 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()); }

Page 36: 객체지향프로그래밍강원대학교 1/58 제 14 일 파일과 스트림 (Files and Streams) 제 14 일 목표 텍스트 파일을 읽고 텍스트 파일에 쓰는 법을 배움

객체지향프로그래밍 강원대학교 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 file006: containing savings account data.007: */008: public class BankData009: {010: /**011: Constructs a BankData object that is not associated012: with a file.013: */014: public BankData()015: {016: file = null;017: }

Page 37: 객체지향프로그래밍강원대학교 1/58 제 14 일 파일과 스트림 (Files and Streams) 제 14 일 목표 텍스트 파일을 읽고 텍스트 파일에 쓰는 법을 배움

객체지향프로그래밍 강원대학교 37/58

File BankData.java018: 019: /**020: Opens the data file.021: @param filename the name of the file containing savings022: account information023: */024: public void open(String filename)025: throws IOException026: {027: if (file != null) file.close();028: file = new RandomAccessFile(filename, "rw");029: }030: 031: /**032: Gets the number of accounts in the file.033: @return the number of accounts034: */

Page 38: 객체지향프로그래밍강원대학교 1/58 제 14 일 파일과 스트림 (Files and Streams) 제 14 일 목표 텍스트 파일을 읽고 텍스트 파일에 쓰는 법을 배움

객체지향프로그래밍 강원대학교 38/58

File BankData.java

035: public int size()036: throws IOException037: {038: return (int) (file.length() / RECORD_SIZE);039: }040: 041: /**042: Closes the data file.043: */044: public void close()045: throws IOException046: {047: if (file != null) file.close();048: file = null;049: }050:

Page 39: 객체지향프로그래밍강원대학교 1/58 제 14 일 파일과 스트림 (Files and Streams) 제 14 일 목표 텍스트 파일을 읽고 텍스트 파일에 쓰는 법을 배움

객체지향프로그래밍 강원대학교 39/58

File BankData.java051: /**052: Reads a savings account record.053: @param n the index of the account in the data file054: @return a savings account object initialized with // the file data055: */056: public BankAccount read(int n)057: throws IOException058: { 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

Page 40: 객체지향프로그래밍강원대학교 1/58 제 14 일 파일과 스트림 (Files and Streams) 제 14 일 목표 텍스트 파일을 읽고 텍스트 파일에 쓰는 법을 배움

객체지향프로그래밍 강원대학교 40/58

File BankData.java067: @param accountNumber the number to find068: @return the position of the account with the given // number, 069: or -1 if there is no such account070: */071: public int find(int accountNumber)072: throws IOException073: {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 match079: return i;080: } 081: return -1; // No match in the entire file082: }

Page 41: 객체지향프로그래밍강원대학교 1/58 제 14 일 파일과 스트림 (Files and Streams) 제 14 일 목표 텍스트 파일을 읽고 텍스트 파일에 쓰는 법을 배움

객체지향프로그래밍 강원대학교 41/58

File BankData.java

083: 084: /**085: Writes a savings account record to the data file086: @param n the index of the account in the data file087: @param account the account to write088: */089: public void write(int n, BankAccount account)090: throws IOException091: { 092: file.seek(n * RECORD_SIZE);093: file.writeInt(account.getAccountNumber());094: file.writeDouble(account.getBalance());095: }096: 097: private RandomAccessFile file;098:

Page 42: 객체지향프로그래밍강원대학교 1/58 제 14 일 파일과 스트림 (Files and Streams) 제 14 일 목표 텍스트 파일을 읽고 텍스트 파일에 쓰는 법을 배움

객체지향프로그래밍 강원대학교 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: }

Page 43: 객체지향프로그래밍강원대학교 1/58 제 14 일 파일과 스트림 (Files and Streams) 제 14 일 목표 텍스트 파일을 읽고 텍스트 파일에 쓰는 법을 배움

객체지향프로그래밍 강원대학교 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 existing07: accounts and deposit money, or create new accounts. The08: accounts are saved in a random access file.09: */10: public class BankDataTester11: { 12: public static void main(String[] args)13: throws IOException14: { 15: Scanner in = new Scanner(System.in);16: BankData data = new BankData();17: try

Page 44: 객체지향프로그래밍강원대학교 1/58 제 14 일 파일과 스트림 (Files and Streams) 제 14 일 목표 텍스트 파일을 읽고 텍스트 파일에 쓰는 법을 배움

객체지향프로그래밍 강원대학교 44/58

File BankDatatester.java18: { 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);

Page 45: 객체지향프로그래밍강원대학교 1/58 제 14 일 파일과 스트림 (Files and Streams) 제 14 일 목표 텍스트 파일을 읽고 텍스트 파일에 쓰는 법을 배움

객체지향프로그래밍 강원대학교 45/58

File BankDatatester.java35: 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: }

Page 46: 객체지향프로그래밍강원대학교 1/58 제 14 일 파일과 스트림 (Files and Streams) 제 14 일 목표 텍스트 파일을 읽고 텍스트 파일에 쓰는 법을 배움

객체지향프로그래밍 강원대학교 46/58

File BankDatatester.java52: finally53: {54: data.close();55: }56: }57: }58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69:

Page 47: 객체지향프로그래밍강원대학교 1/58 제 14 일 파일과 스트림 (Files and Streams) 제 14 일 목표 텍스트 파일을 읽고 텍스트 파일에 쓰는 법을 배움

객체지향프로그래밍 강원대학교 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=1100.0 Done? (Y/N) Y

Page 48: 객체지향프로그래밍강원대학교 1/58 제 14 일 파일과 스트림 (Files and Streams) 제 14 일 목표 텍스트 파일을 읽고 텍스트 파일에 쓰는 법을 배움

객체지향프로그래밍 강원대학교 48/58

객체 스트림(Object Streams)

• 객체를 있는 그대로 입출력하기 위해 사용• 객체 내부의 데이터를 일일이 저장하고

읽어들이는 작업을 하지 않아도 됨• 객체 전체를 ArrayList 에 넣어 한꺼번에 저장할

수도 있음• 이진데이터 형식으로 저장하므로 stream 을

사용함

Page 49: 객체지향프로그래밍강원대학교 1/58 제 14 일 파일과 스트림 (Files and Streams) 제 14 일 목표 텍스트 파일을 읽고 텍스트 파일에 쓰는 법을 배움

객체지향프로그래밍 강원대학교 49/58

BankAccount 객체를 파일에 쓰기

• object output stream 은 각 객체의 모든 인스턴스 필드를 저장함

BankAccount b = . . .; ObjectOutputStream out = new ObjectOutputStream( new FileOutputStream("bank.dat")); out.writeObject(b);

Page 50: 객체지향프로그래밍강원대학교 1/58 제 14 일 파일과 스트림 (Files and Streams) 제 14 일 목표 텍스트 파일을 읽고 텍스트 파일에 쓰는 법을 배움

객체지향프로그래밍 강원대학교 50/58

BankAccount 객체 읽어오기

• readObject 메소드는 Object 타입을 반환함• 캐스팅이 필요함

• readObject 메소드는 확인예외인 ClassNotFoundException 을 던짐

ObjectInputStream in = new ObjectInputStream( new FileInputStream("bank.dat")); BankAccount b = (BankAccount) in.readObject();

Page 51: 객체지향프로그래밍강원대학교 1/58 제 14 일 파일과 스트림 (Files and Streams) 제 14 일 목표 텍스트 파일을 읽고 텍스트 파일에 쓰는 법을 배움

객체지향프로그래밍 강원대학교 51/58

객체들을 모아서 저장하기

• Write

• Read

ArrayList<BankAccount> a = new ArrayList<BankAccount>(); // BankAccount 객체들을 a 에 넣음 out.writeObject(a);

ArrayList<BankAccount> a = (ArrayList<BankAccount>) in.readObject();

Page 52: 객체지향프로그래밍강원대학교 1/58 제 14 일 파일과 스트림 (Files and Streams) 제 14 일 목표 텍스트 파일을 읽고 텍스트 파일에 쓰는 법을 배움

객체지향프로그래밍 강원대학교 52/58

Serializable

• Serializable 인터페이스를 구현한 객체만이 object stream 을 통해 입출력될 수 있음

• Serializable 인터페이스는 아무런 메소드도 갖지 않음

• 표준 라이브러리의 많은 클래스들은 Serializable 인터페이스를 구현하고 있음

class BankAccount implements Serializable { . . . }

Page 53: 객체지향프로그래밍강원대학교 1/58 제 14 일 파일과 스트림 (Files and Streams) 제 14 일 목표 텍스트 파일을 읽고 텍스트 파일에 쓰는 법을 배움

객체지향프로그래밍 강원대학교 53/58

Serializable

• 직렬화 (Serialization)– 스트림 안에서 각 객체는 일련번호를 부여받음– 만약 한 객체가 두 번 저장되면 일련 번호만 두

번 기록됨

Page 54: 객체지향프로그래밍강원대학교 1/58 제 14 일 파일과 스트림 (Files and Streams) 제 14 일 목표 텍스트 파일을 읽고 텍스트 파일에 쓰는 법을 배움

객체지향프로그래밍 강원대학교 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 is11: 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 SerialTester16: {

Page 55: 객체지향프로그래밍강원대학교 1/58 제 14 일 파일과 스트림 (Files and Streams) 제 14 일 목표 텍스트 파일을 읽고 텍스트 파일에 쓰는 법을 배움

객체지향프로그래밍 강원대학교 55/58

File Serialtester.java17: public static void main(String[] args)18: throws IOException, ClassNotFoundException19: { 20: Bank firstBankOfJava;21: 22: File f = new File("bank.dat");23: if (f.exists())24: {25: ObjectInputStream in = new ObjectInputStream26: (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));

Page 56: 객체지향프로그래밍강원대학교 1/58 제 14 일 파일과 스트림 (Files and Streams) 제 14 일 목표 텍스트 파일을 읽고 텍스트 파일에 쓰는 법을 배움

객체지향프로그래밍 강원대학교 56/58

File Serialtester.java34: firstBankOfJava.addAccount(new BankAccount(1015, 10000));35: }36: 37: // Deposit some money38: 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 ObjectOutputStream45: (new FileOutputStream(f));46: out.writeObject(firstBankOfJava);47: out.close();48: }49: }

Page 57: 객체지향프로그래밍강원대학교 1/58 제 14 일 파일과 스트림 (Files and Streams) 제 14 일 목표 텍스트 파일을 읽고 텍스트 파일에 쓰는 법을 배움

객체지향프로그래밍 강원대학교 57/58

Bank.javapublic class Bank implements Serializable{ public Bank() { accounts = new ArrayList<BankAccount>(); } public void addAccount(BankAccount a) public double getTotalBalance() public int count(double atLeast) public BankAccount find(int accountNumber) public BankAccount getMaximum() private ArrayList<BankAccount> accounts;}

Page 58: 객체지향프로그래밍강원대학교 1/58 제 14 일 파일과 스트림 (Files and Streams) 제 14 일 목표 텍스트 파일을 읽고 텍스트 파일에 쓰는 법을 배움

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

Output

First Program Run

Second Program Run

1001:20100.0 1015:10000.0

1001:20200.0 1015:10000.0