์ ์ถ๋ ฅ(Input/Output) ์คํฌ๋ฆผ์ด๋?
์๋ฐ์ ๋ชจ๋ ๋ฐ์ดํฐ ์ ์ถ๋ ฅ์ ์คํธ๋ฆผ์ด๋ผ๋ ๊ฐ๋ ์ ์ํด ์ด๋ฃจ์ด์ง๋ค.
์คํธ๋ฆผ์ ์์ฉํ๋ก๊ทธ๋จ๊ณผ ์ ์ถ๋ ฅ ์ฅ์น๋ฅผ ์ฐ๊ฒฐํ๋ ์ํํธ์จ์ด ๋ชจ๋ํธ์, ์ ์ถ๋ ฅ
์ฅ์น์ ์์ฉํ๋ก๊ทธ๋จ ์ฌ์ด์์ ๋ฐ์ดํฐ๊ฐ ์์๋๋ก ์ ์ก๋๋๋ก ํ๋ค.
๋ฐ์ดํฐ ์ ์ถ๋ ฅ์ ๋ชจ๋ ๋ฐ์ดํฐ๋ฅผ ํํ์๋ ๊ด๊ณ ์์ด ์ผ๋ จ๋ ํ๋ฆ์ผ๋ก ์ ์ก์ ํ๋ ๊ฒ์ด
์ ์ถ๋ ฅ ๋ชจ๋ธ์ ๊ธฐ๋ณธ ๊ฐ๋
๋ฐ์ดํธ ์คํธ๋ฆผ : 1byte๋จ์๋ก ์ ์ถ๋ ฅ์ ์ํํ๋ฉฐ, ๊ธฐ๋ณธ ์ ์ถ๋ ฅ ๋จ์
๋ฌธ์ ์คํธ๋ฆผ: 2byte์ ๋ฌธ์ ๋จ์๋ก ์ ์ถ๋ ฅ์ ์ํ
์ ์ถ๋ ฅ ์์ธ ํด๋์ค
- java.io.IOException
์ ์ถ๋ ฅ ์ฒ๋ฆฌ์ค ์์ธ๊ฐ ๋ฐ์ํ ๋, ๋๋ถ๋ถ ์ ์ถ๋ ฅ ๊ด๋ จ ์์ธ ํด๋์ค์ ์์ ํด๋์ค - java.io.FileNotFoundException
์ง์ ๋ ๊ฒฝ๋ก์ ํ์ผ์ด ์กด์ฌํ์ง ์๋ ๊ฒฝ์ฐ ๋ฐ์ํ๋ ์์ธ - java.io.EOFException
์ ๋ ฅ ๋์ค ์๊ธฐ์น ์๊ฒ ํ์ผ์ด ๋๋ ์คํธ๋ฆผ ์ข ๋ฃ์ ๋๋ฌํ์์ ์๋ฆฌ๋ ์์ธ, ์ฃผ๋ก ๋ฐ์ดํฐ ์ ๋ ฅ ์คํธ๋ฆผ์ ์ข ๋ฃ๋ฅผ ์๋ฆฌ๊ธฐ ์ํด ์ฌ์ฉํ๋ค.
ํ์ค ์ ์ถ๋ ฅ ์คํธ๋ฆผ
- System.in
ํ์ค ์ ๋ ฅ ์คํธ๋ฆผ์ผ๋ก, ์ด๋ ค ์์ด ๋ฐ๋ก ์ฌ์ฉ ๊ฐ๋ฅํ๋ค - System.out
ํ์ค ์ถ๋ ฅ ์คํธ๋ฆผ์ผ๋ก, ์ด๋ ค ์์ด ๋ฐ๋ก ์ฌ์ฉ ๊ฐ๋ฅํ๋ค. - System.err
ํ์ค ์๋ฌ ์ถ๋ ฅ ์คํธ๋ฆผ์ผ๋ก, ์ด๋ ค ์์ด ๋ฐ๋ก ์ฌ์ฉ ๊ฐ๋ฅํ๋ค.
๋ฐ์ดํธ ์คํธ๋ฆผ
- ์คํธ๋ฆผ์ ๊ธฐ๋ณธ ์ ์ถ๋ ฅ ๋จ์๋ ๋ฐ์ดํธ
- 1๋ฐ์ดํธ(8bit) ๋จ์๋ก ์ ์ถ๋ ฅ์ ์ํํ๋ค.
- 2์ง ๋ฐ์ดํฐ์ ์ ์ถ๋ ฅ์ด ๊ฐ๋ฅํ๋ค.
- ๋ชจ๋ ๋ฐ์ดํธ ์คํธ๋ฆผ ํด๋์ค๋ InputStream๋ฐ OutputStream ํด๋์ค์ ์๋ธ ํด๋์ค
๋ฌธ์ ์คํธ๋ฆผ
- 2๋ฐ์ดํธ(16bit)์ ์ ๋์ฝ๋ ๋ฌธ์๋ฅผ ์ ์ถ๋ ฅ ํ๊ธฐ ์ํ ์คํธ๋ฆผ
- ๋ฌธ์ ๋จ์๋ก ์ ์ถ๋ ฅ์ ์ํํ๋ค.
- ๋ชจ๋ ๋ฌธ์ ์คํธ๋ฆผ ํด๋์ค๋ Reader ๋ฐ Writer ํด๋์ค์ ์๋ธ ํด๋์ค
InputStream
์ถ์ ํด๋์ค๋ก, ๋ชจ๋ ๋ฐ์ดํธ ์ ๋ ฅ ์คํธ๋ฆผ ํด๋์ค์ ์ต์์ ํด๋์ค
package ch14.unit02;
public class Ex01_InputStream {
public static void main(String[] args) {
int data;
char ch;
//System.in : InputStream ๊ฐ์ฒด. byte ์คํธ๋ฆผ
//InputStream ์ read() : 1byte๋ฅผ ์ฝ์ด ASCII ์ฝ๋ ๋ฐํ
/*
* -ABC ์ํฐ : 65 66 67 13 10
* -๋ํ์ํฐ : 235 140 128 237 149 156 13 10(UTF8)
* -12์ํฐ :
*/
try {
System.out.println("๋ฌธ์์ด[Ctrl+Z] ?");
while((data=System.in.read()) !=-1) {
//System.out.println(data);
ch=(char)data;
//System.out.print(ch); // ๋ํ > ๋ํ์ผ๋ก ์๋์ด
//ํ๊ธ์ด ๊นจ์ง. 3byte ๋ฌธ์(UTF-8)๋ฅผ 1byte๋ง ์ฝ์ด์ 2byte๋ฅผ ์ถ๋ ฅ ํ์ผ๋ฏ๋ก
System.out.write(ch); // ๋ํ > ๋ํ์ผ๋ก ๋์ด
//ํ์ 1 byte๋ฅผ ์ถ๋ ฅ. ํ๊ธ ๊นจ์ง์ง ์์
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
package ch14.unit02;
import java.io.IOException;
public class Ex02_InputStream {
public static void main(String[] args) {
int data;
try {
System.out.println("ABCDEF ์
๋ ฅํ ์ํฐ...");
data=System.in.read();
System.out.write(data); // ํ์ 1byte๋ฅผ ์ถ๋ ฅ ๋ฒํผ์ ๋ณด๋
System.in.skip(2); // 2byte๋ฅผ ์ฝ์ด์ ๋ฒ๋ฆผ
data=System.in.read();
System.out.write(data); // D
System.in.skip(4); // ef์ํฐ ์ฝ์ด ๋ฒ๋ฆผ
System.out.flush(); // ๋ฒํผ์ ๋ด์ฉ์ ์ถ๋ ฅ ์ฅ์น๋ก ๋ณด๋
} catch (IOException e) {
e.printStackTrace();
}
}
}
package ch14.unit02;
/*
1.์ฆ๊ฐ 2.๊ฐ์ 3.์ข
๋ฃ =>1
speed:1
1.์ฆ๊ฐ 2.๊ฐ์ 3.์ข
๋ฃ =>1
speed:2
*/
public class Ex03_InputStream {
public static void main(String[] args) {
Boolean run=true;
int n=0;
char ch;
try {
while(run) {
do {
System.out.print("1.์ฆ๊ฐ 2.๊ฐ์ 3.์ข
๋ฃ =>");
ch=(char)System.in.read();
System.in.skip(2);
} while (ch<'1'||ch>'3');
switch(ch) {
case '1' :n++; break;
case '2' :n--; break;
case '3' :run =false; break;
}
System.out.println("speed:"+n+"\n");
}
} catch (Exception e) {
// TODO: handle exception
}
}
}
OutputStream
์ถ์ ํด๋์ค๋ก ๋ชจ๋ ๋ฐ์ดํธ ์ถ๋ ฅ ์คํธ๋ฆผ์ ์ต์์ ํด๋์ค
package ch14.unit02;
import java.io.OutputStream;
public class Ex11_OutputStream {
public static void main(String[] args) {
try {
// OutputStream: ์ถ๋ ฅ byte ์คํธ๋ฆผ
OutputStream os=System.out;
os.write(65); // ํ์ 1byte ๋ฅผ ์ถ๋ ฅ ๋ฒํผ๋ก ๋ณด๋
// ๋ฒํผ ํฌ๊ธฐ๋ 128์ด๋ฏ๋ก 129๋ฒ ์ถ๋ ฅํ๋ฉด ์ถ๋ ฅ
os.write(66);
os.write(67);
// ์ถ๋ ฅ ๋ฐฉ๋ฒ - 1
//os.write(10); // <-์ํฐ
// ์ถ๋ ฅ ๋ฐฉ๋ฒ - 2
//os.close();
//System.out.println("end...."); <- System.out์ ๋ซ์์ ์ถ๋ ฅ ์๋จ
// ์ถ๋ ฅ ๋ฐฉ๋ฒ - 3
os.flush();
} catch (Exception e) {
e.printStackTrace();
}
}
}
package ch14.unit02;
import java.io.OutputStream;
public class Ex12_OutputStream {
public static void main(String[] args) {
int data=97;
byte[] b= {65,66,67,68,69,70};
try {
System.out.write(data); // ํ์ 1byte ์ถ๋ ฅ
System.out.println();
//System.out.flush();
//up=casting
OutputStream os=System.out;
os.write(b,2,3); // CDE
System.out.println(); //
os.write(b,0,b.length); //
System.out.println(); // ABCDEF
os.write(235);
os.write(140);
os.write(128);
System.out.println(); // ๋
os.close();
System.out.println("end..."); // ์ถ๋ ฅ ์๋จ
} catch (Exception e) {
e.printStackTrace();
}
}
}
Reader
๋ฌธ์ ์ ๋ ฅ์ ์ํ ์ถ์ํด๋์ค๋ก, ๋ชจ๋ ๋ฌธ์ ์ ๋ ฅ ์คํธ๋ฆผ์ ์ต์์ ํด๋์ค
package ch14.unit02;
import java.io.InputStreamReader;
import java.io.Reader;
public class Ex21_Reader {
public static void main(String[] args) {
int data;
char ch;
try {
// -InputStreamReader
// : byte stream์ ๋ฌธ์ stram์ผ๋ก ๋ณ๊ฒฝ
Reader rd=new InputStreamReader(System.in);
//ABC ์ํฐ : 65 66 67 13 10
//๋ํ์ํฐ : 45824 54620 13 10
//์๋ฌธ์,ํ๊ธ ๋ชจ๋ ํ๋ฒ์ ํ๋ฌธ์์ฉ ์
๋ ฅ
System.out.println("์
๋ ฅ[ctrl+z:์ข
๋ฃ]");
while((data=rd.read())!=-1) {
//System.out.println(data);
ch=(char)data;
//System.out.print(ch);
//ํ๊ธ๊นจ์ง,2byte๋ฌธ์๋ฅผ 1byte ์ถ๋ ฅํ๋ฏ๋ก
System.out.print(ch);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
Writer
๋ฌธ์ ์ถ๋ ฅ์ ์ํ ์ถ์ ํด๋์ค๋ก, ๋ชจ๋ ๋ฌธ์ ์ถ๋ ฅ ์คํธ๋ฆผ์ ์ต์์ ํด๋์ค
package ch14.unit02;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.io.Writer;
public class Ex22_Writer {
public static void main(String[] args) {
int data;
try {
Reader rd= new InputStreamReader(System.in);
Writer wt= new OutputStreamWriter(System.out);
System.out.println("๋ฌธ์์ด[ctr]+z:์ข
๋ฃ...");
while((data=rd.read())!=-1){
wt.write(data);
wt.flush(); // ์ถ๋ ฅ์ด ๋๊ธฐ ์ํด์๋ ๋ง์ ๋ฌธ์๋ฅผ ์
๋ ฅํ๊ฑฐ๋
// ctrl+z๋ฅผ ๋๋ฌ์ผ ํจ
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
ํํฐ ์คํธ๋ฆผ์ด๋?
ใ ฃในํฐ ์คํธ๋ฆผ์ ๊ธฐ์กด์ ์คํธ๋ฆผ์ ์ํ๋ ๊ธฐ๋ฅ์ ์ ๊ณตํ๋ ์๋ก์ด ์คํธ๋ฆผ์ผ๋ก ๋ฐ๊พธ์ด ์ฃผ๋ ์คํธ๋ฆผ์ด๋ค.
ํํฐ ์คํธ๋ฆผ์ ์ต์์ ํด๋์ค์๋ FilterInputStream๊ณผ
FilterOutputStream ํด๋์ค๊ฐ ์๋ค.
์ค์ ๋ฐ์ดํฐ ์์ค์์ ์คํธ๋ฆผ์ ์ฒ๋ฆฌํ๋ ๊ฒ์ด ์๋๋ผ ๋ค๋ฅธ ์คํธ๋ฆผ๊ณผ ์ฐ๊ฒฐํ๋ ์ฉ๋๋ก ์ฌ์ฉ๋๋ค.
๋ฐ๋ผ์ ์ /์ถ๋ ฅ ์คํธ๋ฆผ์์ ๋ฐ์ ๋ฐ์ดํฐ๋ฅผ ๋ค๋ฅธ ๋ฐ์ดํฐ๋ก ๋ณํํ๊ฑฐ๋ ์ถ๊ฐ ์์ ์ ํ๋ ํด๋์ค๋ค.
FilterInputStream
ํํฐ ๋ฐ์ดํธ ์ ๋ ฅ ์คํธ๋ฆผ์ ์ต์์ ํด๋์ค. FilterInputSteam ํด๋์ค์ ๊ทธ ํ์ ํด๋์ค๋ ์์ฑํ ๋์ ๋ค๋ฅธ ์คํธ๋ฆผ ๊ฐ์ฒด๋ฅผ ์์ฑ์๋ก ๋ฐ๋๋ค. ๋ฐ๋ผ์ ๊ธฐ๋ณธ ์คํธ๋ฆผ์ ํน์ ๊ธฐ๋ฅ์ ๊ฐ์ง ํํฐ ์คํธ๋ฆผ์ ์ฐ๊ฒฐํ๋ฉด ์ํ๋ ๋์์ ํ ์์๋ค.
FilterOutputStream
ํํฐ ๋ฐ์ดํธ ์ถ๋ ฅ ์คํธ๋ฆผ์ ์ต์์ ํด๋์ค. FilterOutputSteam ํด๋์ค์ ๊ทธ ํ์ ํด๋์ค๋ ์์ฑํ ๋์ ๋ค๋ฅธ ์คํธ๋ฆผ ๊ฐ์ฒด๋ฅผ ์์ฑ์๋ก ๋ฐ๋๋ค. ๋ณธ ์คํธ๋ฆผ์ ํน์ ๊ธฐ๋ฅ์ ๊ฐ์ง ํํฐ ์คํธ๋ฆผ์ ์ฐ๊ฒฐํ๋ฉด ์ํ๋ ๋์์ ํ ์ ์๋ค.
package ch14.unit03;
import java.io.FileOutputStream;
public class Ex01_FileOutputStream {
public static void main(String[] args) {
String pathname="test.txt";
FileOutputStream fos=null;
int data;
try {
//FileOutputStream : ํ์ผ byte ์ถ๋ ฅ ์คํธ๋ฆผ
// : ํ์ผ์ด ์์ผ๋ฉด ๋ง๋ค๊ณ , ์์ผ๋ฉด ์ง์ฐ๊ณ ๋ง๋ฆ
fos=new FileOutputStream(pathname);
System.out.println("๋ฌธ์์ด ์
๋ ฅ[์ข
๋ฃ:Ctrl+z]...");
while((data = System.in.read()) != -1){
fos.write(data); // ํ์ผ์ ์ ์ฅ
}
System.out.println("ํ์ผ ์ ์ฅ ์๋ฃ...");
} catch (Exception e) {
e.printStackTrace();
}finally {
if(fos!=null) {
try {
fos.close();
} catch (Exception e2) {
}
}
}
}
}
package ch14.unit03;
import java.io.FileOutputStream;
import java.io.IOException;
public class Ex02_FileOutputStream {
public static void main(String[] args) {
int data;
String pathname = "test.txt";
// JDK 7.0 ์ด์, ์๋ close
try (FileOutputStream fos = new FileOutputStream(pathname)) {
System.out.println("๋ด์ฉ ์
๋ ฅ[Ctrl+z์ข
๋ฃ]...");
while ((data = System.in.read()) != -1) {
fos.write(data);
}
fos.flush();
System.out.println("ํ์ผ ์ ์ฅ ์๋ฃ!!!!");
} catch (IOException e) {
e.printStackTrace();
}
}
}
package ch14.unit03;
import java.io.FileOutputStream;
import java.io.IOException;
public class Ex03_FileOutputStream {
public static void main(String[] args) {
int data;
String pathname = "test.txt";
// true: append ์์ผ๋ฉด ๋ง๋ค๊ณ ์์ผ๋ฉด ์ถ๊ฐ
try (FileOutputStream fos = new FileOutputStream(pathname, true)) {
System.out.println("๋ด์ฉ ์
๋ ฅ[Ctrl+z์ข
๋ฃ]...");
while ((data = System.in.read()) != -1) {
fos.write(data);
}
fos.flush();
System.out.println("ํ์ผ ์ ์ฅ ์๋ฃ!!!!");
} catch (IOException e) {
e.printStackTrace();
}
}
}
package ch14.unit03;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class Ex11_FileOutputStream {
public static void main(String[] args) {
String pathname="text.txt";
int data;
try (FileInputStream fis = new FileInputStream(pathname)) {
while ((data = fis.read()) != -1) {
System.out.write(data);
}
} catch (FileNotFoundException e) {
System.out.println(pathname + " ํ์ผ์ด ์์");
} catch (IOException e) {
e.printStackTrace();
}
}
}
'๐ป > JAVA' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
(35) [JAVA] 19์ผ (1) | 2024.04.10 |
---|---|
(34) [JAVA] 18์ผ ์ ์ถ๋ ฅ ์คํธ๋ฆผ(I/O Stream) - 2 (0) | 2024.04.03 |
[Java] 17์ผ์ฐจ : ์ปฌ๋ ์ (Collection) - 4 (0) | 2024.03.11 |
[Java] 17์ผ์ฐจ : ์ปฌ๋ ์ (Collection) - 3 (0) | 2024.03.11 |
[Java] 16์ผ์ฐจ : ์ปฌ๋ ์ (Collection) - 2 (0) | 2024.03.11 |