Pages

Monday, June 7, 2021

Java Convert File Contents to String

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.

Java Convert File Contents to String



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.

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:
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.
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.


No comments:

Post a Comment

Please do not add any spam links in the comments section.