CodeGym /Java Course /๋ชจ๋“ˆ 2: ์ž๋ฐ” ์ฝ”์–ด /ํ‘œ์ค€ ์ธํ„ฐํŽ˜์ด์Šค: InputStream, OutputStream

ํ‘œ์ค€ ์ธํ„ฐํŽ˜์ด์Šค: InputStream, OutputStream

๋ชจ๋“ˆ 2: ์ž๋ฐ” ์ฝ”์–ด
๋ ˆ๋ฒจ 4 , ๋ ˆ์Šจ 6
์‚ฌ์šฉ ๊ฐ€๋Šฅ

"์•ˆ๋…•ํ•˜์„ธ์š”, ์•„๋ฏธ๊ณ ! ์ด์ œ InputStream ๊ณผ OutputStream ์ด๋ผ๋Š” ๋‘ ๊ฐ€์ง€ ์ธํ„ฐํŽ˜์ด์Šค์— ๋Œ€ํ•ด ๋ง์”€๋“œ๋ฆฌ๊ฒ ์Šต๋‹ˆ๋‹ค . ๊ทธ๊ฒƒ๋“ค์€ ์ถ”์ƒ ํด๋ž˜์Šค๋กœ ์„ ์–ธ๋˜์ง€๋งŒ ์ž์„ธํžˆ ์‚ดํŽด๋ณด๋ฉด ๋ณธ์งˆ์ ์œผ๋กœ ์ธํ„ฐํŽ˜์ด์Šค๋ผ๋Š” ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค . ๊ฑฐ์˜ ๋ชจ๋“  ๋ฉ”์„œ๋“œ๋Š” ์ถ”์ƒ์ž…๋‹ˆ๋‹ค. ์˜ˆ์™ธ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ๋ช‡ ๊ฐ€์ง€ ์ค‘์š”ํ•˜์ง€ ์•Š์€ ๋ฐฉ๋ฒ•์ด ์žˆ์Šต๋‹ˆ๋‹ค. ์ด์ „์— ๊ณ ๋ คํ•œ " ๊ฒฝํ˜ธ์› " ๊ณผ ๋งค์šฐ ์œ ์‚ฌํ•ฉ๋‹ˆ๋‹ค ."

์ด๋“ค์€ ๋งค์šฐ ํฅ๋ฏธ๋กœ์šด ์ธํ„ฐํŽ˜์ด์Šค์ž…๋‹ˆ๋‹ค. ์ง€๊ธˆ์€ ์˜๋„์ ์œผ๋กœ ๊ทธ๊ฒƒ๋“ค์„ ์ธํ„ฐํŽ˜์ด์Šค ๋ผ๊ณ  ๋ถ€๋ฅด๊ฒ ์Šต๋‹ˆ๋‹ค . ์™œ ์šฐ๋ฆฌ๊ฐ€ ๊ทธ๊ฒƒ๋“ค์ด ํ•„์š”ํ•œ์ง€ ์ดํ•ดํ•˜์‹ค ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ ๋‹ค์Œ ์ด๋“ค์ด ์‹ค์ œ๋กœ ์ถ”์ƒ ํด๋ž˜์Šค์ธ ์ด์œ ์— ๋Œ€ํ•ด ์ด์•ผ๊ธฐํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

"์ข‹์•„์š”. ๊ทธ๋ ‡๋‹ค๋ฉด ์ด ์ธํ„ฐํŽ˜์ด์Šค๋Š” ๋ฌด์—‡์ž…๋‹ˆ๊นŒ?"

"๋” ๊ณ ๋ฏผํ•˜์ง€ ์•Š๊ณ  ๋ง์”€๋“œ๋ฆฌ๊ฒ ์Šต๋‹ˆ๋‹ค."

Java์—๋Š” ยซ ์ŠคํŠธ๋ฆผ ยป์ด๋ผ๋Š” ํฅ๋ฏธ๋กœ์šด ๊ธฐ๋Šฅ์ด ์žˆ์Šต๋‹ˆ๋‹ค. ์ŠคํŠธ๋ฆผ์€ ๋งค์šฐ ๋‹จ์ˆœํ•œ ์—”ํ„ฐํ‹ฐ์ž…๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๊ทธ ๋‹จ์ˆœ์„ฑ์€ ๋ฐ์ดํ„ฐ๋ฅผ ๊ตํ™˜ํ•˜๋Š” ๋งค์šฐ ๊ฐ•๋ ฅํ•œ ๋ฐฉ๋ฒ•์˜ ํ•ต์‹ฌ์ž…๋‹ˆ๋‹ค. ์ŠคํŠธ๋ฆผ์—๋Š” ์ฝ๊ธฐ์šฉ ์ŠคํŠธ๋ฆผ๊ณผ ์“ฐ๊ธฐ์šฉ ์ŠคํŠธ๋ฆผ ์˜ ๋‘ ๊ฐ€์ง€ ์œ ํ˜•์ด ์žˆ์Šต๋‹ˆ๋‹ค .

์ด๋ฏธ ์ง์ž‘ํ•˜์…จ๊ฒ ์ง€๋งŒ ์“ฐ๊ธฐ๋ฅผ ์œ„ํ•ด ๋ฐ์ดํ„ฐ๋ฅผ ์ŠคํŠธ๋ฆผ ์— ์“ธ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค . ์ด๋ฅผ ์œ„ํ•œ ์“ฐ๊ธฐ ๋ฐฉ๋ฒ• ์ด ์žˆ์Šต๋‹ˆ๋‹ค . ์ฝ๊ธฐ ์œ„ํ•ด ์ŠคํŠธ๋ฆผ ์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ์ฝ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค . ์ด๋ฅผ ์œ„ํ•œ read () ๋ฉ”์„œ๋“œ ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค .

InputStream  ์€ ์ฝ๊ธฐ๋ฅผ ์ง€์›ํ•˜๋Š” ์ŠคํŠธ๋ฆผ์˜ ์ธํ„ฐํŽ˜์ด์Šค์ž…๋‹ˆ๋‹ค. ๊ทธ๊ฒƒ์€ ๋‹ค์Œ ๋Šฅ๋ ฅ์„ ์ •์˜ํ•ฉ๋‹ˆ๋‹ค: ยซ๋‚˜์—๊ฒŒ์„œ ๋ฐ”์ดํŠธ๋ฅผ ์ฝ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹คยป.

๋งˆ์ฐฌ๊ฐ€์ง€๋กœ OutputStream ์€ ์“ฐ๊ธฐ๋ฅผ ์ง€์›ํ•˜๋Š” ์ŠคํŠธ๋ฆผ์˜ ์ธํ„ฐํŽ˜์ด์Šค์ž…๋‹ˆ๋‹ค. ๊ทธ๊ฒƒ์€ ๋‹ค์Œ ๋Šฅ๋ ฅ์„ ์ •์˜ํ•ฉ๋‹ˆ๋‹ค: ยซbytes can be write to meยป.

"๊ทธ๊ฒŒ ๋‹ค์•ผ?"

"๋Œ€๋‹จํžˆ ๊ทธ๋ ‡์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ์š”์ ์€ Java์— InputStream ๋ฐ OutputStream ๊ณผ ํ•จ๊ป˜ ์ž‘๋™ํ•  ์ˆ˜ ์žˆ๋Š” ๋งŽ์€ ํด๋ž˜์Šค ๊ฐ€ ์žˆ๋‹ค๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ๋””์Šคํฌ์—์„œ ํŒŒ์ผ์„ ์ฝ๊ณ  ๊ทธ ๋‚ด์šฉ์„ ํ™”๋ฉด์— ํ‘œ์‹œํ•˜๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์ด๋ณด๋‹ค ๋” ์‰ฌ์šธ ์ˆ˜๋Š” ์—†์Šต๋‹ˆ๋‹ค."

๋””์Šคํฌ์˜ ํŒŒ์ผ์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ์ฝ๊ธฐ ์œ„ํ•ด InputStream ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ตฌํ˜„ํ•˜๋Š” ํŠน์ˆ˜ FileInputStream ํด๋ž˜์Šค๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค . ์ด ๋ฐ์ดํ„ฐ๋ฅผ ๋‹ค๋ฅธ ํŒŒ์ผ์— ์“ฐ์‹œ๊ฒ ์Šต๋‹ˆ๊นŒ? ์ด๋ฅผ ์œ„ํ•ด OutputStream ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ตฌํ˜„ํ•˜๋Š” FileOutputStream ํด๋ž˜์Šค๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค . ๋‹ค์Œ ์ฝ”๋“œ๋Š” ํ•œ ํŒŒ์ผ์—์„œ ๋‹ค๋ฅธ ํŒŒ์ผ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ๋ณต์‚ฌํ•˜๊ธฐ ์œ„ํ•ด ์ˆ˜ํ–‰ํ•ด์•ผ ํ•˜๋Š” ์ž‘์—…์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค.

์•”ํ˜ธ
public static void main(String[] args) throws IOException
{
 InputStream inStream = new FileInputStream("c:/source.txt");
 OutputStream outStream = new FileOutputStream("c:/result.txt");

 while (inStream.available() > 0)
 {
  int data = inStream.read(); //read one byte from the input stream
  outStream.write(data); //write that byte to the other stream.
 }

 inStream.close(); //close the streams
 outStream.close();
}

ํด๋ž˜์Šค๋ฅผ ์ž‘์„ฑํ•˜๊ณ   ์—ฌ๊ธฐ์— InputStream  ๋ฐ  OutputStream ๊ธฐ๋Šฅ์„ ์ถ”๊ฐ€ํ–ˆ๋‹ค๊ณ  ์ƒ์ƒํ•ด ๋ณด์‹ญ์‹œ์˜ค.

์ด๋Ÿฌํ•œ ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์ œ๋Œ€๋กœ ๊ตฌํ˜„ํ–ˆ๋‹ค๋ฉด ์ด์ œ ํด๋ž˜์Šค์˜ ์ธ์Šคํ„ด์Šค๋ฅผ ํŒŒ์ผ์— ์ €์žฅํ•˜๊ฑฐ๋‚˜ ํŒŒ์ผ์—์„œ ์ฝ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. read ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋‚ด์šฉ์„ ์ฝ๊ธฐ๋งŒ ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค . ๋˜๋Š” ๊ฐœ์ฒด๋ฅผ ๋งŒ๋“ค๊ณ  write ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํŒŒ์ผ ๋‚ด์šฉ์„ ์ž‘์„ฑํ•˜์—ฌ ํŒŒ์ผ์—์„œ ๋กœ๋“œํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

"์˜ˆ๋ฅผ ๋“ค์–ด?"

"ํ™•์‹ ํ•˜๋Š”."

์•”ํ˜ธ ์„ค๋ช…
class MyClass
{
private ArrayList<Integer> list;
}
๊ฐ„๋‹จํžˆ ํ•˜๊ธฐ ์œ„ํ•ด ์šฐ๋ฆฌ ํด๋ž˜์Šค์— ์ •์ˆ˜๋ฅผ ํฌํ•จํ•˜๋Š” ArrayList๋ผ๋Š” ํ•˜๋‚˜์˜ ๊ฐœ์ฒด๊ฐ€ ํฌํ•จ๋˜์–ด ์žˆ๋‹ค๊ณ  ์ƒ์ƒํ•ด ๋ณด์‹ญ์‹œ์˜ค.

์ด์ œ ์ฝ๊ธฐ ๋ฐ ์“ฐ๊ธฐ ๋ฉ”์„œ๋“œ๋ฅผ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค.

์•”ํ˜ธ ์„ค๋ช…
class MyClass
{
private ArrayList<Integer> list;
public void write(int data)
{
list.add(data);
}
public int read()
{
int first = list.get(0);
list.remove(0);
return first;
}

public int available()
{
return list.size();
}
}
์ด์ œ ์šฐ๋ฆฌ ํด๋ž˜์Šค๋Š” list ์˜ ์ „์ฒด ๋‚ด์šฉ์„ ์ˆœ์ฐจ์ ์œผ๋กœ ์ฝ์„ ์ˆ˜ ์žˆ๋„๋ก ํ•˜๋Š” read ๋ฉ”์„œ๋“œ๋ฅผ ๊ตฌํ˜„ํ•ฉ๋‹ˆ๋‹ค .

๊ทธ๋ฆฌ๊ณ  ๋ชฉ๋ก์— ๊ฐ’์„ ์“ธ ์ˆ˜ ์žˆ๋Š” write ๋ฉ”์†Œ๋“œ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.

๋ฌผ๋ก  ์ด๊ฒƒ์€ InputStream ๋ฐ OutputStream ์ธํ„ฐํŽ˜์ด์Šค์˜ ๊ตฌํ˜„์€ ์•„๋‹ˆ์ง€๋งŒ ๋งค์šฐ ์œ ์‚ฌํ•ฉ๋‹ˆ๋‹ค.

"์˜ˆ, ์•Œ๊ฒ ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋ฉด ๊ทธ๋Ÿฌํ•œ ๊ฐœ์ฒด์˜ ๋‚ด์šฉ์„ ํŒŒ์ผ์— ์–ด๋–ป๊ฒŒ ์ €์žฅํ•ฉ๋‹ˆ๊นŒ?"

"์˜ˆ๋ฅผ ๋“ค์–ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค."

ํŒŒ์ผ์— MyClass ๊ฐ์ฒด ์“ฐ๊ธฐ
public static void main(String[] args)
{
 MyClass myObject = new MyClass();
 OutputStream outStream = new FileOutputStream ("c:/my-object-data.txt");

 while (myObject.available() > 0)
 {
  int data = myObject.read(); //read one int from the input stream
  outStream.write(data); //write that int to the other stream.
 }

 outStream.close();
}
ํŒŒ์ผ์—์„œ MyClass ๊ฐ์ฒด ์ฝ๊ธฐ
public static void main(String[] args)
{
 InputStream inStream = new FileInputStream("c:/my-object-data.txt");
 MyClass myObject = new MyClass();

 while (inStream.available() > 0)
 {
  int data = inStream.read(); //read one int from the input stream
  myObject.write(data); //write that int to the other stream.
 }

 inStream.close(); //close the streams
}

"์ด๋Ÿฐ ๋ชฐ๋ฆฌ! ์ •๋ง InputStream/OutputStream์œผ๋กœ ์ž‘์—…ํ•˜๋Š” ๊ฒƒ๊ณผ ๋งค์šฐ ์œ ์‚ฌํ•ฉ๋‹ˆ๋‹ค. ์ŠคํŠธ๋ฆผ์ด ํ›Œ๋ฅญํ•ฉ๋‹ˆ๋‹ค!"

"๋‹ค์Œ ๋ช‡ ๊ฐ€์ง€!"

์ฝ”๋ฉ˜ํŠธ
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION