<p>I've been using the idiom below for some time now. And it seems to be the most wide-spread, at least on the sites I've visited.</p>

<p>Is there a better/different way to read a file into a string in Java?</p>

<pre><code>private String readFile(String file) throws IOException {
    BufferedReader reader = new BufferedReader(new FileReader (file));
    String         line = null;
    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();
    }
}
</code></pre>

If you're willing to use an external library, check out
Apache Commons IO
(200KB JAR). It contains an
```
org.apache.commons.io.FileUtils.readFileToString()
```
method that allows you to read an entire
```
File
```
into a
```
String
```
with one line of code.
Example:
```
```
import java.io.*;
import java.nio.charset.*;
import org.apache.commons.io.*;

public String readFile() throws IOException {
    File file = new File("data.txt");
    return FileUtils.readFileToString(file, StandardCharsets.UTF_8);
}
```
```