Java Utililty Methods File to String

List of utility methods to do File to String

Description

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

Method

StringfileToString(String file)
file To String
try {
    File fileDir = new File(file);
    BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(fileDir), "UTF8"));
    String str, strOut = "";
    while ((str = in.readLine()) != null) {
        str = str.replace("\uFEFF", "");
        strOut += str + "\r\n";
    in.close();
    return strOut;
catch (Exception e) {
    return "";
StringfileToString(String file)
Read the contents of a file and place them in a string object.
String contents = "";
File f = null;
try {
    f = new File(file);
    if (f.exists()) {
        FileReader fr = null;
        try {
            fr = new FileReader(f);
...
StringfileToString(String file, String encoding)
file To String
FileInputStream fis = new FileInputStream(file);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buffer = new byte[10240];
int len = fis.read(buffer, 0, buffer.length);
while (len > 0) {
    baos.write(buffer, 0, len);
    len = fis.read(buffer, 0, buffer.length);
fis.close();
return baos.toString(encoding);
StringFileToString(String file_name)
File To String
FileInputStream fis = new FileInputStream(file_name);
return InputStreamToString(fis);
StringfileToString(String filename)
Returns the string representation of a given file.
StringBuffer sb = new StringBuffer();
BufferedReader in = new BufferedReader(new FileReader(filename));
String str;
while ((str = in.readLine()) != null)
    sb.append(str);
in.close();
return sb.toString();
StringfileToString(String filename)
file To String
String result = null;
DataInputStream in = null;
try {
    byte[] buffer = new byte[(int) new File(filename).length()];
    in = new DataInputStream(new FileInputStream(filename));
    in.readFully(buffer);
    result = new String(buffer);
    in.close();
...
StringfileToString(String fileName)
read the file content as a String with default character encoding.
StringBuffer stringBuffer = new StringBuffer();
try {
    char[] buffer = new char[1024];
    FileReader reader = new FileReader(fileName);
    int flag = -1;
    while ((flag = reader.read(buffer, 0, buffer.length)) != -1) {
        stringBuffer.append(buffer, 0, flag);
} catch (FileNotFoundException e) {
    throw new IllegalArgumentException("[" + fileName + "] is not found.");
} catch (IOException e) {
    throw new RuntimeException("Unexpected exception! caused by: " + e.getMessage(), e);
return stringBuffer.toString();
StringfileToString(String fileName)
file To String
System.gc();
File file = new File(fileName);
FileInputStream fis = new FileInputStream(file);
BufferedReader dis = new BufferedReader(new InputStreamReader(fis));
StringBuffer buff = new StringBuffer();
String line = null;
while ((line = dis.readLine()) != null) {
    buff.append(line);
...
StringfileToString(String fileName)
file To String
String result = "";
try {
    FileInputStream file = new FileInputStream(fileName);
    byte[] b = new byte[file.available()];
    file.read(b);
    file.close();
    result = new String(b);
} catch (FileNotFoundException e) {
...
StringfileToString(String filename)
file To String
BufferedReader reader = new BufferedReader(new FileReader(filename));
String line = null;
StringBuilder stringBuilder = new StringBuilder();
String ls = System.getProperty("line.separator");
while ((line = reader.readLine()) != null) {
    stringBuilder.append(line);
    stringBuilder.append(ls);
return stringBuilder.toString();