CodeGym /Java Blog /๋ฌด์ž‘์œ„์˜ /์ž๋ฐ” PrintStream ํด๋ž˜์Šค
John Squirrels
๋ ˆ๋ฒจ 41
San Francisco

์ž๋ฐ” PrintStream ํด๋ž˜์Šค

๋ฌด์ž‘์œ„์˜ ๊ทธ๋ฃน์— ๊ฒŒ์‹œ๋˜์—ˆ์Šต๋‹ˆ๋‹ค
์•ˆ๋…•! ์˜ค๋Š˜ ์šฐ๋ฆฌ๋Š” Java PrintStream ํด๋ž˜์Šค์™€ ๊ทธ๊ฒƒ์ด ํ•  ์ˆ˜ ์žˆ๋Š” ๋ชจ๋“  ๊ฒƒ์— ๋Œ€ํ•ด ์ด์•ผ๊ธฐํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์‹ค์ œ๋กœ PrintStream ํด๋ž˜์Šค ์˜ ๋‘ ๊ฐ€์ง€ ๋ฉ”์„œ๋“œ์— ์ด๋ฏธ ์ต์ˆ™ํ•ฉ๋‹ˆ๋‹ค . ๊ทธ๊ฒƒ๋“ค์€ print() ์™€ println() ์ธ๋ฐ, ์—ฌ๋Ÿฌ๋ถ„์€ ์•„๋งˆ๋„ ๋งค์ผ ์‚ฌ์šฉํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค :) System.out ๋ณ€์ˆ˜๋Š” PrintStream ๊ฐ์ฒด์ด๊ธฐ ๋•Œ๋ฌธ์— System.out.println() ์„ ํ˜ธ์ถœํ•  ๋•Œ ์ด ํด๋ž˜์Šค์˜ ๋ฉ”์„œ๋“œ ์ค‘ ํ•˜๋‚˜๋ฅผ ํ˜ธ์ถœํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค .โ€ฏ PrintStream ํด๋ž˜์Šค ์˜ ์ผ๋ฐ˜์ ์ธ ๋ชฉ์ ์€ ์ผ๋ถ€ ์ŠคํŠธ๋ฆผ์— ์ •๋ณด๋ฅผ ๋ณด๋‚ด๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค.โ€ฏPrintStream ํด๋ž˜์Šค๊ฐ€ ํ•„์š”ํ•œ ์ด์œ  - 1์ด ํด๋ž˜์Šค์—๋Š” ์—ฌ๋Ÿฌ ์ƒ์„ฑ์ž๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ๋‹ค์Œ์€ ๊ฐ€์žฅ ์ผ๋ฐ˜์ ์œผ๋กœ ์‚ฌ์šฉ๋˜๋Š” ๋ช‡ ๊ฐ€์ง€์ž…๋‹ˆ๋‹ค.
  • PrintStream(์ถœ๋ ฅ์ŠคํŠธ๋ฆผ ์ถœ๋ ฅ์ŠคํŠธ๋ฆผ)
  • PrintStream(ํŒŒ์ผ ์ถœ๋ ฅ ํŒŒ์ผ)์ด FileNotFoundException์„ ๋ฐœ์ƒ์‹œํ‚ต๋‹ˆ๋‹ค.
  • PrintStream(String outputFileName)์ด FileNotFoundException์„ ๋ฐœ์ƒ์‹œํ‚ต๋‹ˆ๋‹ค.
์˜ˆ๋ฅผ ๋“ค์–ด ์ถœ๋ ฅ ํŒŒ์ผ์˜ ์ด๋ฆ„์„ PrintStream ์ƒ์„ฑ์ž์— ์ „๋‹ฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋˜๋Š” File ๊ฐ์ฒด๋ฅผ ์ „๋‹ฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๊ฒƒ์ด ์–ด๋–ป๊ฒŒ ์ž‘๋™ํ•˜๋Š”์ง€ ์•Œ์•„๋ณด๊ธฐ ์œ„ํ•ด ๋ช‡ ๊ฐ€์ง€ ์˜ˆ๋ฅผ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

import java.io.File; 
import java.io.FileNotFoundException; 
import java.io.PrintStream; 

public class Main { 

   public static void main(String arr[]) throws FileNotFoundException 
   { 
       PrintStream filePrintStream = new PrintStream(new File("C:\\Users\\Username\\Desktop\\test.txt")); 

       filePrintStream.println(222); 
       filePrintStream.println("Hello world"); 
       filePrintStream.println(false); 

   } 
}
์ด ์ฝ”๋“œ๋Š” ๋ฐ”ํƒ• ํ™”๋ฉด์— test.txt ํŒŒ์ผ์„ ๋งŒ๋“ค๊ณ (์•„์ง ์กด์žฌํ•˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ) ์ˆซ์ž, ๋ฌธ์ž์—ด ๋ฐ ๋ถ€์šธ์„ ์ˆœ์ฐจ์ ์œผ๋กœ ์”๋‹ˆ๋‹ค. ํ”„๋กœ๊ทธ๋žจ์„ ์‹คํ–‰ํ•œ ํ›„์˜ ํŒŒ์ผ ๋‚ด์šฉ์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

222 
Hello world!
false
์œ„์—์„œ ๋งํ–ˆ๋“ฏ์ด File ๊ฐœ์ฒด๋ฅผ ์ „๋‹ฌํ•  ํ•„์š”๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค. ๋‹จ์ˆœํžˆ ํŒŒ์ผ ๊ฒฝ๋กœ๋ฅผ ์ƒ์„ฑ์ž์— ์ „๋‹ฌํ•˜๋Š” ๊ฒƒ์œผ๋กœ ์ถฉ๋ถ„ํ•ฉ๋‹ˆ๋‹ค.

import java.io.FileNotFoundException; 
import java.io.PrintStream; 

public class Main { 

   public static void main(String arr[]) throws FileNotFoundException 
   { 
       PrintStream filePrintStream = new PrintStream("C:\\Users\\Username\\Desktop\\test.txt"); 

       filePrintStream.println(222); 
       filePrintStream.println("Hello world"); 
       filePrintStream.println(false); 
   } 
}
์ด ์ฝ”๋“œ๋Š” ์ด์ „ ์ฝ”๋“œ์™€ ๋™์ผํ•ฉ๋‹ˆ๋‹ค. ์ฃผ๋ชฉํ•  ๊ฐ€์น˜๊ฐ€ ์žˆ๋Š” ๋˜ ๋‹ค๋ฅธ ํฅ๋ฏธ๋กœ์šด ๋ฐฉ๋ฒ•์€ ํ˜•์‹ ๋ฌธ์ž์—ด์„ ๊ธฐ๋ฐ˜์œผ๋กœ ์ถœ๋ ฅ์„ ์ƒ์„ฑํ•˜๋Š” printf() ์ž…๋‹ˆ๋‹ค. "ํ˜•์‹ ๋ฌธ์ž์—ด"์ด๋ž€ ๋ฌด์—‡์ž…๋‹ˆ๊นŒ? ์˜ˆ๋ฅผ ๋“ค์–ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

import java.io.IOException; 
import java.io.PrintStream; 

public class Main { 

   public static void main(String[] args) throws IOException { 

       PrintStream printStream = new PrintStream("C:\\Users\\Steve\\Desktop\\test.txt");

       printStream.println("Hello!"); 
       printStream.println("I'm a robot!"); 

       printStream.printf("My name is %s. I am %d!", "Amigo", 18); 

       printStream.close(); 
   } 
}
์—ฌ๊ธฐ์—์„œ ๋ฌธ์ž์—ด์— ๋กœ๋ด‡์˜ ์ด๋ฆ„๊ณผ ๋‚˜์ด๋ฅผ ๋ช…์‹œ์ ์œผ๋กœ ์ง€์ •ํ•˜๋Š” ๋Œ€์‹  ์ด ์ •๋ณด์— ๋Œ€ํ•œ ์ž๋ฆฌ ํ‘œ์‹œ์ž๋ฅผ %s ๋ฐ %d ๋กœ ํ‘œ์‹œํ•ฉ๋‹ˆ๋‹ค . ๊ทธ๋ฆฌ๊ณ  ์ด๋ฅผ ๋Œ€์ฒดํ•  ๋ฐ์ดํ„ฐ๋ฅผ ์ธ์ˆ˜๋กœ ์ „๋‹ฌํ•ฉ๋‹ˆ๋‹ค. ์šฐ๋ฆฌ์˜ ๊ฒฝ์šฐ ์ด๊ฒƒ์€ ๋ฌธ์ž์—ด " Amigo "์™€ ์ˆซ์ž 18์ž…๋‹ˆ๋‹ค. ๋‹ค๋ฅธ ์ž๋ฆฌ ํ‘œ์‹œ์ž %b ๋ฅผ ๋งŒ๋“ค๊ณ  ๋‹ค๋ฅธ ์ธ์ˆ˜๋ฅผ ์ „๋‹ฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๊ฒƒ์ด ์™œ ํ•„์š”ํ•œ๊ฐ€์š”?โ€ฏ๋ฌด์—‡๋ณด๋‹ค๋„ ๋” ํฐ ์œ ์—ฐ์„ฑ์„ ์œ„ํ•ด. ํ”„๋กœ๊ทธ๋žจ์—์„œ ํ™˜์˜ ๋ฉ”์‹œ์ง€๋ฅผ ์ž์ฃผ ํ‘œ์‹œํ•˜๋„๋ก ์š”๊ตฌํ•˜๋Š” ๊ฒฝ์šฐ ๊ฐ ์ƒˆ ๋กœ๋ด‡์— ํ•„์š”ํ•œ ํ…์ŠคํŠธ๋ฅผ ์ˆ˜๋™์œผ๋กœ ์ž…๋ ฅํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋ชจ๋“  ์‚ฌ๋žŒ์ด ์ด๋ฆ„๊ณผ ๋‚˜์ด๊ฐ€ ๋‹ค๋ฅด๊ธฐ ๋•Œ๋ฌธ์— ์ด ํ…์ŠคํŠธ๋ฅผ ์ƒ์ˆ˜๋กœ ๋งŒ๋“ค ์ˆ˜๋„ ์—†์Šต๋‹ˆ๋‹ค! ๊ทธ๋Ÿฌ๋‚˜์ด ์ƒˆ๋กœ์šด ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ•˜๋ฉด ์ธ์‚ฌ๋ง์„ ์ƒ์ˆ˜๋กœ ๋ถ„๋ฆฌํ•˜๊ณ  ํ•„์š”ํ•œ ๊ฒฝ์šฐ printf() ๋ฉ”์„œ๋“œ์— ์ „๋‹ฌ๋œ ์ธ์ˆ˜๋ฅผ ๊ฐ„๋‹จํžˆ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

import java.io.IOException; 
import java.io.PrintStream; 

public class Main { 

   private static final String GREETINGS_MESSAGE = "My name is %s. I am %d!"; 

   public static void main(String[] args) throws IOException { 

       PrintStream printStream = new PrintStream("C:\\Users\\Steve\\Desktop\\test.txt"); 

       printStream.println("Hello!"); 
       printStream.println("We are robots!"); 


       printStream.printf(GREETINGS_MESSAGE, "Amigo", 18); 
       printStream.printf(GREETINGS_MESSAGE, "R2-D2", 35); 
       printStream.printf(GREETINGS_MESSAGE, "C-3PO", 35); 

       printStream.close(); 
   } 
} 

System.in ๊ต์ฒด

์ด ๋‹จ์›์—์„œ๋Š” "์‹œ์Šคํ…œ๊ณผ ์‹ธ์šฐ๊ณ " ์‹œ์Šคํ…œ ์ถœ๋ ฅ์„ ์›ํ•˜๋Š” ์œ„์น˜๋กœ ๋ฆฌ๋””๋ ‰์…˜ํ•˜๊ธฐ ์œ„ํ•ด System.in ๋ณ€์ˆ˜๋ฅผ ๋Œ€์ฒดํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ๋ฐฐ์›๋‹ˆ๋‹ค. System.in์ด ๋ฌด์—‡์ธ์ง€ ์žŠ์–ด๋ฒ„๋ฆด ์ˆ˜ ์žˆ์ง€๋งŒ CodeGym ํ•™์ƒ์€ ์ด ๊ตฌ์กฐ๋ฅผ ์ ˆ๋Œ€ ์žŠ์ง€ ๋ชปํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค.

BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
System.in ( System.out โ€ฏ๊ณผ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ )์€ System ํด๋ž˜์Šค ์˜ ์ •์  ๋ณ€์ˆ˜์ž…๋‹ˆ๋‹ค . ๊ทธ๋Ÿฌ๋‚˜ System.out ๊ณผ ๋‹ฌ๋ฆฌ ๋‹ค๋ฅธ ํด๋ž˜์Šค, ์ฆ‰ InputStream ์„ ์ฐธ์กฐํ•ฉ๋‹ˆ๋‹ค . ๊ธฐ๋ณธ์ ์œผ๋กœ System.in์€ ์‹œ์Šคํ…œ ์žฅ์น˜(ํ‚ค๋ณด๋“œ)์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ์ฝ๋Š” ์ŠคํŠธ๋ฆผ์ž…๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ System.out ๊ณผ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ํ‚ค๋ณด๋“œ๋ฅผ ๋ฐ์ดํ„ฐ ์†Œ์Šค๋กœ ๋Œ€์ฒดํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์›ํ•˜๋Š” ๊ณณ ์–ด๋””์—์„œ๋‚˜ ๋ฐ์ดํ„ฐ๋ฅผ ์ฝ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค! ์˜ˆ๋ฅผ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

import java.io.*; 

public class Main { 

   public static void main(String[] args) throws IOException { 

       String greetings = "Hi! My name is Amigo!\nI'm learning Java on the CodeGym website.\nOne day I will become a cool programmer!\n"; 
       byte[] bytes = greetings.getBytes(); 

       InputStream inputStream = new ByteArrayInputStream(bytes); 

       System.setIn(inputStream); 

       BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); 

       String str; 

       while ((str = reader.readLine())!= null) { 

           System.out.println(str); 
       } 

   } 
}
๊ทธ๋ž˜์„œ ์šฐ๋ฆฌ๋Š” ๋ฌด์—‡์„ ํ–ˆ์Šต๋‹ˆ๊นŒ? System.in์€ ์ผ๋ฐ˜์ ์œผ๋กœ ํ‚ค๋ณด๋“œ์— ๋ฐ”์ธ๋”ฉ๋ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ์šฐ๋ฆฌ๋Š” ํ‚ค๋ณด๋“œ์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ์ฝ๊ณ  ์‹ถ์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์ผ๋ฐ˜ ๋ฌธ์ž์—ด์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ์ฝ๋„๋ก ํ•ฉ์‹œ๋‹ค! ๋ฌธ์ž์—ด์„ ๋งŒ๋“ค๊ณ  ๋ฐ”์ดํŠธ ๋ฐฐ์—ด๋กœ ๊ฐ€์ ธ์™”์Šต๋‹ˆ๋‹ค. ๋ฐ”์ดํŠธ๊ฐ€ ํ•„์š”ํ•œ ์ด์œ ๋Š” ๋ฌด์—‡์ž…๋‹ˆ๊นŒ? ๋ฌธ์ œ๋Š” InputStream ์ด ์ถ”์ƒ ํด๋ž˜์Šค์ด๋ฏ€๋กœ ์ธ์Šคํ„ด์Šค๋ฅผ ์ง์ ‘ ๋งŒ๋“ค ์ˆ˜ ์—†๋‹ค๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์šฐ๋ฆฌ๋Š” ๊ทธ ์ž์† ์ค‘ ํ•˜๋‚˜๋ฅผ ์„ ํƒํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ByteArrayInputStream ์„ ์„ ํƒํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค . ๊ฐ„๋‹จํ•˜๊ณ  ์ด๋ฆ„๋งŒ์œผ๋กœ๋„ ์ž‘๋™ ๋ฐฉ์‹์„ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฐ์ดํ„ฐ ์†Œ์Šค๋Š” ๋ฐ”์ดํŠธ ๋ฐฐ์—ด์ž…๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ ์šฐ๋ฆฌ๋Š” ๋ฐ”์ดํŠธ ๋ฐฐ์—ด์„ ๋งŒ๋“ค๊ณ  ๋ฐ์ดํ„ฐ๋ฅผ ์ฝ์„ ์ŠคํŠธ๋ฆผ ์˜ ์ƒ์„ฑ์ž์—๊ฒŒ ์ „๋‹ฌํ•ฉ๋‹ˆ๋‹ค . ์ด์ œ ๋ชจ๋“  ๊ฒƒ์ด ์ค€๋น„๋˜์—ˆ์Šต๋‹ˆ๋‹ค! ์ด์ œ System.setIn()์„ ์‚ฌ์šฉํ•˜๊ธฐ๋งŒ ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.in ๋ณ€์ˆ˜ ์˜ ๊ฐ’์„ ๋ช…์‹œ์ ์œผ๋กœ ์„ค์ •ํ•˜๋Š” ๋ฉ”์„œ๋“œ์ž…๋‹ˆ๋‹ค . out ์ด ์—†์œผ๋ฉด ๋ณ€์ˆ˜์˜ ๊ฐ’์„ ์ง์ ‘ ์„ค์ •ํ•˜๋Š” ๊ฒƒ๋„ ๋ถˆ๊ฐ€๋Šฅํ•˜๋‹ค๋Š” ์ ์„ ๊ธฐ์–ตํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค. setOut() ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ–ˆ์Šต๋‹ˆ๋‹ค. InputStream์„ System.in ๋ณ€์ˆ˜ ์— ํ• ๋‹นํ•œ ํ›„ ๋ชฉ์ ์„ ๋‹ฌ์„ฑํ–ˆ๋Š”์ง€ ํ™•์ธํ•˜๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์šฐ๋ฆฌ์˜ ์˜ค๋žœ ์นœ๊ตฌ์ธ BufferedReader๊ฐ€ ์—ฌ๊ธฐ์„œ ์šฐ๋ฆฌ๋ฅผ ๋„์™€์ค๋‹ˆ๋‹ค. ์ผ๋ฐ˜์ ์œผ๋กœ ์ด ์ฝ”๋“œ๋Š” IntelliJ IDEA์—์„œ ์ฝ˜์†”์„ ์—ฐ ๋‹ค์Œ ํ‚ค๋ณด๋“œ์—์„œ ์ž…๋ ฅํ•œ ๋ฐ์ดํ„ฐ๋ฅผ ์ฝ์Šต๋‹ˆ๋‹ค.

BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); 

       String str; 

       while ((str = reader.readLine())!= null) { 

           System.out.println(str); 
       }
๊ทธ๋Ÿฌ๋‚˜ ์ด์ œ ์‹คํ–‰ํ•˜๋ฉด ๋ฌธ์ž์—ด์ด ์ฝ˜์†”์— ํ‘œ์‹œ๋˜๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ‚ค๋ณด๋“œ์—์„œ ์ฝ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. ๋ฐ์ดํ„ฐ ์†Œ์Šค๋ฅผ ๊ต์ฒดํ–ˆ์Šต๋‹ˆ๋‹ค. ๋” ์ด์ƒ ํ‚ค๋ณด๋“œ๊ฐ€ ์•„๋‹ˆ๋ผ ์šฐ๋ฆฌ์˜ ํ˜„์ž…๋‹ˆ๋‹ค! ๊ฐ„๋‹จํ•ฉ๋‹ˆ๋‹ค :) ์˜ค๋Š˜ ์ˆ˜์—…์—์„œ ์šฐ๋ฆฌ๋Š” ์ƒˆ๋กœ์šด ํด๋ž˜์Šค๋ฅผ ์•Œ๊ฒŒ ๋˜์—ˆ๊ณ  I/O ์ž‘์—…์„ ์œ„ํ•œ ์ž‘๊ณ  ์ƒˆ๋กœ์šด ํ•ต์„ ํƒ๊ตฌํ–ˆ์Šต๋‹ˆ๋‹ค. ์ด์ œ ์ฝ”์Šค๋กœ ๋Œ์•„๊ฐ€์„œ ๋ช‡ ๊ฐ€์ง€ ์ž‘์—…์„ ์™„๋ฃŒํ•  ์‹œ๊ฐ„์ž…๋‹ˆ๋‹ค :) ๋‹ค์Œ ๋ ˆ์Šจ์—์„œ ๋ต™๊ฒ ์Šต๋‹ˆ๋‹ค!
์ฝ”๋ฉ˜ํŠธ
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION