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

StringreadFileAsString(String filePath)
read File As String
StringBuffer fileData = new StringBuffer(1000);
BufferedReader reader = new BufferedReader(new FileReader(filePath));
char[] buf = new char[1024];
int numRead = 0;
while ((numRead = reader.read(buf)) != -1) {
    String readData = String.valueOf(buf, 0, numRead);
    fileData.append(readData);
    buf = new char[1024];
...
StringreadFileAsString(String filePath)
File as string [See also MixupUtil.fileToText(File file), which return the file's content and subject line as a concat.
try {
    StringBuffer fileData = new StringBuffer(1000);
    BufferedReader reader = new BufferedReader(new FileReader(filePath));
    char[] buf = new char[1024];
    int numRead = 0;
    while ((numRead = reader.read(buf)) != -1) {
        String readData = String.valueOf(buf, 0, numRead);
        fileData.append(readData);
...
StringreadFileAsString(String filePath)
read File As String
byte[] buffer = new byte[(int) new File(filePath).length()];
BufferedInputStream f = null;
try {
    f = new BufferedInputStream(new FileInputStream(filePath));
    f.read(buffer);
} finally {
    if (f != null) {
        try {
...
StringreadFileAsString(String filePath)
read File As String
File inFile = new File(filePath);
BufferedReader reader = new BufferedReader(new FileReader(inFile));
int cap = (int) inFile.length();
if (inFile.length() > Integer.MAX_VALUE) {
    cap = Integer.MAX_VALUE;
StringBuffer fileData = new StringBuffer(cap);
char[] buf = new char[32 * 1024];
...
StringreadFileAsString(String filePathname)
read File As String
StringBuffer sb = new StringBuffer();
BufferedReader in = null;
try {
    in = new BufferedReader(new FileReader(filePathname));
    String str;
    while ((str = in.readLine()) != null) {
        sb.append(str + "\n");
} finally {
    if (in != null) {
        in.close();
return sb.toString();
StringreadFileAsString(String fname)
read File As String
return readFile(new File(fname), true);
StringreadFileAsString(String fname)
read File As String
return new String(readFile(fname));
StringreadFileAsString(String fpath)
read File As String
final BufferedReader brdr = new BufferedReader(new FileReader(fpath));
final StringBuilder sr = new StringBuilder(fpath.length());
String s = null;
try {
    while ((s = brdr.readLine()) != null) {
        sr.append(s).append('\n');
} finally {
...
StringreadFileAsString(String path)
Given the path to a file (e.g.
StringBuffer sb = new StringBuffer(1000);
BufferedReader reader = new BufferedReader(new FileReader(path));
char[] buf = new char[1024];
int numRead = 0;
while ((numRead = reader.read(buf)) != -1) {
    sb.append(buf, 0, numRead);
reader.close();
...
ListreadFileAsString(String path)
read File As String
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(new File(path))));
try {
    List<String> list = new ArrayList<>();
    String line = reader.readLine();
    while (line != null) {
        list.add(line);
        line = reader.readLine();
    return list;
} finally {
    if (reader != null) {
        reader.close();