์๋ฐ I/O 03
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
๋ถ๊ฐ ์ค๋ช : InputStream & OutputStream
- InputStream์ ๋ฐ์ดํธ ๋จ์๋ก ์ฝ์ด๋ค์ธ๋ค(read ๋ฉ์๋).
- int(4 ๋ฐ์ดํธ) ์ค ๋ง์ง๋ง ํ ๋ฐ์ดํธ๋ง์ ์ ํจ ๋ฐ์ดํฐ๋ก ์ฌ์ฉํ๋ค.
- ํ์ผ์ read ๋ฉ์๋๋ฅผ ํตํด ์ฝ์ด๋ค์ด๊ฒ ๋๋ฉด read ๋ฉ์๋๊ฐ ์ฌ์ฉ๋ ๋๋ง๋ค 1๋ฐ์ดํธ ์ฉ ํ์ผ์์ ์ฝ๊ฒ ๋๋ค.
- byte ๋ฐฐ์ด์ ์ฌ์ฉํ๋ฉด ์ฌ์ฉํ byte ๋ฐฐ์ด์ ๋ฐ๋ผ ํ๋ฒ์ ์ ๋ฌํ๋ค. ๋ฐฐ์ด์ด ํด์๋ก ํ ๋ฒ์ ์ฝ๋ ์์ด ๋ง์์ง๋ค.
- ๊ทธ๋ฆผ ์ค๋ฅ: read๋ฉ์๋๋ int ๊ฐ์ ๋ฃ์ด์ฃผ๋๊ฒ ์๋๋ผ int ๊ฐ์ผ๋ก ๋ฐํ๋๋ค.
- InputStream์ ์ถ์ ํด๋์ค์ด๊ธฐ ๋๋ฌธ์ ์์ ๊ฐ์ฒด๋ฅผ ์ ์ธํ์ฌ ์ฌ์ฉํด์ผ ํ๋ค.
- 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/
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
- ๋ฌธ์ ๋จ์(char)๋ก ์ฝ๋๋ค(2 ๋ฐ์ดํธ).
- ๋ฆฌํด ๊ฐ์ธ int์ ๋ง์ง๋ง ๋ ๋ฐ์ดํธ๋ฅผ ์ฌ์ฉํ์ฌ ๋ฐ์ดํฐ๋ฅผ ์ ๋ฌํ๋ค.
- ๊ทธ๋ฆผ ์ค๋ฅ: byte[]์ด ์๋๋ผ char[]
- InputStream, OutputStream๊ณผ ๋ง์ฐฌ๊ฐ์ง๋ก ์์ ๊ตฌ์ฒด ํด๋์ค๋ก ์ ์ธํด์ผ ํ๋ค.
- ๋ค์ด์จ 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();
}
}
ํ์ฉ
- 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();
}
}
์ด๋ค ํด๋์ค๋ฅผ ์ฌ์ฉํด์ผํ์ง?
- ์ด๋ค ๋ถํ(ํด๋์ค)๊ฐ ์๋์ง ์์์ผ
- ์ด ๋ถํ๋ค์ ์ด๋ป๊ฒ ์ฌ์ฉํ๋์ง ํ๋ จ์ด ํ์
- ์ฌ๋ฌ ๋ถํ๋ค์ ์กฐํฉํ์ฌ ์ฌ์ฉํ๋ค.
๋๊ธ๋จ๊ธฐ๊ธฐ