Example code of BufferedReader and BufferedWriter class in Java. In this example code you will learn how to use these classes to perform basic read and write operations.
Java BufferedReader class is used to read the data in buffer.
The BufferedWriter class is used for buffered writing writing of data.
java.io.BufferedReader and java.io.BufferedWriter are used to read/write Strings from/to text files.
BufferedReader br; BufferedWriter bw; File f; char ca; String s; int c; // This is an int to hold either a char or -1. int offset, len, size;
|BufferedReader Constructor -- see examples|
|br =||new BufferedReader(new FileReader(f));|
|s =||br.readLine()||Returns next input line without newline char(s) or null if no more input. May throw IOException.|
|c =||br.read()||Reads and returns one char.
Returns -1 on end-of-file.
Note that that this returns an |
|size =||br.read(ca, offset, len)||Read up to len chars into ca starting at index offset. Returns -1 on end-of-file. May throw IOException.|
|br.close()||Close when finished reading to unlock file.|
|BufferedWriter Constructor -- see examples|
|bw =||new BufferedWriter(new FileWriter(f));|
|bw.write(s, offset, len)||Writes len chars from s starting at index offset. May throw IOException.|
|bw.write(ca, offset, len)||Writes len chars from ca starting at index offset. May throw IOException.|
|bw.write(s)||Writes s to file. Doesn't add newline char. May throw IOException.|
|bw.write(c)||Writes single character c to file. May throw IOException.|
|bw.newLine()||Writes operating-system dependent newline (CR, LF, or CRLF).|
See File I/O - Text Files for an example of reading and writing text files.