
Java: Input/Output Stream
- ๊ด๋ จ ๋ฌธ์
- ์ธ์ฝ๋ฉ, encoding์ด๋?
- ์คํธ๋ฆผ, stream์ด๋?
- ๋ฐ์ดํธ ๊ธฐ๋ฐ ์คํธ๋ฆผ: InputStream, OutputStream
- ๋ฐ์ดํธ ๊ธฐ๋ฐ ๋ณด์กฐ ์คํธ๋ฆผ
- ๋ฌธ์ ๊ธฐ๋ฐ ์คํธ๋ฆผ: Reader, Writer
- ๋ฌธ์ ๊ธฐ๋ฐ ๋ณด์กฐ ์คํธ๋ฆผ
- PrintWriter
๊ด๋ จ ๋ฌธ์
์ด ๊ธ์ ๋จ๊ถ์ฑ ์ โJava์ ์ ์โ์ ์ฐธ๊ณ ํ์ฌ ์์ฑ๋์์. http://cafe.naver.com/javachobostudy.cafe
์ธ์ฝ๋ฉ, encoding์ด๋?
์์ฉํ๋ก๊ทธ๋จ์ ๋ฐ์ดํฐ๋ฅผ ์คํธ๋ฆผ(Stream)ํ์์ผ๋ก ๋ณํ์์ผ ๋ณด์กฐ๊ธฐ์ต์ฅ์น๋ ๋คํธ์ํฌ์์์ ์ฌ์ฉ๊ฐ๋ฅํ ํํ๋ก ๋ณํํ๋ ์์ .
์คํธ๋ฆผ, stream์ด๋?
์ผ๋ จ์ ์ฐ์๋ ๋ฐ์ดํฐ์ ํ๋ฆ์ผ๋ก ์๋ฐํ๋ก๊ทธ๋จ๊ณผ ์ธ๋ถ์ฅ์น ์ฌ์ด์ ๋ฐ์ดํฐ ๊ตํ์ ์ํ ์ฒ๋ฆฌ ๋ฐฉ์์ด๋ค. ์ถ์ํ, ์ค์ ์ฅ์น์ ์๊ด์์ด ๊ณตํต๋ ์ ๊ทผ ๋ฐฉ์์ ์ ๊ณตํ๋ค.
๋ฐ์ดํธ ๊ธฐ๋ฐ ์คํธ๋ฆผ: InputStream, OutputStream
๋ฐ์ดํธ ๊ธฐ๋ฐ ์คํธ๋ฆผ์ ๋ฐ์ดํธ๋จ์๋ก ๋ฐ์ดํฐ๋ฅผ ์ ์กํ๋ ํด๋์ค๋ก InputStream๊ณผ OutputStream์ ์์๋ฐ๋ FileStream, ByteArrayStream, PipedStream, AudioStream, StringBufferStream ๋ฑ์ด ์๋ค.
๋ฐ์ดํธ๊ธฐ๋ฐ ์คํธ๋ฆผ์ ์ข
๋ฅ์ ๋ฐ๋ผ mark()
์ reset()
์ ์ฌ์ฉํด ํน์ ์ง์ ์ ๋งํนํ๊ณ ๊ทธ ์ง์ ์์ ๋ค์ ์ฝ๋๊ฒ์ด ๊ฐ๋ฅํ๋ค. flush()
๋ ๋ฒํผ๊ฐ ์๋ ์ถ๋ ฅ์ฉ ์คํธ๋ฆผ(OutputStream์ ํ์ฅํ ํด๋์ค๋ค)์ ๊ฒฝ์ฐ์๋ง ์๋ฏธ๊ฐ ์๊ณ , OutputStream์ ์ ์๋ flush()
๋ ์๋ฌด๊ฒ๋ ํ์ง ์๋๋ค.
FileIntputstream/FileOutputStream
ํ์ผ ์ ์ถ๋ ฅ์ ์ฌ์ฉ๋๋ ์คํธ๋ฆผ ํด๋์ค.
File file = new File("c:\\iotest", "stream.txt");
if (! file.exists()) {
file.createNewFile();
}
// ์ฐ๊ธฐ
FileOutputStream fos = new FileOutputStream("c:\\iotest\\stream.txt", true);
// true : Append, ์ด์ด์ฐ๊ธฐ
// false : Create, ๊ธฐ์กด ๋ด์ฉ์ ๋ฎ์ด์ฐ๊ธฐ
// ๊ธฐ๋ณธ๊ฐ : false
fos.write(97); // a๊ฐ stream.txt์ ์ฐ์ฌ์ง
fos.write(98); // b๊ฐ stream.txt์ ์ฐ์ฌ์ง
fos.write(99); // c๊ฐ stream.txt์ ์ฐ์ฌ์ง
fos.close();
// ์ฝ๊ธฐ
FileInputStream fis = new FileInputStream("c:\\iotest\\stream.txt");
int data = 0;
while ((data = fis.read()) != -1) { // ๋ ์ด์ ์ฝ์๊ฒ์ด ์์ ๋ -1
System.out.print((char) data); //์คํธ๋ฆผ์ด ์ฝ์ ๋ฐ์ดํธ์ฝ๋๋ฅผ ๋ฌธ์๋ก ๋ณํํ์ฌ ์ถ๋ ฅ
}
fis.close();
์๋๋ ํ์ผ์ ์ฝ์ด์ ๊ทธ๋๋ก ๋ค๋ฅธํ์ผ์ ๋ด์ฉ์ ์ถ๋ ฅํ๋ ์ฝ๋๋ค:
FileInputStream fis = new FileInputStream("c:\\iotest\\origin.txt");
FileOutputStream fos = new FileOutputStream("c:\\iotest\\clone.txt", false);
int data = 0;
while ((data = fis.read()) != -1) { // ๋ ์ด์ ์ฝ์๊ฒ์ด ์์ ๋ -1
fos.write(data);
}
fis.close();
fos.close();
๋ค์์ฒ๋ผ ์์ฑํ๋ฉด String ๋ณ์์ ๊ฐ์ ์ถ๋ ฅํ๋ค:
FileOutputStream fos = new FileOutputStream("c:\\iotest\\stream.txt", false);
String txt = "์๋
! Fine and strong day! im waldo!";
for (int i = 0; i < txt.length(); i++) { //๋ฌธ์์ด์ ๊ธธ์ด๋งํผ
char c = txt.charAt(i); // ๋ฌธ์๋ก ํ๋ณํ ํ
fos.write((int) c); // ์ถ๋ ฅ
}
fos.close();
๋จ, ์ ๋ฐฉ๋ฒ์ ํ๊ธ์ ์ฒ๋ฆฌํ ์ ์๋ค. ๋ฐ์ดํธ ์คํธ๋ฆผ์ผ๋ก ํ๊ธ์ ์ฒ๋ฆฌํ๊ณ ์ถ๋ค๋ฉด ๋ค์์ฒ๋ผ ํ๋ค:
FileOutputStream fos = new FileOutputStream("c:\\iotest\\stream.txt", false);
String txt = "์๋
! Fine and strong day! im waldo!";
fos.write(txt.getBytes());
fos.close();
๋ฐ์ดํธ ๊ธฐ๋ฐ ๋ณด์กฐ ์คํธ๋ฆผ
์คํธ๋ฆผ์ ๊ธฐ๋ฅ์ ๋ณด์ํ๊ธฐ ์ํ ํด๋์ค๋ฅผ ๋ณด์กฐ ์คํธ๋ฆผ์ด๋ผ ํ๋ฉฐ ๋ฐ์ดํธ๊ธฐ๋ฐ, ๋ฌธ์๊ธฐ๋ฐ ์คํธ๋ฆผ ๋ชจ๋ ๋ณด์กฐ ์คํธ๋ฆผ์ด ์ ๊ณต๋๋ค.
๋ณด์กฐ ์คํธ๋ฆผ์ ์ค์ ๋ฐ์ดํฐ๋ฅผ ์ฃผ๊ณ ๋ฐ๋ ์คํธ๋ฆผ์ด ์๋๊ธฐ ๋๋ฌธ์ ๋ณด์กฐ ์คํธ๋ฆผ๋ง์ผ๋ก๋ ์ ์ถ๋ ฅ์ ์ฒ๋ฆฌํ ์ ์๊ณ , ๋ฐ์ดํธ ๊ธฐ๋ฐ ์คํธ๋ฆผ ํน์ ๋ฌธ์ ๊ธฐ๋ฐ ์คํธ๋ฆผ์ ๋จผ์ ์์ฑํ ๋ค์ ์ด๋ฅผ ์ด์ฉํด ๋ณด์กฐ ์คํธ๋ฆผ์ ์์ฑํ๋ค.
// ๊ธฐ๋ฐ ์คํธ๋ฆผ ์์ฑ
FileInputStream fis = new FileInputStream("c:\\iotest\\sample.txt");
// ๊ธฐ๋ฐ ์คํธ๋ฆผ์ ์ด์ฉํ ๋ณด์กฐ ์คํธ๋ฆผ ์์ฑ
BufferedInputStream bis = new BufferedInputStream(fis);
bis.read();
์๋ฅผ ๋ณด๋ฉด BufferedInputStream์ read()
๋ฅผ ์ฌ์ฉํ ๊ฒ ๊ฐ์ง๋ง ์ค์ ์
๋ ฅ๊ธฐ๋ฅ์ ๊ธฐ๋ฐ ์คํธ๋ฆผ์ FileInputStream์ด ์ํํ๊ณ BufferedInputStream์ ๋ฒํผ ๊ธฐ๋ฅ๋ง ์ ๊ณตํ๋ค.
์ด์ฒ๋ผ ๋ณด์กฐ ์คํธ๋ฆผ์ ๊ฒฝ์ฐ ๋๋ถ๋ถ ์ ์ถ๋ ฅ์ ๋ฒํผ๋ฅผ ์ด์ฉํ๋ค๋ ํน์ง์ด ์๋๋ฐ ์ผํ ๋ณ๊ฑฐ ์์ด ๋ณด์ด์ง๋ง ๋ฐ์ดํฐ์ ์์ด ๋ง์ ์๋ก ๋ฒํผ๊ฐ ์ฒ๋ฆฌ ์๋์ ์๋นํ ์ํฅ์ ๋ผ์น๋ค.
BufferedInputStream/BufferedOutputStream
๋ฒํผ๋ฅผ ์ด์ฉํด ์คํธ๋ฆผ์ ์ ์ถ๋ ฅ ํจ์จ์ ๋์ด๊ธฐ ์ํด ์ ๊ณต๋๋ ๋ณด์กฐ ์คํธ๋ฆผ์ด๋ค.
ํ ๋ฐ์ดํธ ์ฉ ์ฝ๊ณ ์ฐ๋ ๊ฒ๋ณด๋ค ๋ฒํผ(๋ฐ์ดํธ๋ฐฐ์ด)์ ์ด์ฉํด ์ฌ๋ฌ ๋ฐ์ดํธ๋ฅผ ํ ๋ฒ์ ์ฒ๋ฆฌํ๋ ๊ฒ์ด ๋น ๋ฅด๊ธฐ ๋๋ฌธ์ ๋๋ถ๋ถ์ ์ ์ถ๋ ฅ ์์ ์ ์ฌ์ฉ๋๋ค.
์์ฑ์์ ๋ฒํผ ํฌ๊ธฐ๋ฅผ ์ง์ ํ ์ ์๋๋ฐ ์ด๋ฅผ ์๋ตํ๋ฉด ๊ธฐ๋ณธ๊ฐ์ผ๋ก 8192 byte์ ๋ฒํผ๋ฅผ ๊ฐ๊ฒ ๋๋ค. ๋ฒํผ ํฌ๊ธฐ๋ ์ ๋ ฅ ์์ค๋ก๋ถํฐ ํ ๋ฒ์ ๊ฐ์ ธ์ฌ ์ ์๋ ๋ฐ์ดํฐ์ ํฌ๊ธฐ๋ก ์ง์ ํ๋ฉด ์ข๋ค๊ณ ํ๋ค์นด๋๋ผ.
File file = new File("c:\\iotest\\sample.txt");
FileInputStream fis = new FileInputStream(file);
BufferedInputStream bis = new BufferedInputStream(fis, 4096); // ๋ฒํผ ์ฌ์ด์ฆ๋ฅผ ์ง์ ํ ์ ์๋ค.
while (bis.available() > 0) {
System.out.print((char) bis.read());
}
bis.close();
์ฌ๊ธฐ์๋ available()
์ ์ฌ์ฉํ๋๋ฐ ์ด๋ Stream ํด๋์ค์ ๋ฉ์๋๋ก ์คํธ๋ฆผ์ผ๋ก๋ถํฐ ์ฝ์ด ์ฌ ์ ์๋ ๋ฐ์ดํฐ์ ํฌ๊ธฐ๋ฅผ ๋ฆฌํดํ๋ค.
BufferedStream ์ฌ์ฉ ์
// ํ์ผ ๋ณต์ฌ
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("C:/iotest/readme.txt"));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("C:/iotest/writeme.txt"));
int len = 0;
while (bis.available() > 0) {
len ++;
bos.write(bis.read());
}
System.out.printf("์ด %,d๋ฐ์ดํธ ๋ณต์ฌ๋จ\n", len);
bos.flush();
bis.close();
bos.close();
// String ์ง์ ์ฐ๊ธฐ
BufferedOutputStream bos2 = new BufferedOutputStream(new FileOutputStream("C:/iotest/result.txt"));
String txt = "์๋
ํ์ ๊ฐ?\r\ngood morning!";
bos2.write(txt.getBytes());
bos2.flush();
bos2.close();
DataInputStream/DataOutputStream
FileterStream์ ์์ํ๋ฉด์ DataInput/DataOutput ์ธํฐํ์ด์ค๋ฅผ ๊ตฌํํ ํด๋์ค. ๋ชจ๋ ์์ํ ๊ฐ์ ์ฝ๊ณ ์ธ ์ ์๋ค.
DataOutputStream์ ์ถ๋ ฅ ์ ๊ฐ ์์ํ ๊ฐ์ 16์ง์๋ก ํํํ์ฌ ์ ์ฅํ๋ค. ์๋ฅผ ๋ค์ด intํ ๊ฐ์ 4byte์ 16์ง์๋ก ์ถ๋ ฅ๋๋ค.
DataStream์ ๋ฌธ์๋ฅผ ์ง์ ์ ์ฅํ๋ ๊ฒ๋ณด๋ค ์๋๊ฐ ๋น ๋ฅด๋ค๋ ํน์ง์ด ์๋ค. ์ด๋ data์ ๋ฌธ์๊ฐ ๋ณํ ๊ณผ์ ์ด ์๊ธฐ ๋๋ฌธ์ด๋ค.
File file = new File("c:\\iotest\\sample.dat");
// ์ฐ๊ธฐ
DataOutputStream dos = new DataOutputStream(new FileOutputStream(file));
dos.writeBoolean(true);
dos.write(5);
dos.writeByte((byte) 5);
dos.writeInt(100);
dos.writeDouble(200.5);
dos.writeUTF("utfutfutf");
dos.flush();
dos.close();
// ์ฝ๊ธฐ
DataInputStream dis = new DataInputStream(new FileInputStream(file));
System.out.println(dis.readBoolean());
System.out.println(dis.read());
System.out.println(dis.readByte());
System.out.println(dis.readInt());
System.out.println(dis.readDouble());
System.out.println(dis.readUTF());
dis.close();
ํน์ ํ ๋ณํ์์ ์ด๋ ์๋ฆฟ์๋ฅผ ์ ํ์์์ด ์ฝ๊ธฐ๋ง ํ๋ฉด ๋๋๋ฐ ์ฃผ์ํ ์ ์ด ์๋ค. ๋ฐ์ดํฐ๋ฅผ ์ฐ๋ ๊ณผ์ ์์ ์๋ฃํ์ ๋ฌ๋ฆฌํด์ ์ ์ฅํ ๊ฒฝ์ฐ ์ฝ์ด์ฌ ๋๋ ๋ฐ๋์ ๊ทธ ์์์ ๋ง๊ฒ ์ฝ์ด์์ผ ํ๋ค๋ ๊ฒ์ด๋ค. ๋ง์ฝ ์ด ์์๊ฐ ๋ฐ๋๋ฉด read ๋ฉ์๋๋ค์ด ์ฌ๋ฐ๋ฅธ ์๋ฃํ์ผ๋ก ๋ณํํ์ง ๋ชปํด ์๋ฑํ ๊ฐ์ ์ถ๋ ฅํ๊ฒ ๋๋ค.
๋ฌธ์ ๊ธฐ๋ฐ ์คํธ๋ฆผ: Reader, Writer
๋ฐ์ดํธ ๊ธฐ๋ฐ ์คํธ๋ฆผ์ 1byte๋จ์๋ก ์ฝ๊ณ ์ฐ๊ธฐ ๋๋ฌธ์ ํฌ๊ธฐ๊ฐ 2byte ์ด์์ธ ๋ฌธ์๋ฅผ ๋ค๋ฃจ๋๋ฐ ๋ฌธ์ ๊ฐ ์๋ค. ๋ฌธ์ ๊ธฐ๋ฐ ์คํธ๋ฆผ์ ์ด ์ ์ ๋ณด์ํ๊ธฐ ์ํ ํด๋์ค๋ค. ๋ํ ๋ฌธ์ ์ธ์ฝ๋ฉ๊ฐ ๋ณํ์ ์๋์ผ๋ก ์ฒ๋ฆฌํด์ค๋ค๋ ์ฅ์ ์ด ์๋ค.
Reader์ Writer ํด๋์ค๋ ๋ฐ์ดํธ ์คํธ๋ฆผ์์ ์ฒ๋ผ ๋ฌธ์ ๊ธฐ๋ฐ ์คํธ๋ฆผ ํด๋์ค๋ค์ด ์์๋ฐ์ ๋ถ๋ชจ ํด๋์ค๋ก byte ๋ฐฐ์ด์ด ์๋ char ๋ฐฐ์ด์ ์ฌ์ฉํ๋ read()
, write()
๊ฐ ์ถ์๋ฉ์๋๋ก ๊ตฌํ๋์ด ์๋ค.
FileReader/FileWriter
// ์ฐ๊ธฐ
FileWriter out = new FileWriter("c:\\iotest\\sample.txt", true);
//true: Append, ์ด์ด์ฐ๊ธฐ
//false: Create, ๊ธฐ์กด ๋ด์ฉ์ ๋ฎ์ด์ฐ๊ธฐ
//๊ธฐ๋ณธ๊ฐ : false
String txt = "You just activated \r\nMY TRAP CARD \r\n๋ถ๋ฐฅ๋ถ๋ฐฅ๋ถ๋ฐฅ๋ฐ";
out.write(txt);
out.close();
// ์ฝ๊ธฐ
FileReader in = new FileReader("c:\\iotest\\sample.txt");
int data = 0;
while((data = in.read()) != -1) { // ํ์ผ ๋ด์ฉ์ ๊ธธ์ด๋งํผ ๋ฃจํ
System.out.print((char) data); // ์ฝ์ด์จ ๋ฐ์ดํฐ๋ฅผ ๋ฌธ์๋ก ๋ณํ - ์ถ๋ ฅ
}
in.close();
// ํ์ผ ๋ณต์ฌ
FileReader in = new FileReader("c:\\iotest\\sample.txt");
FileWriter out = new FileWriter("c:\\iotest\\clone.txt");
int data = 0;
while ((data = in.read()) != -1) {
out.write(data);
}
in.close();
out.close();
// String ๋ฐ๋ก ์ฐ๊ธฐ
FileWriter out2 = new FileWriter("c:\\iotest\\result.txt");
String txt = "์๋
? abcdefg\r\nhihi~";
out2.write(txt);
out2.close();
๋ฌธ์ ๊ธฐ๋ฐ ๋ณด์กฐ ์คํธ๋ฆผ
BufferedReader/BufferedWriter
๋ฒํผ๋ฅผ ์ด์ฉํด Reader์ Writer์ ์ ์ถ๋ ฅ ํจ์จ์ ๊ฐ์ ํ ํด๋์ค.
// ์ฐ๊ธฐ
BufferedWriter out = new BufferedWriter(new FileWriter("c:\\iotest\\buffer.txt", false));
out.write("๋ฒํผ๋๋ฆฌ๋๋ฅผ ํ์ฉํ \r\nํ์ผ์ถ๋ ฅ");
out.flush();
out.close();
// ์ฝ๊ธฐ
BufferedReader in = new BufferedReader(new FileReader("c:\\iotest\\buffer.txt"));
String data;
while((data = in.readLine()) != null) { //์ฝ์๊ฒ ์์ผ๋ฉด null ๋ฆฌํด
System.out.println(data);
}
in.close();
//// read()๋ฅผ ์ฌ์ฉํ ๊ฒฝ์ฐ
//int data;
//while((data = in.read()) != -1) {
// System.out.print((char) data);
//}
//in.close();
๋ฐ์ดํธ ๊ธฐ๋ฐ ๋ณด์กฐ ์คํธ๋ฆผ๊ณผ ๋ง์ฐฌ๊ฐ์ง๋ก flush()
๋ฅผ ์คํํ๊ฑฐ๋ ์คํธ๋ฆผ์ ๋ซ์์ผ ๋ฒํผ์ ๋ฐ์ดํฐ๊ฐ ์ถ๋ ฅ๋๋ค.
read()
๋ stream์ ๊ฒฝ์ฐ์ ์ฌ์ฉ๋ฐฉ๋ฒ์ ๊ฑฐ์ ์ฐจ์ด๋์ง ์์ง๋ง ์๋กญ๊ฒ ์ถ๊ฐ๋ readLine()
์ read()
์ ๋ค๋ฅด๊ฒ ์ค ๋จ์๋ก ๋ฐ์ดํฐ๋ฅผ ์ฝ์ด์ค๋ฉฐ ๋ ์ด์ ์ฝ์ ๋ฐ์ดํฐ๊ฐ ์์ผ๋ฉด -1์ด ์๋๋ผ null์ ๋ฆฌํดํ๋ค.
// ํ์ผ ๋ณต์ฌ
BufferedReader in = new BufferedReader(new FileReader("c:\\iotest\\sample.txt"));
BufferedWriter out = new BufferedWriter(new FileWriter("c:\\iotest\\clone.txt"));
String data = "";
while ((data = in.readLine()) != null) {
out.write(data + "\r\n");
}
out.flush();
in.close();
out.close();
// String ์ง์ ์ฐ๊ธฐ
BufferedWriter out2 = new BufferedWriter(new FileWriter("c:\\iotest\\result.txt"));
String txt = "ํธ์น์ด";
out2.write(txt);
out2.flush();
out2.close();
์์ฉ - ์ธ๋ฏธ์ฝ๋ก โ;โ์ ํฌํจํ ์ค๋ง ์ถ๋ ฅ
// ์ฝ๊ธฐ
BufferedReader in = new BufferedReader(new FileReader("c:\\iotest\\Test1.java"));
String data = "";
int cnt = 0;
while ((data = in.readLine()) != null) {
cnt ++;
if (data.indexOf(";") != -1) {
System.out.println(cnt + ":" + data);
}
}
InputStreamReader/OutputStreamWriter
๋ฐ์ดํธ ๊ธฐ๋ฐ ์คํธ๋ฆผ์ ๋ฌธ์ ๊ธฐ๋ฐ ์คํธ๋ฆผ์ผ๋ก ์ฐ๊ฒฐ์์ผ์ฃผ๋ ์ญํ ์ ํ๋ค. ์ฝ๊ฑฐ๋ ์ธ ๋ ๋ฌธ์ ์ธ์ฝ๋ฉ์ ์ง์ ํ ์ ์๋ค.
File file = new File("c:\\iotest\\sample.txt");
InputStreamReader inStrReader = new InputStreamReader(new FileInputStream(file), "utf-8");
BufferedReader in = new BufferedReader(inStrReader);
String data = "";
while ((data = in.readLine()) != null) {
System.out.println(data);
}
in.close();
ํฐ๋ฏธ๋์ ํตํด ์ ๋ ฅ๋ฐ์ ๋ ๋ณดํต ์๋์ฒ๋ผ ์ด๋ค:
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
//BufferedReader in = new BufferedReader(new InputStreamReader(System.in, "utf-8"));
PrintWriter
PrintStream๋ณด๋ค ํฅ์๋ ๋ฌธ์๊ธฐ๋ฐ ์คํธ๋ฆผ์ด๋ค. JDK1.1์ ์ถ๊ฐ๋์๋ค.
// PrintStream
System.out.println("hi"); //new PrintStream(System.out).println("hello world");
// PrintWriter
PrintWriter pw = new PrintWriter(System.out);
pw.write("hi");
pw.flush();
pw.close();