์ ์ถ๋ ฅ 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) {}
...
Author And Source
์ด ๋ฌธ์ ์ ๊ดํ์ฌ(์ ์ถ๋ ฅ I/O), ์ฐ๋ฆฌ๋ ์ด๊ณณ์์ ๋ ๋ง์ ์๋ฃ๋ฅผ ๋ฐ๊ฒฌํ๊ณ ๋งํฌ๋ฅผ ํด๋ฆญํ์ฌ ๋ณด์๋ค https://velog.io/@heeboventure/์ ์ถ๋ ฅ-IO์ ์ ๊ท์: ์์์ ์ ๋ณด๊ฐ ์์์ URL์ ํฌํจ๋์ด ์์ผ๋ฉฐ ์ ์๊ถ์ ์์์ ์์ ์ ๋๋ค.
์ฐ์ํ ๊ฐ๋ฐ์ ์ฝํ
์ธ ๋ฐ๊ฒฌ์ ์ ๋
(Collection and Share based on the CC Protocol.)
์ข์ ์นํ์ด์ง ์ฆ๊ฒจ์ฐพ๊ธฐ
๊ฐ๋ฐ์ ์ฐ์ ์ฌ์ดํธ ์์ง
๊ฐ๋ฐ์๊ฐ ์์์ผ ํ ํ์ ์ฌ์ดํธ 100์ ์ถ์ฒ ์ฐ๋ฆฌ๋ ๋น์ ์ ์ํด 100๊ฐ์ ์์ฃผ ์ฌ์ฉํ๋ ๊ฐ๋ฐ์ ํ์ต ์ฌ์ดํธ๋ฅผ ์ ๋ฆฌํ์ต๋๋ค