Question
How can I read the full contents of a file into a String in Java?
I have been using code like this:
private String readFile(String file) throws IOException {
BufferedReader reader = new BufferedReader(new FileReader(file));
String line;
StringBuilder stringBuilder = new StringBuilder();
String ls = System.getProperty("line.separator");
try {
while ((line = reader.readLine()) != null) {
stringBuilder.append(line);
stringBuilder.append(ls);
}
return stringBuilder.toString();
} finally {
reader.close();
}
}
This approach seems common, but is there a better or more modern way to read a file into a string in Java? I would also like to understand any trade-offs, such as handling line breaks, character encoding, and resource management.
Short Answer
By the end of this page, you will understand how to read a whole file into a String in Java, why character encoding matters, how modern Java APIs simplify the task, and when older approaches like BufferedReader are still useful.
Concept
Reading a file into a String means loading all text from a file into memory as one Java object.
This is a common task when you need to:
- load configuration files
- read templates
- process JSON, XML, CSV, or plain text
- run tests using sample input files
The core idea is simple, but there are three important details:
1. Character encoding
A file stores bytes, not characters. Java must decode those bytes into text using an encoding such as UTF-8.
If you use the wrong encoding, text may become corrupted. For example:
émay display incorrectly- non-English characters may break
- symbols may be lost or replaced
That is why modern Java code often specifies StandardCharsets.UTF_8 explicitly.
2. Resource management
Files must be closed after reading. If not, your program can leak file handles.
Older code often uses try/finally to close readers manually. Modern Java uses try-with-resources, which closes resources automatically.
3. Preserving content exactly
Methods like readLine() remove line terminators. If you rebuild the string manually, you may accidentally:
- change line endings
- add an extra newline at the end
- lose the file's original formatting
Modern methods such as or usually make this easier and safer.
Mental Model
Think of a text file like a sealed box full of letters.
- The file is the box.
- The bytes are the raw paper slips inside.
- The character encoding is the language guide that tells Java how to interpret those slips.
- The String is the final readable message.
If you use the wrong guide, the message looks wrong. If you open the box and forget to close it, you leave a mess behind. If you copy line by line and add your own separators, you may not reproduce the original message exactly.
So the safest approach is often: open the file correctly, decode it using the right encoding, and let Java return the full text directly.
Syntax and Examples
Modern Java: Files.readString()
If you are using Java 11 or later, this is the clearest approach:
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
public class FileExample {
public static String readFile(String filePath) throws IOException {
return Files.readString(Path.of(filePath), StandardCharsets.UTF_8);
}
}
Why this is good
- short and readable
- automatically handles opening and closing internally
- lets you specify encoding explicitly
- preserves the file content as text
Java 7+: Files.readAllBytes()
If Files.readString() is not available, you can do this:
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
public class FileExample {
String IOException {
[] bytes = Files.readAllBytes(Path.of(filePath));
(bytes, StandardCharsets.UTF_8);
}
}
Step by Step Execution
Consider this code:
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
public class Demo {
public static void main(String[] args) throws IOException {
String text = Files.readString(Path.of("example.txt"), StandardCharsets.UTF_8);
System.out.println(text);
}
}
Assume example.txt contains:
Hello
Java
What happens step by step
1. Path.of("example.txt")
Java creates a Path object representing the file location.
2. Files.readString(...)
Java opens the file and reads all bytes from it.
3. StandardCharsets.UTF_8
Java decodes those bytes into characters using UTF-8.
Real World Use Cases
Reading a file into a string is useful in many real applications:
Loading JSON or XML
A backend service may read a JSON file before parsing it.
String json = Files.readString(Path.of("config.json"), StandardCharsets.UTF_8);
Reading SQL scripts
Applications often load .sql files and run them against a database.
Email or HTML templates
A server might read a template file into a string, then replace placeholders.
Test fixtures
Automated tests often compare expected output with the contents of a saved text file.
Import tools
A script may read log files, CSV data, or plain-text reports into memory for processing.
Configuration loading
Small config files are commonly loaded fully into memory because they are easy to parse once stored in a string.
This approach is best when the file is reasonably small and you really need the entire content at once.
Real Codebase Usage
In real projects, developers usually choose one of these patterns:
1. Small file, whole content needed
Use Files.readString().
String template = Files.readString(path, StandardCharsets.UTF_8);
Common for:
- templates
- config files
- test resources
- small data files
2. Need explicit validation
Developers often check whether the file exists before reading.
if (!Files.exists(path)) {
throw new IOException("File not found: " + path);
}
String content = Files.readString(path, StandardCharsets.UTF_8);
This is a guard clause: fail early if input is invalid.
3. Stream or process lines
If the file may be large, developers avoid loading the whole thing into memory.
try (var lines = Files.lines(path, StandardCharsets.UTF_8)) {
lines.filter(line -> !line.isBlank())
.forEach(System.out::println);
}
This is common in data processing and log analysis.
Common Mistakes
1. Using FileReader without thinking about encoding
FileReader uses the platform default encoding unless you use a newer constructor overload. That can make your program behave differently on different machines.
Risky code
BufferedReader reader = new BufferedReader(new FileReader("data.txt"));
Better
BufferedReader reader = Files.newBufferedReader(Path.of("data.txt"), StandardCharsets.UTF_8);
2. Forgetting to close the reader
Broken code
BufferedReader reader = Files.newBufferedReader(Path.of("data.txt"), StandardCharsets.UTF_8);
String line = reader.readLine();
If you never close the reader, resources can leak.
Better
Comparisons
| Approach | Java Version | Encoding Control | Preserves Original Text Exactly | Best For |
|---|---|---|---|---|
Files.readString(path, charset) | 11+ | Yes | Usually yes | Small text files, simplest modern option |
Files.readAllBytes(path) + new String(...) | 7+ | Yes | Yes | Whole-file reading when readString is unavailable |
BufferedReader.readLine() + StringBuilder | Any modern Java | Yes, if created correctly | No, not automatically | Line-by-line handling or custom processing |
Cheat Sheet
Read whole file into a string
Java 11+
String text = Files.readString(Path.of("file.txt"), StandardCharsets.UTF_8);
Java 7+
String text = new String(Files.readAllBytes(Path.of("file.txt")), StandardCharsets.UTF_8);
Read line by line
try (BufferedReader reader = Files.newBufferedReader(Path.of("file.txt"), StandardCharsets.UTF_8)) {
String line;
while ((line = reader.readLine()) != null) {
// use line
}
}
Key rules
- Prefer
PathandFilesover olderFileReadercode. - Specify encoding explicitly, usually
StandardCharsets.UTF_8. - Use
try-with-resourceswhen working with readers or streams.
FAQ
What is the easiest way to read a file into a string in Java?
If you use Java 11 or later, Files.readString(path, StandardCharsets.UTF_8) is usually the easiest and clearest option.
Why should I specify UTF-8 explicitly?
If you do not specify an encoding, your program may depend on the machine's default encoding, which can cause inconsistent results.
Does BufferedReader.readLine() keep newline characters?
No. It returns each line without the line terminator.
Is it okay to read an entire file into memory?
Yes, for small or moderate text files. For very large files, it is better to process the file incrementally.
Should I still use FileReader?
Usually, modern code prefers Files.newBufferedReader() or Files.readString() because they work well with Path and explicit charsets.
What happens if the file does not exist?
Java throws an IOException, commonly a NoSuchFileException when using NIO APIs.
How do I preserve the exact file content?
Use Files.readString() or Files.readAllBytes() instead of rebuilding text with .
Mini Project
Description
Build a small Java utility that loads a text template from a file and prints it to the console. This demonstrates the most common real-world use of reading a file into a string: loading full text content such as templates, config snippets, or sample data.
Goal
Create a program that reads a UTF-8 text file into a single String and displays its contents safely.
Requirements
- Read the file path from a
Stringvariable in the program. - Load the full file contents as UTF-8 text.
- Print the contents to the console.
- Show a friendly error message if the file cannot be read.
Keep learning
Related questions
Avoiding Java Code in JSP with JSP 2: EL and JSTL Explained
Learn how to avoid Java scriptlets in JSP 2 using Expression Language and JSTL, with examples, best practices, and common mistakes.
Choosing a @NotNull Annotation in Java: Validation vs Static Analysis
Learn how Java @NotNull annotations differ, when to use each one, and how to choose between validation, IDE hints, and static analysis tools.
Convert a Java Stack Trace to a String
Learn how to convert a Java exception stack trace to a string using StringWriter and PrintWriter, with examples and common mistakes.