
๋ฐ์ดํฐ์ ์ ๋ ฅ๊ณผ ์ถ๋ ฅ์ ์ฒ๋ฆฌํ๋ ๊ธฐ๋ฅ์ ๋ด๋นํ๋ฉฐ, ํ์ผ, ๋คํธ์ํฌ, ๋ฉ๋ชจ๋ฆฌ ๋ฑ ๋ค์ํ ๋ฐ์ดํฐ ์์ค์์ ๋ฐ์ดํฐ ์ก์์ ์ ๊ด๋ฆฌํจ.
๋ฐ์ดํฐ๊ฐ ๋จ๋ฐฉํฅ์ผ๋ก ํ๋ฅด๋ ์ฐ์์ ์ธ ๋ฐ์ดํฐ ํ๋ฆ
๋ฐ์ดํฐ์ ํ๋ฆ์ ์ถ์ํํ ๊ฒ์ผ๋ก ๋ฐ์ดํฐ์ ์ผ๊ด๋ ์ ์ถ๋ ฅ ์ฒ๋ฆฌ๋ฅผ ๊ฐ๋ฅํ๊ฒ ํจ.
๋ฐ์ดํฐ๋ฅผ ์ฝ๊ณ ์ฐ๊ธฐ ์ํด์๋ ๊ฐ๊ฐ ๋ค๋ฅธ ์คํธ๋ฆผ์ด ํ์ํจ.
๋ฉ๋ชจ๋ฆฌ, ํ์ผ, ๋คํธ์ํฌ ๋ฑ ์ด๋์์ ์ค๋ ๋ฐ์ดํฐ์ด๋ ๋์ผํ ๋ฐฉ์์ผ๋ก ์ฒ๋ฆฌํ ์ ์์.
๋ฐ์ดํธ ์คํธ๋ฆผ ๊ณผ ๋ฌธ์ ์คํธ๋ฆผ ์ผ๋ก ๋๋จ.
์ฌ๋์ด ์ฝ์ ์ ์๋ ๋ฐ์ดํฐ(๋์์, ์ด๋ฏธ์ง ๋ฑ)์ผ ๋ ์ฌ์ฉ
InputStream ๊ณผ OutputStream ์ ํ์ ํด๋์ค โ๋ฐ์ด๋๋ฆฌ ๋ฐ์ดํฐ ์ฒ๋ฆฌํ๋ ๊ฒฝ์ฐ
โก๏ธ FileInputStream ์ด๋ FileOutputStream ๊ณผ ๊ฐ์ ๋ฐ์ดํธ ์คํธ๋ฆผ ์ฌ์ฉ
์ฌ๋์ด ์ฝ์ ์ ์๋ ๋ฐ์ดํฐ(html, json ํ์ผ ๋ฑ)์ผ ๋ ์ฌ์ฉ
Reader ์ Writer ์ ํ์ ํด๋์ค โํ
์คํธ ๋ฐ์ดํฐ ์ฒ๋ฆฌํ๋ ๊ฒฝ์ฐ
โก๏ธ FileReader ๋ FileWriter ์ ๊ฐ์ ๋ฌธ์ ์คํธ๋ฆผ ์ฌ์ฉ
InputStream ๊ณผ OutputStream ์ ์๋ธ ํด๋์ค๋ก, ํ์ผ์์ ๋ฐ์ดํธ๋ฅผ ์ฝ๊ฑฐ๋ ์ฐ๋๋ฐ ์ฌ์ฉย Reader ์ Writer ์ ์๋ธ ํด๋์ค๋ก, ํ์ผ์์ ๋ฌธ์๋ฅผ ์ฝ๊ฑฐ๋ ์ฐ๋๋ฐ ์ฌ์ฉย ๋์คํฌ์ ํ์ผ์ด๋ ๋๋ ํ ๋ฆฌ๋ฅผ ํํํ๋ฉฐ ์ ์ถ๋ ฅ ๊ธฐ๋ฅ์ ์์
java.io.RandomAccessFile ํด๋์ค๋ ์
์ถ๋ ฅ ์คํธ๋ฆผ ์ค ์ ์ผํ๊ฒ ํ์ผ์ ๋ํ ์
๋ ฅ๊ณผ ์ถ๋ ฅ์ ๋์์ ํ ์ ์์.FileInputStream, FileOutputStream, FileReader, FileWriter ๋ฑ์ ํด๋์ค ์ ๊ณต import java.io.*;
public class ExampleIO {
public static void main(String[] args) {
try {
// ํ์ผ์์ ๋ฐ์ดํฐ ์ฝ๊ธฐ
FileInputStream fis = new FileInputStream("example.txt");
int i;
while ((i = fis.read()) != -1) { // -1 -> ํ์ผ์ ๋
System.out.println((char) i);
}
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
// ํ์ผ์์ ๋ฐ์ดํฐ ์ฐ๊ธฐ
FileOutputStream fos = new FileOutputStream("example.txt");
String str = "Hello, world";
fos.write(str.getBytes());
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
๋ฐ์ดํฐ๋ฅผ ์ผ์์ ์ผ๋ก ๋ด์๋๋ ์ญํ
BufferedInuptStream, BufferedOutputStream, BufferedReader, BufferedWriter ๋ฑ์ ํด๋์ค๋ค์ด ๋ฒํผ๋ง์ ์ ๊ณตํจimport java.io.*;
public class BufferedReaderExample {
public static void main(String[] args) {
try (BufferedReader reader = new BufferedReader(new FileReader("input.txt"))) {
String line;
while ((line = reader.readline()) != null) {
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
DataInputStream, DataOutputStream ํด๋์ค๋ฅผ ํตํด ๋ฐ์ดํฐ ํ์
์ ๊ฐ์ ํธ๋ฆฌํ๊ฒ ์ฝ๊ณ ์ธ ์ ์์. import java.io.*;
public class DataStreamExample {
public static void main(String[] args) {
try (DataOutputStream dos = new DataOutputStream(new FileOutputStream("data.bin"))) {
dos.write(10);
dos.writeDouble(3.14);
dos.writeUTF("Hello, world!");
} catch (IOException e) {
e.printStackTrace();
}
try (DataInputStream dis = new DataInputStream(new FileInputStream("data.bin"))) {
int i = dis.readInt();
double d = dis.readDouble();
String s = dis.readUTF();
System.out.println("Read values: " + i + ", " + s);
} catch (IOException e) {
e.printStackTrace();
}
}
}
๊ฐ์ฒด๋ฅผ ๋ฐ์ดํธ ์คํธ๋ฆผ์ผ๋ก ๋ณํํ๋ ๊ณผ์
ObjectOutputStream, ObjectInputStream ํด๋์ค๋ฅผ ์ฌ์ฉํด ๊ฐ์ฒด ์ง๋ ฌํ์ ์ญ์ง๋ ฌํ๋ฅผ ์ํํ ์ ์์. import java.io.*;
class Student implements Serializable {
String name;
int age;
Student(String name, int age) {
this.name = name;
this.age = age;
}
}
public class ObjectStreamExample {
public static void main(String[] args) {
Student s = new Student("John", 20);
try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("student.obj"))) {
oos.writeObject(s);
} catch (IOException e) {
e.printStackTrace();
}
try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream("student.obj"))) {
Student loaded = (Student) ois.readObject();
System.out.println("Loaded student: " + loaded.name + ", " + loaded.age);
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
}
}
}
๐๐ผ ์๋ฐ์ ์ ์ถ๋ ฅ ์ฒ๋ฆฌ๋ ๋ค์ํ ์ด์ ๋ก ์์ธ๊ฐ ๋ฐ์ํ ์ ์์
IOException ํด๋์ค์ ํ์ ํด๋์ค์ IOException ์ ์ฒ๋ฆฌํ๋ฉด ๋๋ถ๋ถ์ ์
์ถ๋ ฅ ์์ธ๋ฅผ ์ฒ๋ฆฌํ ์ ์์ try {
FileInputStream fis = new FileInputStream("non_existent_file.txt");
} catch (FileNotFoundException e) {
System.out.println("File not found!");
}
IOException, FileNotFoundException, EOFException ๋ฑ์ด ์์ import java.io.*;
public class ExceptionExample {
public static void main(String[] args) {
try {
FileInputStream fis = new FileInputStream("input.txt");
DataInputStream dis = new DataInputStream(fis);
while (true) {
int num = dis.readInt();
System.out.println(num);
}
} catch (FileNotFoundException e) {
System.out.println("ํด๋น ํ์ผ์ ์ฐพ์ ์ ์์ต๋๋ค." + e.getMessage());
} catch (EOFException e) {
System.out.println("ํ์ผ ๋์ ๋๋ฌํ์ต๋๋ค.");
} catch (IOException e) {
System.out.println("ํ์ผ ์ฝ๊ธฐ์ ์คํจํ์ต๋๋ค." + e.getMessage());
}
}
}