Home » Java » Core Java » Java Convert File Contents to String

About Venkatesh Nukala

Venkatesh Nukala
Venkatesh Nukala is a Software Engineer working for Online Payments Industry Leading company. In my free time, I would love to spend time with family and write articles on technical blogs. More on JavaProgramTo.com

Java Convert File Contents to String

A quick guide on how to convert the file contents to String using Java Files api and with example programs.

1. Overview

In this tutorial, you’ll learn how to convert File contents to String in java.

If you are new to java 8, please read the how to read the file in java 8? we have already shown the different ways to read the file line by line.

Java new Files api has two useful methods to read the file.

readAllLines()

readAllBytes()

Let us write the examples on each method to convert file to string in java.

2. Java File to String – Files.readAllLines()

In the below example program, first we have stored the file location in the string variable.

Next, took the default char set for file encoding using Charset.defaultCharset().

Next, invoke  Files.readAllLines() method with the file path.

This method returns a List of strings as List<String>.

Finally, use java 8 stream api collect() and joining() methods to convert List to String.

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package com.javaprogramto.files.tostring;
 
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;
import java.util.stream.Collectors;
 
/**
 * Java example to convert File To String.
 *
 * @author javaprogramto.com
 *
 */
public class JavaFilesToString {
 
    public static void main(String[] args) throws IOException {
 
        // file location
        String filePath = "/CoreJava/src/main/resources/dummy.txt";
 
        // charset for encoding
        Charset encoding = Charset.defaultCharset();
 
        // reading all lines of file as List of strings
        List<String> lines = Files.readAllLines(Paths.get(filePath), encoding);
         
        // converting List<String> to palin string using java 8 api.
        String string = lines.stream().collect(Collectors.joining("\n"));
         
        // printing the final string.
        System.out.println("file as string ");
        System.out.println(string);
 
    }
 
}

Output:

1
2
3
4
5
6
file as string
Line 1 : Hello Reader
Line 2 : Welcome to the java programt to . com blog
Line 3 : Here you find the articles on the java concepts
Line 4 : This is example to read this file line by line
Line 5 : Let us see the examples on Java 8 Streams API.

3. Java File to String – Files.readAllBytes()

Next, call Files.readAllBytes() method which returns byte[] array as a result. Use String class to convert byte array to string with the default encoding technique.

The below program produces the same output as in the section 2.

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
public class JavaFilesToStringBytes {
 
    public static void main(String[] args) throws IOException {
 
        // file location
        String filePath = "/Users/venkateshn/Documents/VenkY/blog/workspace/CoreJava/src/main/resources/dummy.txt";
 
        // charset for encoding
        Charset encoding = Charset.defaultCharset();
 
        // reading all lines of file as List of strings
        byte[]  bytes = Files.readAllBytes(Paths.get(filePath));
         
        // converting List<String> to palin string using java 8 api.
        String string = new String(bytes, encoding);
         
        // printing the final string.
        System.out.println("file as string ");
        System.out.println(string);
 
    }
 
}

4. Conclusion

In this article, you’ve seen how to convert File contents to String using java 8 stream methods.

GitHub

Files API

Published on Java Code Geeks with permission by Venkatesh Nukala, partner at our JCG program. See the original article here: Java Convert File Contents to String

Opinions expressed by Java Code Geeks contributors are their own.

(0 rating, 0 votes)
You need to be a registered member to rate this.
Start the discussion Views Tweet it!
Do you want to know how to develop your skillset to become a Java Rockstar?
Subscribe to our newsletter to start Rocking right now!
To get you started we give you our best selling eBooks for FREE!
1. JPA Mini Book
2. JVM Troubleshooting Guide
3. JUnit Tutorial for Unit Testing
4. Java Annotations Tutorial
5. Java Interview Questions
6. Spring Interview Questions
7. Android UI Design
and many more ....
I agree to the Terms and Privacy Policy
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments