---
title: "Java I/O: Character Streams and Buffered Reader/Writers"  
description: "Just the way binary streams operate on binary files, the character streams operate on character files—that is, text files such as .txt, .odt, and .doc"  
author: "David Miller"  
published: 2016-05-24  
updated: 2018-03-15  
canonical: https://www.mindstick.com/blog/11121/java-i-o-character-streams-and-buffered-reader-writers  
category: "java"  
tags: ["java"]  
reading_time: 3 minutes  

---

# Java I/O: Character Streams and Buffered Reader/Writers

##### Character Streams

Just the way **[binary](https://www.mindstick.com/forum/34709/please-write-a-program-for-decimal-to-binary-conversion-in-c-sharp) streams** operate on binary [files](https://www.mindstick.com/articles/23302/the-importance-and-advantage-of-keeping-your-important-files-on-the-cloud), the **[character](https://www.mindstick.com/articles/23551/an-investigate-distinctive-seafood-restaurant-for-your-image-stamp-character) streams** operate on character files—that is, [text](https://www.mindstick.com/blog/301635/did-people-reinvent-texting-to-express-the-full-range-of-emotions) files such as .txt, .odt, and .docx. Just the way we have several real (fully implemented) subclasses of [input and output](https://www.mindstick.com/forum/161309/what-are-the-features-of-the-console-class-for-input-and-output-in-java) byte [stream](https://yourviews.mindstick.com/view/88509/us-intercepts-iranian-tanker-m-t-stream-amid-hormuz-tensions) types, we have several real classes of input and output character stream types. The high-level [class hierarchy](https://www.mindstick.com/interview/2459/what-is-the-difference-between-the-reader-writer-class-hierarchy-and-the-inputstream-outputstream-class-hierarchy) for character stream classes is given here

\

![Java I/O: Character Streams and Buffered Reader/Writers](https://www.mindstick.com/blogs/11787ea8-e0e5-4321-a2f1-9280d53af0ec/images/d6b31c02-dea4-494b-91f1-9d69c0d72ed0.png)\

The **Reader** and **Writer** classes act as super classes for the rest of the classes in this category. Several Reader/Writer subclasses are available that match the corresponding byte stream classes in functionality, except they work on character streams rather than byte streams.

##### Buffered Readers/Writers

Java provides the **BufferedReader** and **BufferedWriter** classes, which provide the in-built buffers. Remember that reading a single byte or 512 bytes at a time requires the same amount of I/O processing. Therefore, buffering characters makes file reading and writing more efficient.

When we use these classes, we may specify the size for the buffer; otherwise, a default size is used that is adequate for most purposes. If we have a lot of system memory, we may allocate a larger buffer to provide data caching so as to avoid frequent reads and writes from and to a physical disk.

##### The BufferedReader Class

Typically, we would construct an instance of the BufferedReader class as follows:

BufferedReader reader = new BufferedReader(new FileReader("filename"));

- We first construct an instance of the FileReader class by specifying the name of the file to be opened in its parameterized constructor.
- We use this instance of the FileReader class as a parameter to BufferedReader during its instantiation.
- Once the reader object is constructed, we may call its various overloaded read methods to read the file contents. A typical method for reading the file line by line is the readLine method:

##### public String readLine() throws IOException

- The readLine method reads one line at a time from the input stream and returns it to the caller as a String object.
- A line is terminated by either a linefeed ('\n'), a carriage return ('\r'), or a carriage return followed immediately by a linefeed.
- The returned string does not include the line-termination character. It returns null on reaching the end of the stream.

##### The BufferedWriter Class

Similar to the **BufferedReader** class, Java provides a **BufferedWriter** class for efficient writing to the files. We construct an instance of BufferedWriter as follows:

BufferedWriter writer = new BufferedWriter(new FileWriter("mindstick.out"));

- This creates a file called mindstick.out if it does not already exist; if the file exists, its contents will be overwritten.
- After the file is opened for writing, the BufferedWriter creates a wrapper on it to provide efficient write operations that can accept the data to be written in a character array format or a string.

- It also provides a newline method that writes a newline character to the output stream.

---

Original Source: https://www.mindstick.com/blog/11121/java-i-o-character-streams-and-buffered-reader-writers

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
