This article provides all the ways of reading file into a String in Java along with performance statistics.
Performance statistics
Below are time taken by file reading methods with 50MB file. To get fair statistics, 5 rounds of method call are counted.
Reading method | 50 MB file read time (Milli-Seconds) |
---|---|
Java java.io.FileReader | 88420, 83222, 85353, 83131, 85252 |
Java java.io.BufferedReader | 83263, 85080, 83288, 84097, 83171 |
Java NIO java.nio.file.Files | 59, 37, 35, 36, 35 |
Apache common-io org.apache.commons.io.FileUtils | 168, 113, 104, 145, 136 |
Google com.google.common.io.Files | 63, 61, 58, 59, 56 |
java.io.FileReader
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
/** * Read file with Java's FileReader * * @param fileToRead * @return */ public static String readWithFileReader(String fileToRead) { String readData = ""; try (FileReader fileReader = new FileReader(fileToRead)) { while (fileReader.ready()) { char[] c = new char[8192]; fileReader.read(c); readData = readData + new String(c); } } catch (IOException e) { e.printStackTrace(); } return readData; } |
java.io.BufferedReader
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
/** * Read file with Java's BufferedReader * * @param fileToRead * @return */ public static String readWithBufferedReader(String fileToRead) { String readData = ""; try (FileReader fileReader = new FileReader(fileToRead); BufferedReader bufferedReader = new BufferedReader(fileReader)) { while (bufferedReader.ready()) { char[] c = new char[8192]; bufferedReader.read(c); readData = readData + new String(c); } } catch (IOException e) { e.printStackTrace(); } return readData; } |
java.nio.file.Files
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
/** * Read file with Java's NIO Files * * @param fileToRead * @return */ public static String readWithNioFiles(String fileToRead) { String readData = ""; try { readData = Files.readString(Path.of(fileToRead)); } catch (IOException e) { e.printStackTrace(); } return readData; } |
org.apache.commons.io.FileUtils
1 2 3 4 5 6 7 8 9 10 11 12 |
/** * Read file with Apache commons-io FileUtils */ public static String readWithApacheCommons(String fileToRead) { String readData = ""; try { readData = FileUtils.readFileToString(new File(fileToRead), "UTF-8"); } catch (IOException e) { e.printStackTrace(); } return readData; } |
com.google.common.io.Files
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
/** * Read file with google guava common io Files. * * @param fileToRead * @return */ public static String readWithGoogleGuava(String fileToRead) { String readData = ""; try { readData = com.google.common.io.Files.asCharSource(new File(fileToRead), Charset.defaultCharset()).read(); } catch (IOException e) { e.printStackTrace(); } return readData; } |
Complete code with to test reading & performance
Here is the complete code which tests reading of file into Sting, print it. Also measures performance time take of all methods using Java Instant & Duration. Takes 5 readings to get fair stats.
1 2 3 4 5 6 7 8 9 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 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 |
package com.itsallbinary.java; import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.IOException; import java.nio.charset.Charset; import java.nio.file.Files; import java.nio.file.Path; import java.time.Duration; import java.time.Instant; import java.util.Arrays; import java.util.stream.Collectors; import org.apache.commons.io.FileUtils; public class WaysToReadTextFileToStringPerformance { private static final String FILE_TO_READ = "ItsAllBinary.txt"; private static final String HUGE_50MB_FILE_TO_READ = "HugeItsAllBinary.txt"; public static void main(String[] args) throws IOException { /* * Read file to String & print string. */ String readWithFileReder = readWithFileReader(FILE_TO_READ); String readWithBufferedReader = readWithBufferedReader(FILE_TO_READ); String readWithNioFiles = readWithNioFiles(FILE_TO_READ); String readWithApacheCommons = readWithApacheCommons(FILE_TO_READ); String readWithGoogleGuava = readWithGoogleGuava(FILE_TO_READ); System.out.println("Read using java.io.FileReader.read() = " + readWithFileReder); System.out.println("Read using java.io.BufferedReader.read() = " + readWithBufferedReader); System.out.println("Read using java.nio.file.Files.readString() = " + readWithNioFiles); System.out.println("Read using org.apache.commons.io.FileUtils.readFileToString() = " + readWithApacheCommons); System.out.println("Read using com.google.common.io.Files.asCharSource() = " + readWithGoogleGuava); /* * Read HUGE file & print time taken. 5 iterations will be done of each to get * fair results. */ String readWithFileRederPerf = measurePerformance(() -> readWithFileReader(HUGE_50MB_FILE_TO_READ)); String readWithBufferedReaderPerf = measurePerformance(() -> readWithBufferedReader(HUGE_50MB_FILE_TO_READ)); String readWithNioFilesPerf = measurePerformance(() -> readWithNioFiles(HUGE_50MB_FILE_TO_READ)); String readWithApacheCommonsPerf = measurePerformance(() -> readWithApacheCommons(HUGE_50MB_FILE_TO_READ)); String readWithGoogleGuavaPerf = measurePerformance(() -> readWithGoogleGuava(HUGE_50MB_FILE_TO_READ)); System.out.println("Time for java.io.FileReader.read() = " + readWithFileRederPerf); System.out.println("Time for java.io.BufferedReader() = " + readWithBufferedReaderPerf); System.out.println("Time for java.nio.file.Files.readString() = " + readWithNioFilesPerf); System.out .println("Time for org.apache.commons.io.FileUtils.readFileToString() = " + readWithApacheCommonsPerf); System.out.println("Time for com.google.common.io.Files.asCharSource() = " + readWithGoogleGuavaPerf); } /** * Measure method performance execution time using java.time.Duration & * java.time.Instant. * * @param method * @return Time taken for method in seconds or millis */ public static String measurePerformance(Runnable method) { String[] durations = new String[5]; for (int i = 0; i < 5; i++) { Instant start = Instant.now(); method.run(); Duration duration = Duration.between(start, Instant.now()); durations[i] = duration.toMillis() + " Milli-Seconds"; } return Arrays.stream(durations).collect(Collectors.joining(", ")); } /** * Read file with Java's FileReader * * @param fileToRead * @return */ public static String readWithFileReader(String fileToRead) { String readData = ""; try (FileReader fileReader = new FileReader(fileToRead)) { while (fileReader.ready()) { char[] c = new char[8192]; fileReader.read(c); readData = readData + new String(c); } } catch (IOException e) { e.printStackTrace(); } return readData; } /** * Read file with Java's BufferedReader * * @param fileToRead * @return */ public static String readWithBufferedReader(String fileToRead) { String readData = ""; try (FileReader fileReader = new FileReader(fileToRead); BufferedReader bufferedReader = new BufferedReader(fileReader)) { while (bufferedReader.ready()) { char[] c = new char[8192]; bufferedReader.read(c); readData = readData + new String(c); } } catch (IOException e) { e.printStackTrace(); } return readData; } /** * Read file with Java's NIO Files * * @param fileToRead * @return */ public static String readWithNioFiles(String fileToRead) { String readData = ""; try { readData = Files.readString(Path.of(fileToRead)); } catch (IOException e) { e.printStackTrace(); } return readData; } /** * Read file with Apache commons-io FileUtils */ public static String readWithApacheCommons(String fileToRead) { String readData = ""; try { readData = FileUtils.readFileToString(new File(fileToRead), "UTF-8"); } catch (IOException e) { e.printStackTrace(); } return readData; } /** * Read file with google guava common io Files. * * @param fileToRead * @return */ public static String readWithGoogleGuava(String fileToRead) { String readData = ""; try { readData = com.google.common.io.Files.asCharSource(new File(fileToRead), Charset.defaultCharset()).read(); } catch (IOException e) { e.printStackTrace(); } return readData; } } |
Output:
1 2 3 4 5 6 7 8 9 10 |
Read using java.io.FileReader.read() = Welcome to Its All Binary ! Read using java.io.BufferedReader.read() = Welcome to Its All Binary ! Read using java.nio.file.Files.readString() = Welcome to Its All Binary ! Read using org.apache.commons.io.FileUtils.readFileToString() = Welcome to Its All Binary ! Read using com.google.common.io.Files.asCharSource() = Welcome to Its All Binary ! Time for java.io.FileReader.read() = 88420 Milli-Seconds, 83222 Milli-Seconds, 85353 Milli-Seconds, 83131 Milli-Seconds, 85252 Milli-Seconds Time for java.io.BufferedReader() = 83263 Milli-Seconds, 85080 Milli-Seconds, 83288 Milli-Seconds, 84097 Milli-Seconds, 83171 Milli-Seconds Time for java.nio.file.Files.readString() = 59 Milli-Seconds, 37 Milli-Seconds, 35 Milli-Seconds, 36 Milli-Seconds, 35 Milli-Seconds Time for org.apache.commons.io.FileUtils.readFileToString() = 168 Milli-Seconds, 113 Milli-Seconds, 104 Milli-Seconds, 145 Milli-Seconds, 136 Milli-Seconds Time for com.google.common.io.Files.asCharSource() = 63 Milli-Seconds, 61 Milli-Seconds, 58 Milli-Seconds, 59 Milli-Seconds, 56 Milli-Seconds |