์ž…์ถœ๋ ฅ I/O

Stream ์ž…์ถœ๋ ฅ

byte ๋‹จ์œ„์—์„œ์˜ ํ†ต์‹ 
๋ฌธ์ž๋ฅผ ์ž…์ถœ๋ ฅํ•˜๋ฉด ascii ์ฝ”๋“œ์— ๋”ฐ๋ผ ์ˆซ์ž๋กœ ๋‚˜์˜จ๋‹ค.

Stream ์ž…๋ ฅ

1. ์ฝ˜์†”

FileOutputStream fos = new FileOutputStream(FileDescriptor.out);
BufferedOutputStream bos = new BufferedOutputStream(fos, 1024);

--- 
1024 byte : ์‚ฌ์šฉํ•  buffer์˜ ํฌ๊ธฐ. ์ „์†กํ•˜๊ณ ์ž ํ•˜๋Š” buffer์˜ ์‚ฌ์ด์ฆˆ๋งŒํผ์˜ ๊ณต๊ฐ„๋งŒํผ 
๋ฐ์ดํ„ฐ๋ฅผ ์ž…๋ ฅ๋ฐ›์•˜๋‹ค๊ฐ€ ๊ทธ ์˜์—ญ์˜ ๋ฐ์ดํ„ฐ๋งŒํผ์„ ์Œ“์•„๋‘์—ˆ๋‹ค๊ฐ€ ํ•œ ๋ฒˆ์— ์ถœ๋ ฅํ•  ์ˆ˜ ์žˆ๋„๋ก ํ˜•ํƒœ๋ฅผ ๊ตฌ์„ฑ.
---

DataOutputStream dos = new DataOutputStream(bos);

--- 
DataOutputStream : ์›์‹œ ๋ฐ์ดํ„ฐ ํƒ€์ž…(boolean, integer, ...)์˜ ๊ฐ๊ฐ์˜ ๊ฐ’์œผ๋กœ์„œ ๋ฐ์ดํ„ฐ๋ฅผ 
์ž…๋ ฅํ•˜๊ณ  ์ถœ๋ ฅํ•  ์ˆ˜ ์žˆ๋„๋ก ๋„์™€์ฃผ๋Š” ์—ฌ๋Ÿฌ๊ฐ€์ง€ ๋ฉ”์†Œ๋“œ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ํด๋ž˜์Šค
---

dos.write...

2. ํŒŒ์ผ

File file = new File("ํŒŒ์ผ์ด๋ฆ„");
FileOutputStream fos = new FileOutputStream(file, false);

----parameter์˜ boolean๊ฐ’: ํŒŒ์ผ ๋‚ด์šฉ์„ append์ถ”๊ฐ€(true) / ๋ฎ์–ด์“ฐ๊ธฐ(false) ------

BufferedOutputStream bos = new BufferedOutputStream(fos, 1024);
DataOutputStream dos = new DataOutputStream(bos);
dos.write...

3. ๋„คํŠธ์›Œํฌ

Socket so = new Socket(...);

--- 
๋„คํŠธ์›Œํฌ ํ†ต์‹ . ์›๊ฒฉ์— ์žˆ๋Š” ๋‹ค๋ฅธ ์ปดํ“จํ„ฐ. 
Socket ํด๋ž˜์Šค๋ฅผ ์ด์šฉํ•˜๋ฉด ๋žœ์„ ์œผ๋กœ ์—ฐ๊ฒฐ๋œ ์ธํ„ฐ๋„ท ๋“ฑ์„ ํ™œ์šฉํ•  ์ˆ˜ ์žˆ๋Š” API ์ œ๊ณตํ•˜๊ณ  ์žˆ์Œ. 
์ด ํด๋ž˜์Šค์˜ ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ค๊ฒŒ ๋˜๋ฉด ๋žœ์„ ์„ ํ†ตํ•ด ๋ฐ์ดํ„ฐ๋ฅผ ์ฃผ๊ณ ๋ฐ›์„ ์ˆ˜ ์žˆ๋Š” outputstream ์ด๋ผ๋Š” ๊ฐ์ฒด๋ฅผ ๊ฐ€์ง.
---

BufferedOutputStream bos = new BufferedOutputStream(soc.getOutputStream(), 1024);
DataOutputStream dos = new DataOutputStream(bos);
dos.write...

์ฐธ๊ณ 

  • ๋ช‡ byte ์•ˆ ๋˜๋Š” ๊ฑด FileOutputStream๋งŒ ์จ๋„ ๋˜๊ณ 
  • ๋งŽ์€ ์–‘์˜ ๋ฐ์ดํ„ฐ๋ฅผ ๊ด€๋ฆฌํ•ด์•ผํ•˜๋ฉด BufferedOutputStream,
  • ๋‹ค์–‘ํ•œ ํ˜•ํƒœ๋กœ ์ž…์ถœ๋ ฅ์„ ํ•ด์•ผํ•˜๋ฉด DataOutputStream ์„ ์“ฐ๋ฉด ๋œ๋‹ค.
  • ์„ธ ๊ฐ€์ง€๋ฅผ ๋ชจ๋‘ ๋‹ค ์จ์•ผ ํ•˜๋Š” ๊ฑด ์•„๋‹˜.

์˜ˆ์‹œ

dos.write(์ถœ๋ ฅํ•˜๊ณ ์žํ•˜๋Š”๊ฒƒ); --- ์ถœ๋ ฅ
dos.flush(); --- ํ™”๋ฉด ์ƒ์— ๋ฟŒ๋ ค์ ธ๋ผ. buffer์— ์Œ“์—ฌ์žˆ๋˜ ๊ฑธ ๋น„์šฐ๋Š” ๊ฒƒ. ์ถœ๋ ฅํ•ด๋ž.
dos.close(); --- ๋‹ค ๋๋‚ฌ์œผ๋ฉด ๋‹ซ๊ธฐ

Stream ์ž…๋ ฅ

์ฝ˜์†”, ํŒŒ์ผ, ๋„คํŠธ์›Œํฌ

  • ์œ„์— stream ์ถœ๋ ฅ๊ณผ ๋™์ผํ•œ๋ฐ
  • Output ๋Œ€์‹  Input ์ด๋‹ค.
FileInputStream fis = new FileInputStream(FileDescriptor.in);
...
dis.read ...

ํ…์ŠคํŠธ ์ž…์ถœ๋ ฅ

2 byte ๋‹จ์œ„์˜ ํ…์ŠคํŠธ ์ž…์ถœ๋ ฅ
๋ฌธ์ž๋ฅผ ๋„ฃ์œผ๋ฉด ๋ฌธ์ž ๊ทธ๋Œ€๋กœ ์ž…์ถœ๋ ฅ

ํ…์ŠคํŠธ ์ถœ๋ ฅ

์ฝ˜์†”, ํŒŒ์ผ, ๋„คํŠธ์›Œํฌ

์ฝ˜์†”
OutputStreamWriter osw = new OutputStreamWriter(System.out);
BufferedWriter bw = new BufferedWriter(osw);
PrintWriter pw = new PrintWriter(bw);
pw.println(...);

ํ…์ŠคํŠธ ์ž…๋ ฅ

์ฝ˜์†”, ํŒŒ์ผ, ๋„คํŠธ์›Œํฌ

  • ์ฝ˜์†” ์˜ˆ์‹œ
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);

System.out.print("์ด๋ฆ„ = ");
String name = br.readLine();
System.out.println("๋‹น์‹ ์˜ ์ด๋ฆ„์€ " + name + "์ž…๋‹ˆ๋‹ค.");

--- readLine()์€ enterํ‚ค ์น  ๋•Œ๊นŒ์ง€ ์ž…๋ ฅ ๋ฐ›๊ฒ ๋‹ค๋Š” ๋œป ---

์ด๋Ÿฌ๋ฉด console์ฐฝ์—
์ด๋ฆ„ = 
๋‚˜์™€์„œ ์ž…๋ ฅํ•˜๊ณ  enter ์น˜๋ฉด ๋ฌธ์žฅ ์ถœ๋ ฅ๋จ
  • ํŒŒ์ผ ์˜ˆ์‹œ
File file = new File("C:/java/aaa.txt");
InputStreamReader isr = new InputStreamReader(file);
BufferedReader br = new BufferedReader(isr);

while(true) {
   String str = br.readLine();
   if(str == null) break;
   System.out.println("ํ•œ ์ค„์”ฉ ์ฝ์€ ๋‚ด์šฉ = " + str);
}
br.close();

--- br.readLine() ํ•˜๋ฉด ํŒŒ์ผ์—์„œ ํ•œ ์ค„์”ฉ ์ฝ์Œ ---

๊ฐ์ฒด ์ž…์ถœ๋ ฅ

๊ฐ์ฒด ์ถœ๋ ฅ

ํŒŒ์ผ, ๋„คํŠธ์›Œํฌ

  • ํŒŒ์ผ
File file = new File("ํŒŒ์ผ์ด๋ฆ„");
FileOutputStream fos = new FileOutputStream(file);
BufferedOutputStream bos = new BufferedOutputStream(fos);
ObjectOutputStream oos = new ObjectOutputStream(bos);
oos.writeObject(...);
...

๊ฐ์ฒด ์ž…๋ ฅ

ํŒŒ์ผ, ๋„คํŠธ์›Œํฌ

OutputStream --> InputStream

...
ObjectInputStream ois = new ObjectInputStream(bis);
try {
   Object obj = ois.readObject();
} catch(ClassNotFoundException e) {}
...

์ข‹์€ ์›นํŽ˜์ด์ง€ ์ฆ๊ฒจ์ฐพ๊ธฐ