Jenkov.com
Tutorials Books About
Java Io

1 Java IO Tutorial
2 Java IO Overview
3 Java IO: Files
4 Java IO: Pipes
5 Java IO: Networking
6 Java IO: Byte & Char Arrays
7 Java IO: System.in, System.out, and System.error
8 Java IO: Streams
9 Java IO: Input Parsing
10 Java IO: Readers and Writers
11 Java IO: Concurrent IO
12 Java IO: Exception Handling
13 Java IO: InputStream
14 Java IO: OutputStream
15 Java IO: FileInputStream
16 Java IO: FileOutputStream
17 Java IO: RandomAccessFile
18 Java IO: File
19 Java IO: PipedInputStream
20 Java IO: PipedOutputStream
21 Java IO: ByteArrayInputStream
22 Java IO: ByteArrayOutputStream
23 Java IO: FilterInputStream
24 Java IO: FilterOutputStream
25 Java IO: BufferedInputStream
26 Java IO: BufferedOutputStream
27 Java IO: PushbackInputStream
28 Java IO: SequenceInputStream
29 Java IO: DataInputStream
30 Java IO: DataOutputStream
31 Java IO: PrintStream
32 Java IO: ObjectInputStream
33 Java IO: ObjectOutputStream
34 Java IO: Serializable
35 Java IO: Reader
36 Java IO: Writer
37 Java IO: InputStreamReader
38 Java IO: OutputStreamWriter
39 Java IO: FileReader
40 Java IO: FileWriter
41 Java IO: PipedReader
42 Java IO: PipedWriter
43 Java IO: CharArrayReader
44 Java IO: CharArrayWriter
45 Java IO: BufferedReader
46 Java IO: BufferedWriter
47 Java IO: FilterReader
48 Java IO: FilterWriter
49 Java IO: PushbackReader
50 Java IO: LineNumberReader
51 Java IO: StreamTokenizer
52 Java IO: PrintWriter
53 Java IO: StringReader
54 Java IO: StringWriter




Java IO: BufferedReader


The BufferedReader class provides buffering to your Reader's. Buffering can speed up IO quite a bit. Rather than read one character at a time from the network or disk, you read a larger block at a time. This is typically much faster, especially for disk access and larger data amounts.

The main difference between BufferedReader and BufferedInputStream is that Reader's work on characters (text), wheres InputStream's works on raw bytes.

To add buffering to your Reader's simply wrap them in a BufferedReader. Here is how that looks:

Reader input = new BufferedReader(
                      new FileReader("c:\\data\\input-file.txt"));

Simple, isn't it? You can set the buffer size to use internally by the BufferedReader. You provide the size as a constructor parameter, like this:

    Reader input = new BufferedReader(
                          new FileReader("c:\\data\\input-file.txt"),
                      8 * 1024
    );

This example sets the internal buffer to 8 KB. It is best to use buffer sizes that are multiples of 1024 bytes. That works best with most built-in buffering in hard disks etc.

Except for adding buffering to your Reader's, BufferedReader behaves pretty much like a Reader. It has one extra method though, the readLine() method. This method can be handy if you need to read input one line at a time.



Connect with me: Newsletter - Get all my free tips!