Java Utililty Methods Text File Read

List of utility methods to do Text File Read

Description

The list of methods to do Text File Read are organized into topic(s).

Method

StringreadFile(String filePath)
read File
return readFile(filePath, true);
StringreadFile(String path)
read File
StringBuilder result = new StringBuilder();
BufferedReader reader = null;
try {
    reader = new BufferedReader(new FileReader(path));
    String buffer;
    while ((buffer = reader.readLine()) != null) {
        result.append(buffer + '\n');
    reader.close();
} catch (IOException e) {
    try {
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        JOptionPane.showMessageDialog(null, "<html><br>Could not read from file: " + path + "<br>"
                + "Error: " + e.toString() + "</html>", "Error!", JOptionPane.ERROR_MESSAGE);
    } catch (Exception ee) {
    e.printStackTrace();
return result.toString();
StringreadTextFile(File file, int max, String ellipsis)
Read a text file into a String, optionally limiting the length.
InputStream input = new FileInputStream(file);
try {
    if (max > 0) { 
        byte[] data = new byte[max + 1];
        int length = input.read(data);
        if (length <= 0)
            return "";
        if (length <= max)
...
StringreadTextFile(final DataInputStream is)
Read file as text file return as US-ASCII string
byte[] dataBuf = new byte[is.available()];
is.readFully(dataBuf);
return new String(dataBuf, "US-ASCII");
StringreadTextFile(final String path, final String enc)
read Text File
ByteArrayOutputStream os = new ByteArrayOutputStream();
try {
    File f = new File(path);
    if (f.exists()) {
        FileInputStream is = new FileInputStream(path);
        try {
            copyStream(is, os);
        } finally {
...
StringreadTextFile(InputStream inStream)
read Text File
String content = "";
try {
    int ca = inStream.available();
    byte[] by = new byte[ca];
    inStream.read(by);
    content = new String(by, encoding);
    inStream.close();
} catch (FileNotFoundException e) {
...
StringreadTextFile(String fileName)
read Text File
File file = new File(fileName);
FileInputStream fis = new FileInputStream(file);
byte[] data = new byte[(int) file.length()];
fis.read(data);
fis.close();
return new String(data, "UTF-8");
StringreadTextFile(String fn)
read Text File
FileInputStream fis = new FileInputStream(fn);
int numBytes = fis.available();
byte[] buf = new byte[numBytes];
fis.read(buf);
String content = new String(buf);
return content;
StringreadTextFileFast(final File file)
read Text File Fast
try {
    return new String(readCloseBinaryStream(new FileInputStream(file)));
} catch (FileNotFoundException e) {
    System.err.println("readTextFileFast: File " + file + " not found.");
return "";