ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด/JAVA

[JAVA] BufferedReader / BufferedWriter ํด๋ž˜์Šค

NaNaRin๐Ÿ™ƒ 2021. 1. 7. 20:39

docs.oracle.com/en/java/javase/15/docs/api/java.base/java/io/BufferedReader.html

 

BufferedReader (Java SE 15 & JDK 15)

All Implemented Interfaces: Closeable, AutoCloseable, Readable Direct Known Subclasses: LineNumberReader public class BufferedReader extends Reader Reads text from a character-input stream, buffering characters so as to provide for the efficient reading of

docs.oracle.com

docs.oracle.com/en/java/javase/15/docs/api/java.base/java/io/BufferedWriter.html

 

BufferedWriter (Java SE 15 & JDK 15)

All Implemented Interfaces: Closeable, Flushable, Appendable, AutoCloseable public class BufferedWriter extends Writer Writes text to a character-output stream, buffering characters so as to provide for the efficient writing of single characters, arrays, a

docs.oracle.com

 

java.io ์— ์†ํ•ด์žˆ๋Š” ํด๋ž˜์Šค. ์ž…๋ ฅ๋œ / ์ถœ๋ ฅํ•  ๋ฐ์ดํ„ฐ๊ฐ€ ๋ฒ„ํผ๋ฅผ ๊ฑฐ์ณ์„œ ์ „๋‹ฌ๋œ๋‹ค.

 

์ผ๋ฐ˜์ ์ธ ์ž…์ถœ๋ ฅ๊ณผ๋Š” ๋‹ฌ๋ฆฌ ๋ฒ„ํผ๋ฅผ ์ด์šฉํ•˜์—ฌ ๋ฐ์ดํ„ฐ ์ž…์ถœ๋ ฅ์˜ ํšจ์œจ์„ฑ์„ ๋†’์—ฌ์ค€๋‹ค

์‚ฌ๊ณผ ํ•˜๋‚˜๋ฅผ 100๋ฒˆ ์™”๋‹ค๊ฐ”๋‹ค ์˜ฎ๊ธฐ๋Š” ๊ฒƒ๋ณด๋‹ค ๋ฐ”๊ตฌ๋‹ˆ์— ํ•œ๋ฒˆ์— 100๊ฐœ๋ฅผ ๋‹ด์•„ ์˜ฎ๊ธฐ๋Š”๊ฒŒ ๋” ํšจ์œจ์ ์ธ ๊ฒƒ๊ณผ ๊ฐ™์€ ์˜๋ฏธ!

 


BufferedReader :

- space์™€ Enter ๋‘˜ ๋ชจ๋‘๋ฅผ ๊ฒฝ๊ณ„๋กœ ์ธ์‹ํ•˜๋Š” Scanner ํด๋ž˜์Šค์™€ ๋‹ฌ๋ฆฌ Enter๋งŒ ๊ฒฝ๊ณ„๋กœ ์ธ์‹

- ์ž…๋ ฅ๋ฐ›์€ ๋ฐ์ดํ„ฐ๊ฐ€ ๋ชจ๋‘ String์œผ๋กœ ๊ณ ์ • -> ํ˜•๋ณ€ํ™˜ ํ•„์š”

- ์˜ˆ์™ธ์ฒ˜๋ฆฌ๋ฅผ ํ•ด ์ค˜์•ผ ํ•จ ( throws IOException )

  • int read() : ํ•œ ๋ฌธ์ž๋ฅผ ์ฝ์–ด intํ˜•์œผ๋กœ ๋ฆฌํ„ด

  • String readLine() : ํ•œ ์ค„์„ ์ฝ์–ด Stringํ˜•์œผ๋กœ ๋ฆฌํ„ด

  • ์ž…๋ ฅ๋ฐ›์€ ๋ฌธ์ž์—ด์„ ์ฒ˜๋ฆฌํ•  ๋•Œ๋Š” StringTokernizer ํด๋ž˜์Šค์˜ hasMoreTokens(), nextToken() ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•˜์—ฌ ๊ณต๋ฐฑ ๋‹จ์œ„๋กœ ๋ถ„๋ฆฌํ•˜๋ฉด ํŽธ๋ฆฌํ•˜๋‹ค.
    • boolean hasMoreTokens() : ๋‹ค์Œ ํ† ํฐ์ด ์กด์žฌํ•˜๋Š”์ง€ ํ™•์ธํ•˜์—ฌ ์กด์žฌํ•  ๋•Œ true, ์กด์žฌํ•˜์ง€ ์•Š์„ ๋•Œ false ๋ฐ˜ํ™˜
    • String nextToken() : ๋‹ค์Œ ํ† ํฐ์„ String ํ˜•์œผ๋กœ ๋ฐ˜ํ™˜

 

BufferedReader.read()

BufferedReader.readLine()

StringTokernizer.hasMoreTokens()

StringTokernizer.nextToken()

 

 

BufferedWriter :

- System.out.println() ๋ณด๋‹ค ๋” ํšจ์œจ์ ์œผ๋กœ ์ถœ๋ ฅ์ด ๊ฐ€๋Šฅ

  • void write(int i / char[] c / String s) : intํ˜•/char[]ํ˜•/Stringํ˜• ๋ฐ์ดํ„ฐ๋ฅผ ๋ฒ„ํผ์— ์ถœ๋ ฅ. ์ž๋™๊ฐœํ–‰ ๊ธฐ๋Šฅ์ด ์—†์Œ.

  • String newLine() : ์ค„๋ฐ”๊ฟˆ. ๊ฐœํ–‰
  • void flush() : ๋ฒ„ํผ์— ๋‚จ์•„์žˆ๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ๋ชจ๋‘ ์ถœ๋ ฅ

-> flush() ํ•„์ˆ˜!

 

BufferedWriter.write()

BufferedWriter.newLine()

BufferedWriter.flush()

 


์˜ˆ์ œ 

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.StringTokenizer;

public class BufferedEx {

	public static void main(String[] args) throws IOException {
		
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));

		String s = br.readLine();

		bw.write(s);
		bw.newLine();
		bw.write("====================\n");
		bw.flush();
		
		StringTokenizer st = new StringTokenizer(s);
		
		while(st.hasMoreTokens()) {
			bw.write(st.nextToken());
			bw.newLine();
		}
				
		bw.flush();
	}
}

 

์ถœ๋ ฅ

Hello Java BufferedReader BufferedWriter
Hello Java BufferedReader BufferedWriter
====================
Hello
Java
BufferedReader
BufferedWriter

 

 

์‚ฌ์šฉ์˜ˆ : nanarin.tistory.com/19