Contents

BufferedWriter vs System.out.println() ์„ฑ๋Šฅ ๋น„๊ต

   Mar 12, 2025     0 min read

BufferedWriter vs System.out.println() ์„ฑ๋Šฅ ๋น„๊ต์— ๋Œ€ํ•˜์—ฌ ์•Œ์•„๋ณธ ๊ธ€์ž…๋‹ˆ๋‹ค.

์ž๋ฐ”์—์„œ ์ถœ๋ ฅ์„ ๋‹ด๋‹นํ•˜๋Š” ๋Œ€ํ‘œ์ ์ธ ํด๋ž˜์Šค์ธ BufferedWriter์™€ System.out.println()์€ ๋‚ด๋ถ€์ ์œผ๋กœ ์„œ๋กœ ๋‹ค๋ฅธ ๋ฐฉ์‹์œผ๋กœ ๋™์ž‘ํ•ฉ๋‹ˆ๋‹ค.

BufferedWriter

BufferedWriter๋Š” ์ถœ๋ ฅ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฒ„ํผ์— ์ €์žฅํ•œ ํ›„, ์ผ์ • ํฌ๊ธฐ๊ฐ€ ๋˜๊ฑฐ๋‚˜ flush()๊ฐ€ ํ˜ธ์ถœ๋  ๋•Œ ํ•œ ๋ฒˆ์— ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค.

์ด ๋ฐฉ์‹์€ ์—ฌ๋Ÿฌ ๋ฒˆ์˜ ์ž‘์€ ์ถœ๋ ฅ ์ž‘์—…์„ ํ•œ ๋ฒˆ์˜ ํฐ ์ž‘์—…์œผ๋กœ ํ•ฉ์ณ์„œ ์„ฑ๋Šฅ์„ ์ตœ์ ํ™”ํ•˜๋Š” ์—ญํ• ์„ ํ•ฉ๋‹ˆ๋‹ค.

์žฅ์ 

  • ๋งŽ์€ ์–‘์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์ถœ๋ ฅํ•  ๋•Œ ์„ฑ๋Šฅ์ด ๋›ฐ์–ด๋‚จ
  • ๋ถˆํ•„์š”ํ•œ I/O ํ˜ธ์ถœ์„ ์ค„์—ฌ CPU ์ž์›์„ ์ ˆ์•ฝ

๋‹จ์ 

  • flush()๋ฅผ ํ˜ธ์ถœํ•ด์•ผ ์ถœ๋ ฅ์ด ๋จ
  • ์ ์€ ์–‘์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์ถœ๋ ฅํ•  ๊ฒฝ์šฐ, ์˜คํžˆ๋ ค ์˜ค๋ฒ„ํ—ค๋“œ๊ฐ€ ๋ฐœ์ƒํ•  ์ˆ˜๋„ ์žˆ์Œ

System.out.println()

System.out.println()์€ ๋‚ด๋ถ€์ ์œผ๋กœ PrintStream์„ ์‚ฌ์šฉํ•˜์—ฌ ๋ฐ์ดํ„ฐ๋ฅผ ์ฆ‰์‹œ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค.

ํ•˜์ง€๋งŒ ์ด PrintStream๋„ ๋‚ด๋ถ€์ ์œผ๋กœ ๋ฒ„ํผ๋ง์„ ์ ์šฉํ•˜๊ณ  ์žˆ๊ธฐ ๋•Œ๋ฌธ์—, ๊ฐ„๋‹จํ•œ ์ถœ๋ ฅ์—์„œ๋Š” BufferedWriter๋ณด๋‹ค ๋น ๋ฅด๊ฒŒ ๋™์ž‘ํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค.

์žฅ์ 

  • ์ฆ‰๊ฐ์ ์ธ ์ถœ๋ ฅ ๊ฐ€๋Šฅ
  • ์ ์€ ์–‘์˜ ๋ฐ์ดํ„ฐ ์ถœ๋ ฅ ์‹œ ๋น ๋ฆ„

๋‹จ์ 

  • ๋Œ€๋Ÿ‰์˜ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ˜๋ณตํ•ด์„œ ์ถœ๋ ฅํ•˜๋ฉด ์„ฑ๋Šฅ ์ €ํ•˜ ๋ฐœ์ƒ ๊ฐ€๋Šฅ
  • BufferedWriter์ฒ˜๋Ÿผ ์„ฑ๋Šฅ ์ตœ์ ํ™”๊ฐ€ ์ž๋™์œผ๋กœ ์ด๋ฃจ์–ด์ง€์ง€ ์•Š์Œ

๊ฒฐ๋ก 

์ถœ๋ ฅ ๋ฐ์ดํ„ฐ ์–‘์ด ์ ์„ ๊ฒฝ์šฐ BufferedWriter๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด System.out.println()๋ณด๋‹ค ์„ฑ๋Šฅ์ด ๋–จ์–ด์งˆ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๋ฒ„ํผ๋ง์˜ ์žฅ์ ์„ ๊ฑฐ์˜ ํ™œ์šฉํ•˜์ง€ ๋ชปํ•œ์ฑ„๋กœ flush()๋ฅผ ํ˜ธ์ถœํ•˜๋Š” ๋ถˆํ•„์š”ํ•œ ์˜ค๋ฒ„ํ—ค๋“œ๊ฐ€ ๋ฐœ์ƒํ•˜๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค.