Java Utililty Methods Text File Read by Encoding

List of utility methods to do Text File Read by Encoding

Description

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

Method

StringreadFileAsString(File file, String encoding)
read File As String
StringBuilder sb = new StringBuilder();
ArrayList<String> lines = readFile(file, encoding);
for (String line : lines) {
    sb.append(line).append(NEW_LINE_CHARACTER);
return new String(sb);
StringreadFileAsString(File file, String encoding)
read File As String
StringBuffer fileData = new StringBuffer(1000);
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), encoding));
char[] buf = new char[1024];
int numRead = 0;
while ((numRead = reader.read(buf)) != -1) {
    fileData.append(buf, 0, numRead);
reader.close();
...
StringreadTextFile(File f, String fileEncoding)
Reads the file and returns the content as a String
StringBuffer sb = new StringBuffer();
InputStreamReader isr = new InputStreamReader(new FileInputStream(f), fileEncoding);
char[] buf = new char[255];
int read = isr.read(buf);
while (read > 0) {
    sb.append(buf, 0, read);
    read = isr.read(buf);
isr.close();
return sb.toString();
String[]readTextFile(File file, String encoding)
Reads a text file.
ArrayList lines = new ArrayList();
BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(file), encoding));
String line;
while ((line = in.readLine()) != null) {
    lines.add(line);
in.close();
return (String[]) lines.toArray(new String[lines.size()]);
...
StringreadTextFile(File file, String encoding)
read Text File
BufferedReader br;
if (encoding == null) {
    br = new BufferedReader(new FileReader(file));
} else if (!encoding.isEmpty()) {
    br = new BufferedReader(new FileReader(file));
} else {
    br = new BufferedReader(new InputStreamReader(new FileInputStream(file), encoding));
try {
    StringBuilder sb = new StringBuilder();
    String line = br.readLine();
    while (line != null) {
        sb.append(line);
        sb.append(String.format("%s%n", line));
        line = br.readLine();
    return sb.toString();
} finally {
    br.close();
StringBufferreadTextFile(File path, String encoding)
read Text File
StringBuffer sb = new StringBuffer("");
InputStreamReader read = null;
BufferedReader reader = null;
try {
    read = new InputStreamReader(new FileInputStream(path), encoding);
    reader = new BufferedReader(read);
    String line;
    while ((line = reader.readLine()) != null) {
...
String[]readTextFile(InputStream in, String encoding)
Reads the text from the given input stream in the default encoding.
ArrayList lines = new ArrayList();
BufferedReader bufferedIn;
if (encoding == null) {
    bufferedIn = new BufferedReader(new InputStreamReader(in));
} else {
    bufferedIn = new BufferedReader(new InputStreamReader(in, encoding));
String line;
...
String[]readTextFile(InputStream in, String encoding)
Reads the text from the given input stream in the default encoding.
ArrayList<String> lines = new ArrayList<String>();
BufferedReader bufferedIn;
if (encoding != null) {
    bufferedIn = new BufferedReader(new InputStreamReader(in, encoding));
} else {
    bufferedIn = new BufferedReader(new InputStreamReader(in));
String line;
...
StringreadTextFile(java.io.File file, String encoding)
Read text file to a string.
StringBuffer contents = new StringBuffer((int) file.length());
BufferedReader bufferedReader;
String safeEncoding = (encoding == null) ? "" : encoding;
if (safeEncoding.length() > 0) {
    bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(file), safeEncoding));
} else {
    bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
boolean eof = false;
String contentLine = "";
String eolChar = "\n";
while (!eof) {
    contentLine = bufferedReader.readLine();
    if (contentLine == null) {
        eof = true;
    } else {
        contents.append(contentLine);
        contents.append(eolChar);
bufferedReader.close();
return contents.toString();
StringreadTextFile(String sFileName, String sEncode)
read Text File
StringBuffer sbStr = new StringBuffer();
try {
    File ff = new File(sFileName);
    InputStreamReader read = new InputStreamReader(new FileInputStream(ff), sEncode);
    BufferedReader ins = new BufferedReader(read);
    String dataLine = "";
    while (null != (dataLine = ins.readLine())) {
        sbStr.append(dataLine);
...