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.
BufferedReader and BufferedWriter methods
java.io.BufferedReader and java.io.BufferedWriter are used to read/write Strings from/to text files.
Assume
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)); | |
| BufferedReader Methods | ||
| 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 int, which is necessary so that the
-1 value indicating EOF can be stored. The int value can be cast to a char.
May throw IOException. |
| size = | br.read(ca, offset, len) | Read up to len chars into ca starting at index offset. Returns -1 on end-of-file |