5 ๋ถ„ ์†Œ์š”

IO Stream: Byte or Char์˜ ํ๋ฆ„

Input Stream, OutputStream

  • ์ถ”์ƒ ํด๋ž˜์Šค
  • byte ๋‹จ์œ„ ์ž…์ถœ๋ ฅ ํด๋ž˜์Šค๋Š” InputStream, OutputStream์˜ ํ›„์†

Reader, Writer

  • ์ถ”์ƒ ํด๋ž˜์Šค
  • char ๋‹จ์œ„ ์ž…์ถœ๋ ฅ ํด๋ž˜์Šค๋Š” Reader, Writer์˜ ํ›„์†

IO Stream Hierarchy

์ถœ์ฒ˜ - https://java8.info/inputoutput/javaioovw.html

Byte Input Stream Hierarchy
Byte Output Stream Hierarchy
Char Input Stream Hierarchy
Char Output Stream Hierarchy

๋ถ€๊ฐ€ ์„ค๋ช…: InputStream & OutputStream

How to read: InputStream
  • InputStream์€ ๋ฐ”์ดํŠธ ๋‹จ์œ„๋กœ ์ฝ์–ด๋“ค์ธ๋‹ค(read ๋ฉ”์„œ๋“œ).
  • int(4 ๋ฐ”์ดํŠธ) ์ค‘ ๋งˆ์ง€๋ง‰ ํ•œ ๋ฐ”์ดํŠธ๋งŒ์„ ์œ ํšจ ๋ฐ์ดํ„ฐ๋กœ ์‚ฌ์šฉํ•œ๋‹ค.
  • ํŒŒ์ผ์„ read ๋ฉ”์„œ๋“œ๋ฅผ ํ†ตํ•ด ์ฝ์–ด๋“ค์ด๊ฒŒ ๋˜๋ฉด read ๋ฉ”์„œ๋“œ๊ฐ€ ์‚ฌ์šฉ๋  ๋•Œ๋งˆ๋‹ค 1๋ฐ”์ดํŠธ ์”ฉ ํŒŒ์ผ์—์„œ ์ฝ๊ฒŒ ๋œ๋‹ค.
  • byte ๋ฐฐ์—ด์„ ์‚ฌ์šฉํ•˜๋ฉด ์‚ฌ์šฉํ•œ byte ๋ฐฐ์—ด์— ๋”ฐ๋ผ ํ•œ๋ฒˆ์— ์ „๋‹ฌํ•œ๋‹ค. ๋ฐฐ์—ด์ด ํด์ˆ˜๋ก ํ•œ ๋ฒˆ์— ์ฝ๋Š” ์–‘์ด ๋งŽ์•„์ง„๋‹ค.
  • ๊ทธ๋ฆผ ์˜ค๋ฅ˜: read๋ฉ”์„œ๋“œ๋Š” int ๊ฐ’์„ ๋„ฃ์–ด์ฃผ๋Š”๊ฒŒ ์•„๋‹ˆ๋ผ int ๊ฐ’์œผ๋กœ ๋ฐ˜ํ™˜๋œ๋‹ค.
  • InputStream์€ ์ถ”์ƒ ํด๋ž˜์Šค์ด๊ธฐ ๋•Œ๋ฌธ์— ์ž์‹ ๊ฐ์ฒด๋ฅผ ์„ ์–ธํ•˜์—ฌ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค.
How to write: OutputStream
  • read์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ write๋กœ ์ „๋‹ฌ๋˜๋Š” int์˜ ๋งˆ์ง€๋ง‰ ํ•œ ๋ฐ”์ดํŠธ๊ฐ€ ์จ์•ผํ•  ๋Œ€์ƒ์— ์จ์ง„๋‹ค.
  • OutputStream๋„ ์ถ”์ƒ ํด๋ž˜์Šค์ด๋ฏ€๋กœ ์—ญ์‹œ ์ž์‹ ๊ฐ์ฒด๋ฅผ ์„ ์–ธํ•˜์—ฌ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค.

์˜ˆ์ œ

import java.io.FileOutputStream;
import java.io.OutputStream;

public class HelloIO01 {
    public static void main(String[] args) throws Exception {
        OutputStream out = new FileOutputStream("/tmp/helloio01.dat");
        out.write(1); // 0000 0000   0000 0000   0000 0000   0000 0001
        out.write(255);
        out.write(0);
        out.close();
    }
}

๋ฉ”์ธ ๋ฉ”์„œ๋“œ์— ์˜ˆ์™ธ๋ฅผ ๋˜์ง€๋„๋ก ๋‘” ๊ฒƒ์€ ํŽธ์˜์ƒ์ด๋ฏ€๋กœ ์ฃผ์˜

  • ์‹คํ–‰ ๊ฒฐ๊ณผ 3๋ฐ”์ดํŠธ ์งœ๋ฆฌ helloio01.datํŒŒ์ผ์ด ์ƒ์„ฑ๋œ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.
  • ๋ฐ”์ด๋„ˆ๋ฆฌ ํŒŒ์ผ์„ ์—ด์–ด๋ณผ ๋•Œ์—๋Š” hexa editor๋ฅผ ์‚ฌ์šฉํ•˜๋‹ˆ ํ•œ ๋ฒˆ ํ™•์ธํ•ด๋ณด์ž: https://hexed.it/
16 ์ง„์ˆ˜๋กœ ์ž…๋ ฅ๋œ ์ˆซ์ž๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค: 01, FF, 00
import java.io.FileInputStream;
import java.io.InputStream;

public class HelloIO02 {
    public static void main(String[] args) throws Exception {
        InputStream in = new FileInputStream("/tmp/helloio01.dat");

        int i1 = in.read();
        System.out.println(i1); // 1

        int i2 = in.read();
        System.out.println(i2); // 255

        int i3 = in.read();
        System.out.println(i3); // 0

        int i4 = in.read();
        System.out.println(i4); // -1 (ํŒŒ์ผ์˜ ๋)

        in.close();
    }
}

๋ฉ”์ธ ๋ฉ”์„œ๋“œ์— ์˜ˆ์™ธ๋ฅผ ๋˜์ง€๋„๋ก ๋‘” ๊ฒƒ์€ ํŽธ์˜์ƒ์ด๋ฏ€๋กœ ์ฃผ์˜

์ด ์ฝ”๋“œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋‹จ์ˆœํ™” ํ•  ์ˆ˜ ์žˆ๋‹ค: ๋ฐ˜๋ณต๋ฌธ์„ ํ†ตํ•ด ํŒŒ์ผ์„ EOF๊นŒ์ง€ ์ฝ๊ธฐ

import java.io.FileInputStream;
import java.io.InputStream;

public class HelloIO02 {
    public static void main(String[] args) throws Exception {
        InputStream in = new FileInputStream("/tmp/helloio01.dat");
        
        int buf = -1;
        while((buf = in.read()) != -1) {
            System.out.println(buf);
        }

        in.close();
    }
}

๋ฉ”์ธ ๋ฉ”์„œ๋“œ์— ์˜ˆ์™ธ๋ฅผ ๋˜์ง€๋„๋ก ๋‘” ๊ฒƒ์€ ํŽธ์˜์ƒ์ด๋ฏ€๋กœ ์ฃผ์˜

1 ๋ฐ”์ดํŠธ ์”ฉ ํŒŒ์ผ์˜ ๋ชจ๋“  ๋ฐ์ดํ„ฐ๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์ด ๋œ๋‹ค.

๋ถ€๊ฐ€ ์„ค๋ช…: Reader & Writer

How to read: Reader
  • ๋ฌธ์ž ๋‹จ์œ„(char)๋กœ ์ฝ๋Š”๋‹ค(2 ๋ฐ”์ดํŠธ).
  • ๋ฆฌํ„ด ๊ฐ’์ธ int์˜ ๋งˆ์ง€๋ง‰ ๋‘ ๋ฐ”์ดํŠธ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ฐ์ดํ„ฐ๋ฅผ ์ „๋‹ฌํ•œ๋‹ค.
  • ๊ทธ๋ฆผ ์˜ค๋ฅ˜: byte[]์ด ์•„๋‹ˆ๋ผ char[]
  • InputStream, OutputStream๊ณผ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ์ž์‹ ๊ตฌ์ฒด ํด๋ž˜์Šค๋กœ ์„ ์–ธํ•ด์•ผ ํ•œ๋‹ค.
How to write: Wirter
  • ๋“ค์–ด์˜จ int์˜ ๋งˆ์ง€๋ง‰ 2 ๋ฐ”์ดํŠธ์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์จ์ค€๋‹ค.
  • ๊ทธ๋ฆผ ์˜ค๋ฅ˜: byte[]๊ฐ€ ์•„๋‹ˆ๋ผ char[]
  • InputStream, OutputStream๊ณผ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ์ž์‹ ๊ตฌ์ฒด ํด๋ž˜์Šค๋กœ ์„ ์–ธํ•ด์•ผ ํ•œ๋‹ค.

์˜ˆ์ œ

import java.io.FileWriter;
import java.io.Writer;

public class HelloIO03 {
    public static void main(String[] args) throws Exception {
        Writer out = new FileWriter("/tmp/hello.txt");
        out.write((int) 'a');
        out.write((int) 'h');
        out.write((int) '!');
        out.close();
    }
}

๋ฉ”์ธ ๋ฉ”์„œ๋“œ์— ์˜ˆ์™ธ๋ฅผ ๋˜์ง€๋„๋ก ๋‘” ๊ฒƒ์€ ํŽธ์˜์ƒ์ด๋ฏ€๋กœ ์ฃผ์˜

  • 1 ๋ฐ”์ดํŠธ์”ฉ ์ €์žฅ๋œ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค(jdk 12), ํ•œ๊ธ€์€ 3 ๋ฐ”์ดํŠธ
  • ์ฝ์„๊ฑฐ๋ฆฌ
import java.io.FileReader;
import java.io.Reader;

public class HelloIO04 {
    public static void main(String[] args) throws Exception {
        Reader in = new FileReader("/tmp/hello.txt");

        int ch1 = in.read();
        System.out.println((char) ch1);
        int ch2 = in.read();
        System.out.println((char) ch2);
        int ch3 = in.read();
        System.out.println((char) ch3);
        int ch4 = in.read();
        System.out.println((char) ch4); // -1

        in.close();
    }
}

๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๊ฐ„๋‹จํžˆ ์“ธ ์ˆ˜ ์žˆ๋‹ค.

import java.io.FileReader;
import java.io.Reader;

public class HelloIO04 { 
    public static void main(String[] args) throws Exception {
        Reader in = new FileReader("/tmp/hello.txt");

        int cha = -1;
        while((buf = in.read()) != -1) {
            System.out.println((char) cha);
        }

        in.close();
    }
}

ํ™œ์šฉ

How to read: ํ™œ์šฉ
  • byte ๋‹จ์œ„๋กœ ์ฝ๋Š” InputStream์€ 1 ๋ฐ”์ดํŠธ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์ฝ์–ด์˜จ๋‹ค.
  • Reader๋Š” char ๋‹จ์œ„๋กœ ์ฝ์œผ๋ฏ€๋กœ 2 ๋ฐ”์ดํŠธ ๋‹จ์œ„๋กœ ์ฝ์–ด์˜จ๋‹ค: ์ด ๋•Œ InputStream์€ 2๋ฒˆ ํ˜ธ์ถœ๋œ๋‹ค.
  • BufferedReader์˜ readLine()์€ ํ•œ ์ค„์„ ์ฝ์–ด์˜ค๋ฏ€๋กœ ํ•œ ์ค„์„ ์ฝ๊ธฐ ์œ„ํ•ด ํ•„์š”ํ•œ ์ˆ˜๋งŒํผ ๊ธ€์ž(2 ๋ฐ”์ดํŠธ)๋ฅผ ์ฝ์–ด์˜จ๋‹ค.

์˜ˆ์ œ

import java.io.PrintWriter;
import java.io.FileOutputStream;
import java.io.OutputStreamReader;

public class HelloIO05 {
    public static void main(String[] args) throws Exception {
        PrintWriter out = new PrintWriter(new OutputStreamWriter(new FileOutputStream("/tmp/my.txt")));

        out.println("Hello");
        out.println("World");
        out.println("!!!");

        out.close();
    }
}
  • FileOutputStream์€ โ€œ/tmp/my.txtโ€์— ์ €์žฅ
  • FileOutputStream์€ wirte(int)๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ์Œ: int์˜ ๋งˆ์ง€๋ง‰ byte๋งŒ ์ €์žฅ
  • OutputStreamWriter์ƒ์„ฑ์ž์— ๋“ค์–ด์˜จ OutputStream์˜ write()๋ฅผ ์ด์šฉํ•˜์—ฌ ์“ด๋‹ค.
  • OutputStreamWriter๋Š” write(int); int ๋์˜ char๋ฅผ ์ €์žฅ
  • PrintWriter๋Š” ์ƒ์„ฑ์ž์— ๋“ค์–ด์˜จ OutputStreamWriter์˜ wirte()๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์“ด๋‹ค.
  • PrintWriter๋Š” println(๋ฌธ์ž์—ด); ๋ฌธ์ž์—ด์„ ์ถœ๋ ฅํ•œ๋‹ค.

ํŒŒ์ผ์—์„œ ์ฝ์„ ๋•Œ์—๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ฝ์„ ์ˆ˜ ์žˆ๋‹ค.

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;

public class HelloIO06 {
    public static void main(String[] args) throws Exception {
        BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream("/tmp/my.txt")));

        String line1 = in.readLine();
        String line2 = in.readLine();
        String line3 = in.readLine();
        String line4 = in.readLine();

        System.out.println(line1);
        System.out.println(line2);
        System.out.println(line3);
        System.out.println(line4);

        in.close();
    }
}

์ข€ ๋” ๊ฐ„๋‹จํžˆ ๋ฐ”๊พธ๋ฉด,

public class HelloIO06 {
    public static void main(String[] args) throws Exception {
        BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream("/tmp/my.txt")));

        String line = null;
        while((line = in.readLine()) != null) {
            System.out.println(line);
        }

        in.close();
    }
}

์–ด๋–ค ํด๋ž˜์Šค๋ฅผ ์‚ฌ์šฉํ•ด์•ผํ•˜์ง€?

  • ์–ด๋–ค ๋ถ€ํ’ˆ(ํด๋ž˜์Šค)๊ฐ€ ์žˆ๋Š”์ง€ ์•Œ์•„์•ผ
  • ์ด ๋ถ€ํ’ˆ๋“ค์„ ์–ด๋–ป๊ฒŒ ์‚ฌ์šฉํ•˜๋Š”์ง€ ํ›ˆ๋ จ์ด ํ•„์š”
  • ์—ฌ๋Ÿฌ ๋ถ€ํ’ˆ๋“ค์„ ์กฐํ•ฉํ•˜์—ฌ ์‚ฌ์šฉํ•œ๋‹ค.

ํƒœ๊ทธ: ,

์นดํ…Œ๊ณ ๋ฆฌ:

์—…๋ฐ์ดํŠธ:

๋Œ“๊ธ€๋‚จ๊ธฐ๊ธฐ