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.
package com.oraclejavacertified.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 oraclejavacertified.blogspot.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);
}
}
Source: javacodegeeks.com
0 comments:
Post a Comment