🌐
Oracle
docs.oracle.com › en › java › javase › 11 › docs › api › java.base › java › io › Reader.html
Reader (Java SE 11 & JDK 11 )
January 20, 2026 - java.io.Reader · All Implemented Interfaces: Closeable, AutoCloseable, Readable · Direct Known Subclasses: BufferedReader, CharArrayReader, FilterReader, InputStreamReader, PipedReader, StringReader, URLReader · public abstract class Reader extends Object implements Readable, Closeable ·
🌐
Oracle
docs.oracle.com › en › java › javase › 11 › docs › api › java.base › java › io › BufferedReader.html
BufferedReader (Java SE 11 & JDK 11 )
January 20, 2026 - In general, each read request made of a Reader causes a corresponding read request to be made of the underlying character or byte stream. It is therefore advisable to wrap a BufferedReader around any Reader whose read() operations may be costly, such as FileReaders and InputStreamReaders.
🌐
Oracle
docs.oracle.com › en › java › javase › 11 › docs › api › java.base › java › io › FileReader.html
FileReader (Java SE 11 & JDK 11 )
January 20, 2026 - java.io.Reader · java.io.InputStreamReader · java.io.FileReader · All Implemented Interfaces: Closeable, AutoCloseable, Readable · public class FileReader extends InputStreamReader · Reads text from character files using a default buffer size. Decoding from bytes to characters uses either ...
🌐
Java
download.java.net › java › early_access › panama › docs › api › java.base › java › io › FileReader.html
FileReader (Java SE 19 & JDK 19 [build 1])
11 · public FileReader · (File file, Charset charset) throws IOException · Creates a new FileReader, given the File to read and the charset. Parameters: file - the File to read · charset - the charset · Throws: IOException - if the file does not exist, is a directory rather than a regular ...
🌐
Oracle
docs.oracle.com › en › java › javase › 11 › docs › api › java.base › java › io › InputStreamReader.html
InputStreamReader (Java SE 11 & JDK 11 )
January 20, 2026 - java.io.Reader · java.io.InputStreamReader · All Implemented Interfaces: Closeable, AutoCloseable, Readable · Direct Known Subclasses: FileReader · public class InputStreamReader extends Reader · An InputStreamReader is a bridge from byte streams to character streams: It reads bytes and ...
🌐
Baeldung
baeldung.com › home › java › java io › guide to bufferedreader
Guide to BufferedReader | Baeldung
January 8, 2024 - Simply put, it enables us to minimize the number of I/O operations by reading chunks of characters and storing them in an internal buffer. While the buffer has data, the reader will read from it instead of directly from the underlying stream.
🌐
Oracle
docs.oracle.com › en › java › javase › 11 › docs › api › java.base › java › io › StringReader.html
StringReader (Java SE 11 & JDK 11 )
January 20, 2026 - Closes the stream and releases any system resources associated with it. Once the stream has been closed, further read(), ready(), mark(), or reset() invocations will throw an IOException. Closing a previously closed stream has no effect. This method will block while there is another thread ...
🌐
GitHub
github.com › AdoptOpenJDK › openjdk-jdk11 › blob › master › src › java.base › share › classes › java › io › BufferedReader.java
openjdk-jdk11/src/java.base/share/classes/java/io/BufferedReader.java at master · AdoptOpenJDK/openjdk-jdk11
import java.util.stream.StreamSupport; · /** * Reads text from a character-input stream, buffering characters so as to · * provide for the efficient reading of characters, arrays, and lines.
Author   AdoptOpenJDK
🌐
Oracle
docs.oracle.com › en › java › javase › 11 › docs › api › java.base › java › lang › Readable.html
Readable (Java SE 11 & JDK 11 )
January 20, 2026 - Package java.lang · All Known Implementing Classes: BufferedReader, CharArrayReader, CharBuffer, FileReader, FilterReader, InputStreamReader, LineNumberReader, PipedReader, PushbackReader, Reader, StringReader, URLReader · public interface Readable · A Readable is a source of characters.
🌐
Oracle
docs.oracle.com › en › java › javase › 21 › docs › api › java.base › java › io › Reader.html
Reader (Java SE 21 & JDK 21)
January 20, 2026 - a Reader which reads no characters · Since: 11 · public int read · (CharBuffer target) throws IOException · Attempts to read characters into the specified character buffer. The buffer is used as a repository of characters as-is: the only changes made are the results of a put operation.
Find elsewhere
Top answer
1 of 16
765

My favorite way to read a small file is to use a BufferedReader and a StringBuilder. It is very simple and to the point (though not particularly effective, but good enough for most cases):

BufferedReader br = new BufferedReader(new FileReader("file.txt"));
try {
    StringBuilder sb = new StringBuilder();
    String line = br.readLine();

    while (line != null) {
        sb.append(line);
        sb.append(System.lineSeparator());
        line = br.readLine();
    }
    String everything = sb.toString();
} finally {
    br.close();
}

Some has pointed out that after Java 7 you should use try-with-resources (i.e. auto close) features:

try(BufferedReader br = new BufferedReader(new FileReader("file.txt"))) {
    StringBuilder sb = new StringBuilder();
    String line = br.readLine();

    while (line != null) {
        sb.append(line);
        sb.append(System.lineSeparator());
        line = br.readLine();
    }
    String everything = sb.toString();
}

When I read strings like this, I usually want to do some string handling per line anyways, so then I go for this implementation.

Though if I want to actually just read a file into a String, I always use Apache Commons IO with the class IOUtils.toString() method. You can have a look at the source here:

http://www.docjar.com/html/api/org/apache/commons/io/IOUtils.java.html

FileInputStream inputStream = new FileInputStream("foo.txt");
try {
    String everything = IOUtils.toString(inputStream);
} finally {
    inputStream.close();
}

And even simpler with Java 7:

try(FileInputStream inputStream = new FileInputStream("foo.txt")) {     
    String everything = IOUtils.toString(inputStream);
    // do something with everything string
}
2 of 16
648

ASCII is a TEXT file so you would use Readers for reading. Java also supports reading from a binary file using InputStreams. If the files being read are huge then you would want to use a BufferedReader on top of a FileReader to improve read performance.

Go through this article on how to use a Reader

I'd also recommend you download and read this wonderful (yet free) book called Thinking In Java

In Java 7:

new String(Files.readAllBytes(...))

(docs) or

Files.readAllLines(...)

(docs)

In Java 8:

Files.lines(..).forEach(...)

(docs)

🌐
Runoob
runoob.com › manual › jdk1.6 › java.base › java › io › Reader.html
Reader (Java SE 11 & JDK 11 )
Reader ,不读取任何字符 · 从以下版本开始: · 11 · public int read​(CharBuffer target) throws IOException · 尝试将字符读入指定的字符缓冲区。 缓冲区按原样用作字符存储库:唯一的更改是put操作的结果。 不执行缓冲器的翻转或倒带。 ·
🌐
Oracle
docs.oracle.com › en › java › javase › 17 › docs › api › java.base › java › io › Reader.html
Reader (Java SE 17 & JDK 17)
April 21, 2026 - a Reader which reads no characters · Since: 11 · public int read · (CharBuffer target) throws IOException · Attempts to read characters into the specified character buffer. The buffer is used as a repository of characters as-is: the only changes made are the results of a put operation.
🌐
Medium
medium.com › @AlexanderObregon › javas-files-readstring-method-explained-219f66db08ca
Java's Files.readString() Method Explained
February 7, 2025 - Reading files is a common task in any programming language, and Java is no exception. Over time, Java has introduced several ways to read files, each with its advantages and drawbacks. With Java 11, a new method was added to the java.nio.file.Files class—readString().
🌐
Oracle
docs.oracle.com › en › java › javase › 12 › docs › api › java.base › java › io › Reader.html
Reader (Java SE 12 & JDK 12 )
a Reader which reads no characters · Since: 11 · public int read​(CharBuffer target) throws IOException · Attempts to read characters into the specified character buffer. The buffer is used as a repository of characters as-is: the only changes made are the results of a put operation.
🌐
HappyCoders.eu
happycoders.eu › java › how-to-read-files-easily-and-fast
How to Read Files Easily and Fast (Java Files Tutorial)
November 29, 2024 - If you want to load the contents of a text file into a String, use – since Java 11 – the Files.readString() method as follows:
🌐
GitHub
github.com › maxmind › MaxMind-DB-Reader-java
GitHub - maxmind/MaxMind-DB-Reader-java: Java reader for the MaxMind DB format · GitHub
In such applications, we suggest creating one Reader object and sharing that among threads. By default, this API uses the MEMORY_MAP mode, which memory maps the file. On Windows, this may create an exclusive lock on the file that prevents it from being renamed or deleted. Due to the implementation of memory mapping in Java, this lock will not be released when the DatabaseReader is closed; it will only be released when the object and the MappedByteBuffer it uses are garbage collected.
Starred by 129 users
Forked by 50 users
Languages   Java 98.2% | Shell 1.8%
🌐
OpenJDK
cr.openjdk.org › ~jlaskey › templates › docs › api › java.base › java › io › FileReader.html
FileReader (Java SE 21 & JDK 21 [ad-hoc build])
java.io.Reader · java.io.InputStreamReader · java.io.FileReader · All Implemented Interfaces: Closeable, AutoCloseable, Readable · public class FileReader extends InputStreamReader · Reads text from character files using a default buffer size. Decoding from bytes to characters uses either ...
🌐
Java
download.java.net › java › early_access › panama › docs › api › java.base › java › io › Reader.html
Reader (Java SE 19 & JDK 19 [build 1])
a Reader which reads no characters · Since: 11 · public int read · (CharBuffer target) throws IOException · Attempts to read characters into the specified character buffer. The buffer is used as a repository of characters as-is: the only changes made are the results of a put operation.