๐Ÿ“•์ž๋ฐ” I/O์™€ ์ŠคํŠธ๋ฆผ(Char, Byte, String)

dev_itzel_02โœจยท2024๋…„ 8์›” 31์ผ
post-thumbnail

๐Ÿท๏ธ์ž๋ฐ” I/O

๋ฐ์ดํ„ฐ์˜ ์ž…๋ ฅ๊ณผ ์ถœ๋ ฅ์„ ์ฒ˜๋ฆฌํ•˜๋Š” ๊ธฐ๋Šฅ์„ ๋‹ด๋‹นํ•˜๋ฉฐ, ํŒŒ์ผ, ๋„คํŠธ์›Œํฌ, ๋ฉ”๋ชจ๋ฆฌ ๋“ฑ ๋‹ค์–‘ํ•œ ๋ฐ์ดํ„ฐ ์†Œ์Šค์™€์˜ ๋ฐ์ดํ„ฐ ์†ก์ˆ˜์‹ ์„ ๊ด€๋ฆฌํ•จ.

  • ํ”„๋กœ๊ทธ๋žจ์€ ๋ฐ์ดํ„ฐ๋ฅผ ์ฒ˜๋ฆฌํ•˜๋Š” ๊ณผ์ •์—์„œ ์ž…๋ ฅ๊ณผ ์ถœ๋ ฅ์ด ํ•„์ˆ˜์ ์ธ ๋ถ€๋ถ„์ž„.
  • ์‚ฌ์šฉ์ž๋กœ๋ถ€ํ„ฐ ์ž…๋ ฅ์„ ๋ฐ›์•„ ์ฒ˜๋ฆฌํ•œ ๊ฒฐ๊ณผ๋ฅผ ๋‹ค์‹œ ์ถœ๋ ฅํ•˜๋Š” ๊ฒƒ์€ ํ”„๋กœ๊ทธ๋žจ์˜ ๊ธฐ๋ณธ์ ์ธ ๋™์ž‘์ž„.

๐Ÿท๏ธ์ŠคํŠธ๋ฆผ

๋ฐ์ดํ„ฐ๊ฐ€ ๋‹จ๋ฐฉํ–ฅ์œผ๋กœ ํ๋ฅด๋Š” ์—ฐ์†์ ์ธ ๋ฐ์ดํ„ฐ ํ๋ฆ„

  • ๋ฐ์ดํ„ฐ์˜ ํ๋ฆ„์„ ์ถ”์ƒํ™”ํ•œ ๊ฒƒ์œผ๋กœ ๋ฐ์ดํ„ฐ์˜ ์ผ๊ด€๋œ ์ž…์ถœ๋ ฅ ์ฒ˜๋ฆฌ๋ฅผ ๊ฐ€๋Šฅํ•˜๊ฒŒ ํ•จ.

  • ๋ฐ์ดํ„ฐ๋ฅผ ์ฝ๊ณ  ์“ฐ๊ธฐ ์œ„ํ•ด์„œ๋Š” ๊ฐ๊ฐ ๋‹ค๋ฅธ ์ŠคํŠธ๋ฆผ์ด ํ•„์š”ํ•จ.

  • ๋ฉ”๋ชจ๋ฆฌ, ํŒŒ์ผ, ๋„คํŠธ์›Œํฌ ๋“ฑ ์–ด๋””์—์„œ ์˜ค๋Š” ๋ฐ์ดํ„ฐ์ด๋“  ๋™์ผํ•œ ๋ฐฉ์‹์œผ๋กœ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ์Œ.

  • ๋ฐ”์ดํŠธ ์ŠคํŠธ๋ฆผ ๊ณผ ๋ฌธ์ž ์ŠคํŠธ๋ฆผ ์œผ๋กœ ๋‚˜๋‰จ.

๋ฐ”์ดํŠธ ์ŠคํŠธ๋ฆผ ๐Ÿ†š ๋ฌธ์ž ์ŠคํŠธ๋ฆผ

โ˜๐Ÿผ๋ฐ”์ดํŠธ ์ŠคํŠธ๋ฆผ

์‚ฌ๋žŒ์ด ์ฝ์„ ์ˆ˜ ์—†๋Š” ๋ฐ์ดํ„ฐ(๋™์˜์ƒ, ์ด๋ฏธ์ง€ ๋“ฑ)์ผ ๋•Œ ์‚ฌ์šฉ

  • 8bit๋ฅผ ์ฒ˜๋ฆฌํ•˜๋ฉฐ, ๋ฐ”์ด๋„ˆ๋ฆฌ ๋ฐ์ดํ„ฐ๋ฅผ ์ฒ˜๋ฆฌํ•˜๋Š”๋ฐ ์ ํ•ฉ
  • InputStream ๊ณผ OutputStream ์˜ ํ•˜์œ„ ํด๋ž˜์Šค

โ“๋ฐ”์ด๋„ˆ๋ฆฌ ๋ฐ์ดํ„ฐ ์ฒ˜๋ฆฌํ•˜๋Š” ๊ฒฝ์šฐ
โžก๏ธ FileInputStream ์ด๋‚˜ FileOutputStream ๊ณผ ๊ฐ™์€ ๋ฐ”์ดํŠธ ์ŠคํŠธ๋ฆผ ์‚ฌ์šฉ

โœŒ๐Ÿผ๋ฌธ์ž ์ŠคํŠธ๋ฆผ

์‚ฌ๋žŒ์ด ์ฝ์„ ์ˆ˜ ์žˆ๋Š” ๋ฐ์ดํ„ฐ(html, json ํŒŒ์ผ ๋“ฑ)์ผ ๋•Œ ์‚ฌ์šฉ

  • 16๋น„ํŠธ ์œ ๋‹ˆ์ฝ”๋“œ ๋ฌธ์ž๋ฅผ ์ฒ˜๋ฆฌํ•˜๋ฉฐ, ๋ฌธ์ž ๋ฐ์ดํ„ฐ๋ฅผ ์ฒ˜๋ฆฌํ•˜๋Š”๋ฐ ์ ํ•ฉ
  • Reader ์™€ Writer ์˜ ํ•˜์œ„ ํด๋ž˜์Šค

โ“ํ…์ŠคํŠธ ๋ฐ์ดํ„ฐ ์ฒ˜๋ฆฌํ•˜๋Š” ๊ฒฝ์šฐ
โžก๏ธ FileReader ๋‚˜ FileWriter ์™€ ๊ฐ™์€ ๋ฌธ์ž ์ŠคํŠธ๋ฆผ ์‚ฌ์šฉ


๐Ÿท๏ธํด๋ž˜์Šค ์ข…๋ฅ˜์™€ ๋‚ด์šฉ

>> img ref.

โ˜๐ŸผByte Stream Classes

  1. File ํด๋ž˜์Šค
    • ํŒŒ์ผ ์‹œ์Šคํ…œ์˜ ํŒŒ์ผ์„ ๋‹ค๋ฃจ๋Š” ๋ฐ ์‚ฌ์šฉ
    • ํŒŒ์ผ ์ƒ์„ฑ, ์ด๋ฆ„ ๋ณ€๊ฒฝ, ์‚ญ์ œ, ๊ฒฝ๋กœ ๊ฐ€์ ธ์˜ค๊ธฐ ๋“ฑ์˜ ๊ธฐ๋Šฅ์„ ์ œ๊ณตย 
  1. InputStream / OutputStream ํด๋ž˜์Šค
    • ๋ฐ”์ดํŠธ ๊ธฐ๋ฐ˜ ์ŠคํŠธ๋ฆผ์˜ ์ตœ์ƒ์œ„ ํด๋ž˜์Šค๋กœ, ์ด์ง„ ๋ฐ์ดํ„ฐ๋ฅผ ๋‹ค๋ฃจ๋Š” ๋ฐ ์‚ฌ์šฉย 
  1. FileInputStream / FileOutputStream ํด๋ž˜์Šค
    • InputStream ๊ณผ OutputStream ์˜ ์„œ๋ธŒ ํด๋ž˜์Šค๋กœ, ํŒŒ์ผ์—์„œ ๋ฐ”์ดํŠธ๋ฅผ ์ฝ๊ฑฐ๋‚˜ ์“ฐ๋Š”๋ฐ ์‚ฌ์šฉย 
  1. BufferedInputStream / BufferdOutputStream ํด๋ž˜์Šค
    • ๋ฒ„ํผ๋ฅผ ์ œ๊ณตํ•˜์—ฌ ์ž…์ถœ๋ ฅ ์„ฑ๋Šฅ์„ ํ–ฅ์ƒ์‹œํ‚ด.
    • ํฐ ๋ฐ์ดํ„ฐ ์ฒ˜๋ฆฌํ•  ๋•Œ ์œ ์šฉ ย 

โœŒ๐ŸผCharacter Stream Classes

  1. Reader / Writer ํด๋ž˜์Šค
    • ๋ฌธ์ž ๊ธฐ๋ฐ˜ ์ŠคํŠธ๋ฆผ์˜ ์ตœ์ƒ์œ„ ํด๋ž˜์Šค๋กœ, ํ…์ŠคํŠธ ๋ฐ์ดํ„ฐ๋ฅผ ๋‹ค๋ฃจ๋Š” ๋ฐ ์‚ฌ์šฉย 
  1. FileReader / FileWriter ํด๋ž˜์Šค
    • Reader ์™€ Writer ์˜ ์„œ๋ธŒ ํด๋ž˜์Šค๋กœ, ํŒŒ์ผ์—์„œ ๋ฌธ์ž๋ฅผ ์ฝ๊ฑฐ๋‚˜ ์“ฐ๋Š”๋ฐ ์‚ฌ์šฉย 
  1. BufferedReader / BufferdWriter ํด๋ž˜์Šค
    • ๋ฒ„ํผ๋ฅผ ์ œ๊ณตํ•˜์—ฌ ์ž…์ถœ๋ ฅ ์„ฑ๋Šฅ์„ ํ–ฅ์ƒ์‹œํ‚ด
    • ํฐ ํ…์ŠคํŠธ ๋ฐ์ดํ„ฐ๋ฅผ ์ฒ˜๋ฆฌํ•  ๋•Œ ์œ ์šฉย 
  1. PrintWriter ํด๋ž˜์Šค
    • ํ…์ŠคํŠธ ๋ฐ์ดํ„ฐ๋ฅผ ์“ฐ๋Š” ๋ฐ ์‚ฌ์šฉย 
  1. Scanner ํด๋ž˜์Šค
    • ํ…์ŠคํŠธ ๋ฐ์ดํ„ฐ๋ฅผ ํŒŒ์‹ฑํ•˜๋Š” ๋ฐ ์œ ์šฉํ•œ ๋ฉ”์†Œ๋“œ ์ œ๊ณต

๐Ÿท๏ธFile ํด๋ž˜์Šค

๋””์Šคํฌ์˜ ํŒŒ์ผ์ด๋‚˜ ๋””๋ ‰ํ† ๋ฆฌ๋ฅผ ํ‘œํ˜„ํ•˜๋ฉฐ ์ž…์ถœ๋ ฅ ๊ธฐ๋Šฅ์€ ์—†์Œ

  • ์ฃผ๋กœ ํŒŒ์ผ์ด๋‚˜ ๋””๋ ‰ํ† ๋ฆฌ์˜ ์ •๋ณด๋ฅผ ํ™•์ธํ•˜๊ฑฐ๋‚˜ ์ƒ์„ฑํ•˜๊ฑฐ๋‚˜ ์‚ญ์ œํ•˜๋Š” ๋“ฑ์˜ ์ž‘์—…์— ์‚ฌ์šฉ๋จ
  • java.io.RandomAccessFile ํด๋ž˜์Šค๋Š” ์ž…์ถœ๋ ฅ ์ŠคํŠธ๋ฆผ ์ค‘ ์œ ์ผํ•˜๊ฒŒ ํŒŒ์ผ์— ๋Œ€ํ•œ ์ž…๋ ฅ๊ณผ ์ถœ๋ ฅ์„ ๋™์‹œ์— ํ•  ์ˆ˜ ์žˆ์Œ.
  • ํŒŒ์ผ ํฌ์ธํ„ฐ๋ฅผ ์ด์šฉํ•ด ํŒŒ์ผ์˜ ์ž„์˜์˜ ์œ„์น˜์—์„œ ์ฝ๊ณ  ์“ธ ์ˆ˜ ์žˆ์–ด ๋žœ๋ค ์•ก์„ธ์Šค๋ผ๋Š” ์ด๋ฆ„์ด ๋ถ™์Œ.
  • ํฌ๊ฒŒ ํŒŒ์ผ์—์„œ ์ฝ๊ณ  ์“ฐ๋Š” ์ž‘์—…๊ณผ ํŒŒ์ผ ํฌ์ธํ„ฐ๋ฅผ ์ด๋™์‹œํ‚ค๋Š” ์ž‘์—…์œผ๋กœ ๋‚˜๋‰จ.

๐Ÿท๏ธI/O Read/Write

  • FileInputStream, FileOutputStream, FileReader, FileWriter ๋“ฑ์˜ ํด๋ž˜์Šค ์ œ๊ณต

e.g.

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 ๋“ฑ์˜ ํด๋ž˜์Šค๋“ค์ด ๋ฒ„ํผ๋ง์„ ์ œ๊ณตํ•จ
  • ์œ„์˜ ํด๋ž˜์Šค๋“ค์€ ๋‚ด๋ถ€์ ์œผ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฒ„ํผ์— ์ €์žฅํ•ด ๋‘์—ˆ๋‹ค๊ฐ€ ํ•œ๋ฒˆ์— ์ž…์ถœ๋ ฅ์„ ์ˆ˜ํ–‰ํ•˜์—ฌ ์„ฑ๋Šฅ์„ ํ–ฅ์ƒ์‹œํ‚ด

e.g.

๐Ÿ“‘BufferdReader๋ฅผ ์‚ฌ์šฉํ•ด ํ…์ŠคํŠธ ํŒŒ์ผ์„ ํ•œ ์ค„์”ฉ ์ฝ์–ด์˜ค๋Š” ์ฝ”๋“œ

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 ํด๋ž˜์Šค๋ฅผ ํ†ตํ•ด ๋ฐ์ดํ„ฐ ํƒ€์ž…์˜ ๊ฐ’์„ ํŽธ๋ฆฌํ•˜๊ฒŒ ์ฝ๊ณ  ์“ธ ์ˆ˜ ์žˆ์Œ.

e.g.

๐Ÿ“‘ํด๋ž˜์Šค๋ฅผ ์‚ฌ์šฉํ•ด ๊ธฐ๋ณธ ๋ฐ์ดํ„ฐ ํƒ€์ž… ๊ฐ’์„ํŒŒ์ผ์— ์“ฐ๊ณ  ์ฝ๋Š” ์ฝ”๋“œ

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 ํด๋ž˜์Šค๋ฅผ ์‚ฌ์šฉํ•ด ๊ฐ์ฒด ์ง๋ ฌํ™”์™€ ์—ญ์ง๋ ฌํ™”๋ฅผ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ์Œ.

e.g.

๐Ÿ“‘Student.java

import java.io.*;

class Student implements Serializable {
    String name;
    int age;
    
    Student(String name, int age) {
        this.name = name;
        this.age = age;
    }
}

๐Ÿ“‘"student.obj" ํŒŒ์ผ์— ์ž‘์„ฑํ•˜๊ณ  ์ฝ์–ด์˜ค๋Š” ์ฝ”๋“œ

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();
        }
    }
}

๐Ÿท๏ธI/O ์˜ˆ์™ธ์ฒ˜๋ฆฌ ์ƒํ™ฉ ๋ฐ ๋ฐฉ๋ฒ•

๐Ÿ‘‰๐Ÿผ ์ž๋ฐ”์˜ ์ž…์ถœ๋ ฅ ์ฒ˜๋ฆฌ๋Š” ๋‹ค์–‘ํ•œ ์ด์œ ๋กœ ์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ์Œ

  • ์ž…์ถœ๋ ฅ์—์„œ ๋ฐœ์ƒํ•˜๋Š” ๋Œ€๋ถ€๋ถ„์˜ ์˜ˆ์™ธ๋Š” IOException ํด๋ž˜์Šค์˜ ํ•˜์œ„ ํด๋ž˜์Šค์ž„
  • ๋”ฐ๋ผ์„œ IOException ์„ ์ฒ˜๋ฆฌํ•˜๋ฉด ๋Œ€๋ถ€๋ถ„์˜ ์ž…์ถœ๋ ฅ ์˜ˆ์™ธ๋ฅผ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ์Œ

e.g.

๐Ÿ“‘FileNotFoundException -> ์กด์žฌํ•˜์ง€ ์•Š๋Š” ํŒŒ์ผ์„ ์ฝ์œผ๋ ค๋Š” ๊ฒฝ์šฐ

try {
    FileInputStream fis = new FileInputStream("non_existent_file.txt");
} catch (FileNotFoundException e) {
    System.out.println("File not found!");
}

๐Ÿท๏ธI/O ์˜ˆ์™ธ์ฒ˜๋ฆฌ ์ข…๋ฅ˜

  • ์ž…์ถœ๋ ฅ ์ฒ˜๋ฆฌ์—์„œ ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ๋Š” ์˜ˆ์™ธ์—๋Š” IOException, FileNotFoundException, EOFException ๋“ฑ์ด ์žˆ์Œ

e.g.

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());
        }
    }
}
profile
๐Ÿœ๐Ÿ‘ฃsteadiness๐Ÿœ๐Ÿ‘ฃ

0๊ฐœ์˜ ๋Œ“๊ธ€