Here you can find the source of readTextFileFast(final File file)
public static String readTextFileFast(final File file)
//package com.java2s; /*####################################################### * * Maintained by Gregor Santner, 2017- * https://gsantner.net//*from w ww. ja va 2 s . c om*/ * * License: Apache 2.0 * https://github.com/gsantner/opoc/#licensing * https://www.apache.org/licenses/LICENSE-2.0 * #########################################################*/ import java.io.BufferedInputStream; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; public class Main { private static final int BUFFER_SIZE = 4096; public static String readTextFileFast(final File file) { try { return new String(readCloseBinaryStream(new FileInputStream(file))); } catch (FileNotFoundException e) { System.err.println("readTextFileFast: File " + file + " not found."); } return ""; } public static byte[] readCloseBinaryStream(final InputStream stream, int byteCount) { final ArrayList<String> lines = new ArrayList<>(); BufferedInputStream reader = null; byte[] buf = new byte[byteCount]; int totalBytesRead = 0; try { reader = new BufferedInputStream(stream); while (totalBytesRead < byteCount) { int bytesRead = reader.read(buf, totalBytesRead, byteCount - totalBytesRead); if (bytesRead > 0) { totalBytesRead = totalBytesRead + bytesRead; } } } catch (IOException e) { e.printStackTrace(); } finally { if (reader != null) { try { reader.close(); } catch (IOException e) { e.printStackTrace(); } } } return buf; } public static byte[] readCloseBinaryStream(final InputStream stream) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); try { byte[] buffer = new byte[BUFFER_SIZE]; int read; while ((read = stream.read(buffer)) != -1) { baos.write(buffer, 0, read); } } catch (IOException e) { e.printStackTrace(); } finally { if (stream != null) { try { stream.close(); } catch (IOException e) { e.printStackTrace(); } } } return baos.toByteArray(); } }