CodeGym /Java Blog /๋ฌด์ž‘์œ„์˜ /BufferedReader ๋ฐ BufferedWriter
John Squirrels
๋ ˆ๋ฒจ 41
San Francisco

BufferedReader ๋ฐ BufferedWriter

๋ฌด์ž‘์œ„์˜ ๊ทธ๋ฃน์— ๊ฒŒ์‹œ๋˜์—ˆ์Šต๋‹ˆ๋‹ค
BufferedReader ๋ฐ BufferedWriter - 1Java์˜ BufferedReaderํด๋ž˜์Šค๋Š” ๊ธฐํ˜ธ ์ŠคํŠธ๋ฆผ์—์„œ ํ…์ŠคํŠธ๋ฅผ ์ฝ๊ณ  ๊ธฐํ˜ธ๋ฅผ ๋ฒ„ํผ๋งํ•˜์—ฌ ๋ฌธ์ž, ๋ฐฐ์—ด ๋ฐ ๋ฌธ์ž์—ด์„ ํšจ์œจ์ ์œผ๋กœ ์ฝ์Šต๋‹ˆ๋‹ค. ๋ฒ„ํผ ํฌ๊ธฐ๋ฅผ ์ƒ์„ฑ์ž์— ๋‘ ๋ฒˆ์งธ ์ธ์ˆ˜๋กœ ์ „๋‹ฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. BufferedReader ๋ฐ BufferedWriter - 2์ƒ์„ฑ์ž:

BufferedReader(Reader in) // Creates a buffered stream for reading symbols. It uses the default buffer size.
BufferedReader(Reader in, int sz) // Creates a buffered stream for reading symbols. It uses the specified buffer size.
ํ–‰๋™ ์–‘์‹:

close() // Close the stream
mark(int readAheadLimit) // Mark the position in the stream
markSupported() // Indicates whether stream marking is supported
int	read() // Read the buffer
int	read(char[] cbuf, int off, int len) // Read the buffer
String	readLine() // Next line
boolean	ready() // Is the stream ready to read?
reset() // Reset the stream
skip(long n) // Skip characters

BufferedReader๋‹ค์Œ์€ ๋ฐ ํด๋ž˜์Šค๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ์˜ˆ์ž…๋‹ˆ๋‹ค BufferedWriter.

ํŒŒ์ผ ์ฝ๊ธฐ:

import java.io.*;

public class ReadFile {
	public static void main(String[] args) {
		try {
			File file = new File("file.txt"); 
			FileReader fileReader = new FileReader(file); // A stream that connects to the text file
			BufferedReader bufferedReader = new BufferedReader(fileReader); // Connect the FileReader to the BufferedReader
			
			String line;
			while((line = bufferedReader.readLine()) != null) {
				System.out.println(line); // Display the file's contents on the screen, one line at a time
			}
			
			bufferedReader.close(); // Close the stream
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
Java์˜ BufferedWriterํด๋ž˜์Šค๋Š” ์ถœ๋ ฅ ๋ฌธ์ž ์ŠคํŠธ๋ฆผ์— ํ…์ŠคํŠธ๋ฅผ ์“ฐ๊ณ  ๋ฌธ์ž, ๋ฐฐ์—ด ๋ฐ ๋ฌธ์ž์—ด์„ ํšจ์œจ์ ์œผ๋กœ ์“ฐ๊ธฐ ์œ„ํ•ด ๋ฌธ์ž๋ฅผ ๋ฒ„ํผ๋งํ•ฉ๋‹ˆ๋‹ค. ๋ฒ„ํผ ํฌ๊ธฐ๋ฅผ ์ƒ์„ฑ์ž์— ๋‘ ๋ฒˆ์งธ ์ธ์ˆ˜๋กœ ์ „๋‹ฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ƒ์„ฑ์ž:

BufferedWriter(Writer out) // Create a buffered output character stream that uses the default buffer size.
BufferedWriter(Writer out, int sz) // Creates a buffered character output stream that uses a buffer with the specified size.
ํ–‰๋™ ์–‘์‹:

close() // Close the stream
flush() // Send the data from the buffer to the Writer
newLine() // Move to a new line
write(char[] cbuf, int off, int len) // Write to the buffer
write(int c) // Write to the buffer
write(String s, int off, int len) // Write to the buffer

BufferedReader๋‹ค์Œ์€ Java ์™€ ํด๋ž˜์Šค๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ์˜ˆ์ž…๋‹ˆ๋‹ค BufferedWriter.

ํŒŒ์ผ์— ์“ฐ๊ธฐ:

import java.io.*;

public class WriteFile {
    public static void main(String[] args) {
        String[] list = {"one", "two", "three", "fo"};
        try {
            File file = new File("file.txt");
            FileWriter fileReader = new FileWriter(file); // A stream that connects to the text file
            BufferedWriter bufferedWriter = new BufferedWriter(fileReader); // Connect the FileWriter to the BufferedWriter

            for (String s : list) {
                bufferedWriter.write(s + "\n");
            }

            bufferedWriter.close (); // Close the stream
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
FileWriter์ฆ‰์‹œ ๋ฐ์ดํ„ฐ๋ฅผ ๋””์Šคํฌ์— ๊ธฐ๋กํ•ฉ๋‹ˆ๋‹ค. ์•ก์„ธ์Šคํ•  ๋•Œ๋งˆ๋‹ค ๋ฒ„ํผ๋ฅผ ๊ฐ์‹ธ์„œ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์†๋„๋ฅผ ๋†’์ž…๋‹ˆ๋‹ค. ๋ฒ„ํผ๋Š” ๋‚ด๋ถ€์ ์œผ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์“ด ๋‹ค์Œ ๋‚˜์ค‘์— ๋””์Šคํฌ์— ๋งŽ์€ ์–‘์˜ ํŒŒ์ผ์„ ์”๋‹ˆ๋‹ค. ์ฝ˜์†”์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ์ฝ๊ณ  ํŒŒ์ผ์— ์”๋‹ˆ๋‹ค.

import java.io.*;

class ConsoleRead {
    public static void main(String[] args) {
        try {
            File file = new File("file.txt");
            InputStreamReader inputStreamReader = new InputStreamReader(System.in); // A stream for reading from the console
            BufferedReader bufferedReader = new BufferedReader(inputStreamReader); // Connect InputStreamReader to a BufferedReader

            FileWriter fileReader = new FileWriter(file); // A stream that connects to the text file
            BufferedWriter bufferedWriter = new BufferedWriter(fileReader); // Connect the FileWriter to the BufferedWriter

            String line;
            while(!(line = bufferedReader.readLine()).equals("exit")) {
                bufferedWriter.write(line);
            }

            bufferedReader.close(); // Close the stream
            bufferedWriter.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
์ฝ”๋ฉ˜ํŠธ
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION