EN
Java - read all text from file with custom FileUtils
11 points
In this article, we would like to show how in Java, read all text from file to string.
Quick solution:
xxxxxxxxxx
1
package com.dirask.examples;
2
3
import java.io.FileInputStream;
4
import java.io.IOException;
5
import java.io.InputStreamReader;
6
import java.io.Reader;
7
import java.nio.charset.Charset;
8
import java.nio.charset.StandardCharsets;
9
10
class Program {
11
12
public static void main(String[] args) throws IOException {
13
14
String path = "C:\\Project\\input.txt";
15
String text = readText(path, StandardCharsets.UTF_8);
16
17
System.out.println(text);
18
}
19
20
private static String readText(String path, Charset charset) throws IOException {
21
StringBuilder builder = new StringBuilder();
22
try (FileInputStream stream = new FileInputStream(path);
23
Reader reader = new InputStreamReader(stream, charset))
24
{
25
char[] buffer = new char[1024];
26
while (true) {
27
int count = reader.read(buffer, 0, buffer.length);
28
if (count == -1) {
29
break;
30
}
31
builder.append(buffer, 0, count);
32
}
33
}
34
return builder.toString();
35
}
36
}
Example output:
xxxxxxxxxx
1
This is 1st line...
2
This is 2nd line...
3
This is 3rd line...
Example input.txt
:

This article presents some simple util that lets users to read file using: String
path, File
object, Path
object, Reader
object or InputStream
object.
Note: check below
FileUtils.java
file to see how text reading util looks.
Program.java
file:
xxxxxxxxxx
1
package com.dirask.examples;
2
3
import java.io.*;
4
import java.nio.file.Paths;
5
6
public class Program {
7
8
public static void main(String[] args) throws IOException {
9
10
readWithStringPath();
11
readWithFileObject();
12
readWithPathObject();
13
readWithReaderObject();
14
readWithInputStreamObject();
15
}
16
17
private static void readWithStringPath() {
18
String text = FileUtils.readText("C:\\Project\\input.txt");
19
System.out.println(text);
20
}
21
22
private static void readWithFileObject() {
23
String text = FileUtils.readText(new File("C:\\Project\\input.txt"));
24
System.out.println(text);
25
}
26
27
private static void readWithPathObject() {
28
String text = FileUtils.readText(Paths.get("C:\\Project\\input.txt"));
29
System.out.println(text);
30
}
31
32
private static void readWithReaderObject() {
33
try(FileReader reader = new FileReader("C:\\Project\\input.txt")) {
34
String text = FileUtils.readText(reader);
35
36
System.out.println(text);
37
}
38
}
39
40
private static void readWithInputStreamObject() {
41
try(InputStream stream = new FileInputStream("C:\\Project\\input.txt")) {
42
String text = FileUtils.readText(stream);
43
44
System.out.println(text);
45
}
46
}
47
}
FileUtils.java
file:
xxxxxxxxxx
1
package com.dirask.examples;
2
3
import java.io.*;
4
import java.nio.file.Path;
5
6
public class FileUtils {
7
8
public static String readText(Reader reader) throws IOException {
9
StringBuilder builder = new StringBuilder();
10
char[] buffer = new char[1024];
11
while (true) {
12
int count = reader.read(buffer, 0, buffer.length);
13
if (count == -1) {
14
break;
15
}
16
builder.append(buffer, 0, count);
17
}
18
return builder.toString();
19
}
20
21
public static String readText(InputStream stream) throws IOException {
22
Reader reader = new InputStreamReader(stream, "UTF-8");
23
return readText(reader);
24
}
25
26
public static String readText(File file) throws IOException {
27
try (InputStream stream = new FileInputStream(file)) {
28
return readText(stream);
29
}
30
}
31
32
public static String readText(Path path) throws IOException {
33
File file = path.toFile();
34
return readText(file);
35
}
36
37
public static String readText(String path) throws IOException {
38
try (FileInputStream stream = new FileInputStream(path)) {
39
return readText(stream);
40
}
41
}
42
}
input.txt
file (full path is C:\Project\input.txt
):
xxxxxxxxxx
1
This is 1st line...
2
This is 2nd line...
3
This is 3rd line...
- Java - how to read all lines as one string from file object?
- Java - how to read all lines as one string from input stream object?
- Java - how to read all lines as one string from reader object?
- Java - how to read all lines as one string using file path string?
- Java - how to read all lines as one string using path object?
Alternative titles
- Java - how to read all lines as one string from file object?
- Java - how to read all lines as one string from input stream object?
- Java - how to read all lines as one string from reader object?
- Java - how to read all lines as one string using file path string?
- Java - how to read all lines as one string using path object?