Overview
In this tutorial, we show you how to read file to string with utf-8. We read a sequence of lines from a text file and output the file line by line on the console eclipse.We discuss three different ways for reading contents from a text file. The text file is stored at C:/data/fruits.txt with following content
Cherry Banana Chico fruit Jackfruit Kiwifruit Orange Pineapple Mango Apple
Use BufferedReader to read UTF-8 encoded data from a text file.
String filePath = "C:/data/fruits.txt"; StringBuilder sb = new StringBuilder(); try (BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(filePath), "UTF8"))) { String cLine; while ((cLine = br.readLine()) != null) { sb.append(cLine).append("\n"); } } catch (IOException e) { e.printStackTrace(); } System.out.println("##### Use BufferedReader to read UTF-8 encoded data from a text file"); System.out.println(sb.toString());
Reading file in JDK 7+
In JDK 1.7, we using the java.nio.file.Files class with Files.readAllBytes(Path) method to read a complete file in memory. This method returns a byte array, which can be passed to String constructor to create String out of itString contents = null; try { contents = new String(Files.readAllBytes(Paths.get(filePath))); } catch (IOException e) { e.printStackTrace(); } System.out.println("##### for JDK 7+"); System.out.println(contents);
Reading file line by line JDK 8+
We using java.nio.file.Files.lines() method, which returns a Stream of String read from file, where bytes are converted to character using UTF-8 character encoding.StringBuilder stringBuilder = new StringBuilder(); try (StreamReadFileToStringExample.javastream = Files.lines( Paths.get(filePath), StandardCharsets.UTF_8)) { stream.forEach(s -> stringBuilder.append(s).append("\n")); } catch (IOException e) { e.printStackTrace(); } System.out.println("##### for JDK 8+"); System.out.println(stringBuilder.toString());
package com.jackrutorial; import java.io.BufferedReader; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStreamReader; import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Paths; import java.util.stream.Stream; public class ReadFileToStringExample { public static void main(String[] args) { String filePath = "c:/data/fruits.txt"; //using BufferedReader StringBuilder sb = new StringBuilder(); try (BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(filePath), "UTF8"))) { String cLine; while ((cLine = br.readLine()) != null) { sb.append(cLine).append("\n"); } } catch (IOException e) { e.printStackTrace(); } System.out.println("##### Use BufferedReader to read UTF-8 encoded data from a text file"); System.out.println(sb.toString()); //for java 7+ String contents = null; try { contents = new String(Files.readAllBytes(Paths.get(filePath))); } catch (IOException e) { e.printStackTrace(); } System.out.println("##### for JDK 7+"); System.out.println(contents); //for java 8+ StringBuilder stringBuilder = new StringBuilder(); try (Streamstream = Files.lines( Paths.get(filePath), StandardCharsets.UTF_8)) { stream.forEach(s -> stringBuilder.append(s).append("\n")); } catch (IOException e) { e.printStackTrace(); } System.out.println("##### for JDK 8+"); System.out.println(stringBuilder.toString()); } }
Console Output
##### Use BufferedReader to read UTF-8 encoded data from a text file Cherry Banana Chico fruit Jackfruit Kiwifruit Orange Pineapple Mango Apple ##### for JDK 7+ Cherry Banana Chico fruit Jackfruit Kiwifruit Orange Pineapple Mango Apple ##### for JDK 8+ Cherry Banana Chico fruit Jackfruit Kiwifruit Orange Pineapple Mango Apple