Search

์ž…์ถœ๋ ฅ ์ŠคํŠธ๋ฆผ

์ž…์ถœ๋ ฅ ์ŠคํŠธ๋ฆผ

โ€ข
์ž…์ถœ๋ ฅ ์ŠคํŠธ๋ฆผ(IO Stream)
โ—ฆ
๋ฐ”์ดํŠธ ์ŠคํŠธ๋ฆผ (Byte Stream)
โ—ฆ
๋ฌธ์ž ์ŠคํŠธ๋ฆผ (Character Stream)
โ€ข
ํ‘œ์ค€ ์ž…์ถœ๋ ฅ (Standard I/O)
โ€ข
ํŒŒ์ผ ์ž…์ถœ๋ ฅ
โ—ฆ
FileInputStream
โ—ฆ
FileOutputStream
โ—ฆ
File
โ€ข
์ง๋ ฌํ™” (Serialization)
โ—ฆ
SerialVersionUID

์ž…์ถœ๋ ฅ ์ŠคํŠธ๋ฆผ(IO Stream)

๋ฐ์ดํ„ฐ์˜ ์ž…๋ ฅ ๋ฐ ์ถœ๋ ฅ์˜ ํ๋ฆ„์„ ๋‹ค๋ฅด๋Š” ๊ฐœ๋…
โ€ข
๋ฐ”์ดํŠธ ์ŠคํŠธ๋ฆผ (Byte Stream)
โ€ข
๋ฌธ์ž ์ŠคํŠธ๋ฆผ (Character Stream)
์ŠคํŠธ๋ฆผ(Stream) : โ€œํ๋ฆ„โ€ ๋ฌผ๋ฆฌ์  ๋˜๋Š” ๊ฐ€์ƒ์˜ ์žฅ์น˜๋กœ๋ถ€ํ„ฐ ๋ฐ์ดํ„ฐ๊ฐ€ ํ˜๋Ÿฌ๊ฐ€๋Š” ํ†ต๋กœ ํ”„๋กœ๊ทธ๋žจ(SW) ์ปดํ“จํ„ฐ(HW) ์‚ฌ์ด์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์ฃผ๊ณ  ๋ฐ›๋Š” ํ๋ฆ„

๋ฐ”์ดํŠธ ์ŠคํŠธ๋ฆผ (Byte Stream)

๋ฐ์ดํ„ฐ๋ฅผ Byte ๋‹จ์œ„๋กœ ์ฒ˜๋ฆฌํ•˜๋Š” ๋ฐ์ดํ„ฐ ํ๋ฆ„
ํด๋ž˜์Šค
์„ค๋ช…
InputStream
๋ฐ”์ดํŠธ ์ž…๋ ฅ ์ŠคํŠธ๋ฆผ์„ ์œ„ํ•œ ์ถ”์ƒ ํด๋ž˜์Šค.
FileInputStream
ํŒŒ์ผ๋กœ๋ถ€ํ„ฐ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ”์ดํŠธ ๋‹จ์œ„๋กœ ์ฝ์–ด๋“ค์ด๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ.
BufferedInputStream
์ž…๋ ฅ ์„ฑ๋Šฅ์„ ํ–ฅ์ƒ์‹œํ‚ค๊ธฐ ์œ„ํ•ด ๋‚ด๋ถ€ ๋ฒ„ํผ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋ฐ”์ดํŠธ ์ž…๋ ฅ ์ŠคํŠธ๋ฆผ.
DataInputStream
๊ธฐ๋ณธ ๋ฐ์ดํ„ฐ ํƒ€์ž…(int, char ๋“ฑ)์„ ๋ฐ”์ดํŠธ ์ŠคํŠธ๋ฆผ์œผ๋กœ๋ถ€ํ„ฐ ์ฝ๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ.
OutputStream
๋ฐ”์ดํŠธ ์ถœ๋ ฅ ์ŠคํŠธ๋ฆผ์„ ์œ„ํ•œ ์ถ”์ƒ ํด๋ž˜์Šค.
FileOutputStream
ํŒŒ์ผ์— ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ”์ดํŠธ ๋‹จ์œ„๋กœ ์“ฐ๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ.
BufferedOutputStream
์ถœ๋ ฅ ์„ฑ๋Šฅ์„ ํ–ฅ์ƒ์‹œํ‚ค๊ธฐ ์œ„ํ•ด ๋‚ด๋ถ€ ๋ฒ„ํผ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋ฐ”์ดํŠธ ์ถœ๋ ฅ ์ŠคํŠธ๋ฆผ.
DataOutputStream
๊ธฐ๋ณธ ๋ฐ์ดํ„ฐ ํƒ€์ž…(int, char ๋“ฑ)์„ ๋ฐ”์ดํŠธ ์ŠคํŠธ๋ฆผ์œผ๋กœ ์ถœ๋ ฅํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ.

๋ฌธ์ž ์ŠคํŠธ๋ฆผ (Character Stream)

๋ฐ์ดํ„ฐ๋ฅผ ๋ฌธ์ž ๋‹จ์œ„๋กœ ์ฒ˜๋ฆฌํ•˜๋Š” ๋ฐ์ดํ„ฐ ํ๋ฆ„
ํด๋ž˜์Šค
์„ค๋ช…
Reader
๋ฌธ์ž ์ž…๋ ฅ ์ŠคํŠธ๋ฆผ์„ ์œ„ํ•œ ์ถ”์ƒ ํด๋ž˜์Šค.
FileReader
ํŒŒ์ผ๋กœ๋ถ€ํ„ฐ ๋ฌธ์ž๋ฅผ ์ฝ์„ ๋•Œ ์‚ฌ์šฉ.
BufferedReader
์ž…๋ ฅ ์„ฑ๋Šฅ์„ ํ–ฅ์ƒ์‹œํ‚ค๊ธฐ ์œ„ํ•ด ๋‚ด๋ถ€ ๋ฒ„ํผ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋ฌธ์ž ์ž…๋ ฅ ์ŠคํŠธ๋ฆผ.
InputStreamReader
๋ฐ”์ดํŠธ ์ŠคํŠธ๋ฆผ์„ ๋ฌธ์ž ์ŠคํŠธ๋ฆผ์œผ๋กœ ๋ณ€ํ™˜ํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ.
Writer
๋ฌธ์ž ์ถœ๋ ฅ ์ŠคํŠธ๋ฆผ์„ ์œ„ํ•œ ์ถ”์ƒ ํด๋ž˜์Šค.
FileWriter
ํŒŒ์ผ์— ๋ฌธ์ž๋ฅผ ์“ธ ๋•Œ ์‚ฌ์šฉ.
BufferedWriter
์ถœ๋ ฅ ์„ฑ๋Šฅ์„ ํ–ฅ์ƒ์‹œํ‚ค๊ธฐ ์œ„ํ•ด ๋‚ด๋ถ€ ๋ฒ„ํผ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋ฌธ์ž ์ถœ๋ ฅ ์ŠคํŠธ๋ฆผ.
OutputStreamWriter
๋ฐ”์ดํŠธ ์ŠคํŠธ๋ฆผ์„ ๋ฌธ์ž ์ŠคํŠธ๋ฆผ์œผ๋กœ ๋ณ€ํ™˜ํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ.

ํ‘œ์ค€ ์ž…์ถœ๋ ฅ (Standard I/O)

ํ‚ค๋ณด๋“œ๋ฅผ ํ†ตํ•ด ์ž…๋ ฅ๋ฐ›๊ณ , ๋ชจ๋‹ˆํ„ฐ์— ์ถœ๋ ฅํ•˜๋Š” ๊ธฐ๋ณธ์ ์€ ์ž…์ถœ๋ ฅ
ํ‘œ์ค€ ์ŠคํŠธ๋ฆผ
์„ค๋ช…
ํƒ€์ž…
ํ‘œ์ค€ ์ž…๋ ฅ (System.in)
๊ธฐ๋ณธ์ ์œผ๋กœ InputStream ํƒ€์ž…์ž…๋‹ˆ๋‹ค. Scanner ๋˜๋Š” InputStreamReader๋ฅผ ํ†ตํ•ด ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
InputStream
ํ‘œ์ค€ ์ถœ๋ ฅ (System.out)
๊ธฐ๋ณธ์ ์œผ๋กœ PrintStream ํƒ€์ž…์ž…๋‹ˆ๋‹ค.
PrintStream
ํ‘œ์ค€ ์˜ค๋ฅ˜ (System.err)
๊ธฐ๋ณธ์ ์œผ๋กœ PrintStream ํƒ€์ž…์œผ๋กœ, ์˜ค๋ฅ˜ ๋ฉ”์‹œ์ง€๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.
PrintStream

ํ‘œ์ค€ ์ž…๋ ฅ ์˜ˆ์‹œ์ฝ”๋“œ

Scanner scanner = new Scanner(System.in); String name = scanner.nextLine(); // ์‚ฌ์šฉ์ž๋กœ๋ถ€ํ„ฐ ํ•œ ์ค„ ์ž…๋ ฅ ๋ฐ›๊ธฐ
Java
๋ณต์‚ฌ

ํ‘œ์ค€ ์ถœ๋ ฅ ์˜ˆ์‹œ์ฝ”๋“œ

System.out.println("Aloha, World~!");
Java
๋ณต์‚ฌ

ํŒŒ์ผ ์ž…์ถœ๋ ฅ

File ํด๋ž˜์Šค์™€ ํ•จ๊ป˜ ํŒŒ์ผ ์‹œ์Šคํ…œ์—์„œ ํ”„๋กœ๊ทธ๋žจ์œผ๋กœ ํŒŒ์ผ์„ ์ž…๋ ฅ๋ฐ›๊ณ , ๋ฐ˜๋Œ€๋กœ ํ”„๋กœ๊ทธ๋žจ์—์„œ ํŒŒ์ผ ์‹œ์Šคํ…œ์„ ํŒŒ์ผ์„ ์ถœ๋ ฅํ•˜๋Š” ๋ฐฉ๋ฒ•
FileInputStream, FileOutputStream, FileReader, FileWriter ๋“ฑ์ด ์ฃผ๋กœ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.

FileInputStream

ํŒŒ์ผ์„ ์—ด์–ด ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ”์ดํŠธ ๋‹จ์œ„๋กœ ์ž…๋ ฅํ•˜๋Š” ํด๋ž˜์Šค

FileInputStream ์ฃผ์š” ๋ฉ”์„œ๋“œ

๋ฉ”์„œ๋“œ
์„ค๋ช…
๋ฐ˜ํ™˜ ํƒ€์ž…
int read()
ํŒŒ์ผ์—์„œ ๋‹ค์Œ ๋ฐ”์ดํŠธ๋ฅผ ์ฝ์–ด ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ๋” ์ด์ƒ ์ฝ์„ ๋ฐ”์ดํŠธ๊ฐ€ ์—†์œผ๋ฉด -1์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
int
int read(byte[] b)
์ฃผ์–ด์ง„ ๋ฐ”์ดํŠธ ๋ฐฐ์—ด์— ํŒŒ์ผ์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์ฝ์–ด ๋“ค์ž…๋‹ˆ๋‹ค. ๋ฐ˜ํ™˜๊ฐ’์€ ์ฝ์€ ๋ฐ”์ดํŠธ ์ˆ˜์ž…๋‹ˆ๋‹ค.
int
void close()
์ŠคํŠธ๋ฆผ์„ ๋‹ซ์•„ ํŒŒ์ผ์„ ํ•ด์ œํ•ฉ๋‹ˆ๋‹ค.
void
long skip(long n)
ํŒŒ์ผ์˜ ํ˜„์žฌ ์œ„์น˜์—์„œ n ๋ฐ”์ดํŠธ๋ฅผ ๊ฑด๋„ˆ๋œ๋‹ˆ๋‹ค.
long
int available()
ํ˜„์žฌ ์ฝ์„ ์ˆ˜ ์žˆ๋Š” ๋ฐ”์ดํŠธ ์ˆ˜๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
int

FileOutputStream

ํŒŒ์ผ์— ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ”์ดํŠธ ๋‹จ์œ„๋กœ ์“ฐ๊ธฐ ์œ„ํ•œ ํด๋ž˜์Šค

FileOutputStream ์ฃผ์š” ๋ฉ”์„œ๋“œ

๋ฉ”์„œ๋“œ
์„ค๋ช…
๋ฐ˜ํ™˜ ํƒ€์ž…
void write(int b)
์ง€์ •๋œ ๋ฐ”์ดํŠธ๋ฅผ ํŒŒ์ผ์— ์”๋‹ˆ๋‹ค.
void
void write(byte[] b)
๋ฐ”์ดํŠธ ๋ฐฐ์—ด์˜ ๋ฐ์ดํ„ฐ๋ฅผ ํŒŒ์ผ์— ์”๋‹ˆ๋‹ค.
void
void write(byte[] b, int off, int len)
๋ฐ”์ดํŠธ ๋ฐฐ์—ด์˜ ํŠน์ • ๋ถ€๋ถ„์„ ํŒŒ์ผ์— ์”๋‹ˆ๋‹ค. off๋Š” ์‹œ์ž‘ ์ธ๋ฑ์Šค, len์€ ๊ธธ์ด์ž…๋‹ˆ๋‹ค.
void
void close()
์ŠคํŠธ๋ฆผ์„ ๋‹ซ์•„ ํŒŒ์ผ์„ ํ•ด์ œํ•ฉ๋‹ˆ๋‹ค.
void
void flush()
์ŠคํŠธ๋ฆผ์— ๋ฒ„ํผ๋ง๋œ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ•์ œ๋กœ ํŒŒ์ผ์— ์”๋‹ˆ๋‹ค.
void

File

ํŒŒ์ผ์ด๋‚˜ ๋””๋ ‰ํ† ๋ฆฌ๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ํด๋ž˜์Šค
โ€ข
File ๊ฐ์ฒด๋Š” ํŒŒ์ผ์ด๋‚˜ ๋””๋ ‰ํ† ๋ฆฌ์— ๋Œ€ํ•œ ๊ฒฝ๋กœ๋ฅผ ๋‚˜ํƒ€๋‚ด๋ฉฐ, ํŒŒ์ผ์„ ์ƒ์„ฑ, ์‚ญ์ œ, ํ™•์ธ, ํŒŒ์ผ ์†์„ฑ ์ฝ๊ธฐ ๋“ฑ์˜ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
โ€ข
ํŒŒ์ผ ์ฝ๊ธฐ/์“ฐ๊ธฐ๋Š” File ๊ฐ์ฒด๋ฅผ ์ด์šฉํ•˜์—ฌ ์ŠคํŠธ๋ฆผ์„ ์—ด์–ด ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค.
โ€ข
File ํด๋ž˜์Šค ์ž์ฒด๋กœ๋Š” ํŒŒ์ผ์˜ ๋‚ด์šฉ์— ์ ‘๊ทผํ•  ์ˆ˜ ์—†์œผ๋ฉฐ, ํŒŒ์ผ์˜ ์กด์žฌ ์—ฌ๋ถ€๋ฅผ ํ™•์ธํ•˜๊ฑฐ๋‚˜, ๋””๋ ‰ํ† ๋ฆฌ๋ฅผ ๋‚˜์—ดํ•˜๋Š” ๋“ฑ์˜ ์ž‘์—…์— ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.
File file = new File("ํŒŒ์ผ๊ฒฝ๋กœ");
Java
๋ณต์‚ฌ
File file = new File("ํŒŒ์ผ๊ฒฝ๋กœ", "ํŒŒ์ผ๋ช…");
Java
๋ณต์‚ฌ

์ง๋ ฌํ™” (Serialization)

๊ฐ์ฒด๋ฅผ ์ŠคํŠธ๋ฆผ์„ ํ†ตํ•ด ์ „์†กํ•˜๊ฑฐ๋‚˜ ํŒŒ์ผ๋กœ ์ €์žฅ ์œ„ํ•ด ๋ฐ”์ดํŠธ ํ˜•ํƒœ๋กœ ๋ณ€ํ™˜ํ•˜๋Š” ๊ณผ์ •

์ง๋ ฌํ™” ์˜ˆ์‹œ์ฝ”๋“œ

โ€ข
Person.java
import java.io.Serializable; public class Person implements Serializable { private static final long serialVersionUID = 1L; private String name; private int age; public Person(String name, int age) { this.name = name; this.age = age; } @Override public String toString() { return "Person{name='" + name + "', age=" + age + "}"; } }
Java
๋ณต์‚ฌ
โ€ข
SerializePerson.java
import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectOutputStream; import java.util.Scanner; public class SerializePerson { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); // ์‚ฌ์šฉ์ž๋กœ๋ถ€ํ„ฐ ์ž…๋ ฅ ๋ฐ›๊ธฐ System.out.print("Enter name: "); String name = scanner.nextLine(); System.out.print("Enter age: "); int age = scanner.nextInt(); // Person ๊ฐ์ฒด ์ƒ์„ฑ Person person = new Person(name, age); // ํŒŒ์ผ์— ์ง๋ ฌํ™” try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("Person.dat"))) { oos.writeObject(person); System.out.println("Person object has been serialized and saved to Person.dat"); } catch (IOException e) { e.printStackTrace(); } finally { scanner.close(); } } }
Java
๋ณต์‚ฌ

์—ญ์ง๋ ฌํ™” ์˜ˆ์‹œ์ฝ”๋“œ

import java.io.FileInputStream; import java.io.IOException; import java.io.ObjectInputStream; public class DeserializePerson { public static void main(String[] args) { // ํŒŒ์ผ์—์„œ ์—ญ์ง๋ ฌํ™” try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream("Person.dat"))) { Person person = (Person) ois.readObject(); System.out.println("Person object has been deserialized: " + person); } catch (IOException | ClassNotFoundException e) { e.printStackTrace(); } } }
Java
๋ณต์‚ฌ

SerialVersionUID

ํด๋ž˜์Šค์˜ ๊ตฌ์กฐ ๋ณ€๊ฒฝ ์‹œ, ์ด๋ฏธ ์ง๋ ฌํ™”๋œ ๊ฐ์ฒด์™€ ํด๋ž˜์Šค๊ฐ„์˜ ํ˜ธํ™˜์„ฑ์„ ๊ฒ€์ฆํ•˜๊ธฐ ์œ„ํ•œ ์‹๋ณ„์ž
ํด๋ž˜์Šค์˜ ๋ณ€์ˆ˜๋‚˜ ๋ฉ”์†Œ๋“œ ๋ณ€๊ฒฝ ์‹œ, serialVersionUID ๋ฅผ ๋ณ€๊ฒฝํ•ด์•ผํ•œ๋‹ค.
๋ฒ„์ „ ๋ณ€๊ฒฝ ์—†์ด, ํด๋ž˜์Šค๋ฅผ ๋ณ€๊ฒฝํ•˜๋ฉด ์—ญ์ง๋ ฌํ™” ์‹œ, InvalidClassException์ด ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ๋‹ค.
import java.io.Serializable; public class Person implements Serializable { private static final long serialVersionUID = 1L; // ๊ณ ์œ  ์‹๋ณ„์ž private String name; private int age; public Person(String name, int age) { this.name = name; this.age = age; } // Getter, Setter, toString() ๋“ฑ ํ•„์š”ํ•œ ๋ฉ”์†Œ๋“œ๋“ค }
Java
๋ณต์‚ฌ

๋ณ€๊ฒฝ ์ „

private static final long serialVersionUID = 1L;
Java
๋ณต์‚ฌ
โ€ข
๋ณ€์ˆ˜ ์ถ”๊ฐ€
private String name; private int age; private String address;
Java
๋ณต์‚ฌ

๋ณ€๊ฒฝ ํ›„

private static final long serialVersionUID = 2L;
Java
๋ณต์‚ฌ
์‹œ๋ฆฌ์–ผ๋ฒ„์ „ UID ๋ฅผ ๋ฐ”๊พธ์–ด์ฃผ์—ˆ๋‹ค.