JavaRush /Java Blog /Random EN /BufferedReader and BufferedWriter
Author
John Selawsky
Senior Java-разработчик и преподаватель at LearningTree

BufferedReader and BufferedWriter

Published in the Random EN group
The Java BufferedReader class reads text from a character input stream, buffering the characters read to enable efficient reading of characters, arrays, and strings. You can specify the buffer size as the second parameter in the constructor.
BufferedReader and BufferedWriter - 1
Designers:
BufferedReader(Reader in) // Creates a character input buffer stream that uses the default buffer size.
BufferedReader(Reader in, int sz) // Creates a character input buffer stream that uses the specified size.
Methods:
close() // close the stream
mark(int readAheadLimit) // mark the position in the stream
markSupported() // does it support thread tagging
int read() // read buffer
int read(char[] cbuf, int off, int len) // read buffer
String readLine() // next line
boolean ready() // can the thread read
reset() // reset stream
skip(long n) // skip characters
Example of using the BufferedReader and BufferedWriter classes: Reading a file:
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class FileReaderExample {

	public static void main(String[] args) {
		String inputFileName = "file.txt";

		try (BufferedReader reader = new BufferedReader(new FileReader(inputFileName))) {
			String line;
			while ((line = reader.readLine()) != null) {
				System.out.println(line + "\n");
			}
		}
                catch (IOException e) {
			e.printStackTrace();
		}
	}

}
The Java BufferedWriter class writes text to the character output stream, buffering the written characters to enable efficient writing of characters, arrays, and strings. You can specify the buffer size as the second parameter in the constructor. Designers:
BufferedWriter(Writer out) // Creates a character output buffer stream that uses the default buffer size.
BufferedWriter(Writer out, int sz) // Creates a character output buffer stream that uses the specified size.
Methods:
close() // close the stream
flush() // pass data from buffer to Writer
newLine() // wrap to new line
write(char[] cbuf, int off, int len) // write to buffer
write(int c) // write to buffer
write(String s, int off, int len) // write to buffer
Example of using the Java BufferedReader and BufferedWriter classes: Writing to a file
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;

public class FileWritterExample {

	public static void main(String[] args) {
		String outputFileName = "file.txt";
		String[] array = { "one", "two", "three", "four" };

		try (BufferedWriter writter = new BufferedWriter(new FileWriter(outputFileName))) {
			for (String value : array) {
				writter.write(value + "\n");
			}
		}
        catch (IOException e) {
			e.printStackTrace();
		}
	}

}
FileWriterimmediately writes data to disk and accesses it every time, the buffer works as a wrapper and speeds up the application. The buffer will write data to itself, and then a large piece of files to disk. We read data from the console and write it to a file:
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;

public class ConsoleReaderExample {

	public static void main(String[] args) {
		String outputFileName = "file.txt";

		try (BufferedReader reader = new BufferedReader(new InputStreamReader(System.in))) {
			try (BufferedWriter writter = new BufferedWriter(new FileWriter(outputFileName))) {
				String line;
				while (!(line = reader.readLine()).equals("exit")) { // Break the loop when writing the line exit
					writter.write(line);
				}
			}
		}
         catch (IOException e) {
			e.printStackTrace();
		}
	}

}
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION