Java Text File Load loadTextFile(File textFile)

Here you can find the source of loadTextFile(File textFile)

Description

load Text File

License

Apache License

Declaration

public static String loadTextFile(File textFile) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.io.*;

public class Main {
    public static String loadTextFile(File textFile) {
        try {/*from ww w .j  ava  2 s .c o  m*/
            try (FileInputStream fileStream = new FileInputStream(textFile)) {
                return new String(loadBytes(fileStream));
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public static byte[] loadBytes(InputStream inputStream) {
        try {
            ByteArrayOutputStream buffer = new ByteArrayOutputStream();

            int nRead;
            byte[] data = new byte[16384];

            while ((nRead = inputStream.read(data, 0, data.length)) != -1) {
                buffer.write(data, 0, nRead);
            }
            buffer.flush();
            return buffer.toByteArray();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}

Related

  1. loadText(java.io.File f)
  2. loadText(String fname)
  3. loadText(String path)
  4. loadText(String path)
  5. loadTextFile(File file)
  6. loadTextFile(final String filePath)
  7. loadTextFile(String fileFullPath)
  8. loadTextFile(String fileName)
  9. loadTextFile(String filename)