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(File f)
Reads in a File and dumps it into a String.
Scanner s = new Scanner(f);
StringBuilder sb = new StringBuilder();
while (s.hasNextLine()) {
    sb.append(s.nextLine() + '\n');
s.close();
return sb.toString();
StringfileToString(File f, String encoding)
Reads the contents of a file into a String.
Scanner m = new Scanner(f, encoding);
String s = "";
while (m.hasNextLine())
    s += m.nextLine().trim() + "\n";
m.close();
return s.trim();
StringfileToString(File file)
Read in the contents of a file and return it as a String.
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
FileInputStream fileInputStream = new FileInputStream(file);
try {
    writeInputStreamToOutputStream(fileInputStream, byteArrayOutputStream);
} finally {
    try {
        fileInputStream.close();
    } catch (Throwable ignore) {
...
StringfileToString(File file)
file To String
FileInputStream fis = null;
String str = "";
try {
    fis = new FileInputStream(file);
    int content;
    while ((content = fis.read()) != -1) {
        str += (char) content;
    return str;
} catch (IOException e) {
    e.printStackTrace();
} finally {
    try {
        if (fis != null)
            fis.close();
    } catch (IOException ex) {
        ex.printStackTrace();
return str;
StringfileToString(File file)
file To String
BufferedReader reader = new BufferedReader(new FileReader(file));
StringWriter writer = new StringWriter();
char[] c = new char[2048];
int bytesRead;
while ((bytesRead = reader.read(c, 0, c.length)) != -1)
    writer.write(c, 0, bytesRead);
writer.close();
reader.close();
...
StringfileToString(File file)
Reads the entire file content.
return inputStreamToString(new FileInputStream(file));
StringfileToString(File file)
Reads a complete text file.
try {
    return new Scanner(file).useDelimiter("\\Z").next();
} catch (FileNotFoundException e) {
    throw new RuntimeException("Failed to read file: " + file, e);
StringfileToString(File file)
file To String
StringBuilder sb = new StringBuilder();
try (BufferedInputStream inputStream = new BufferedInputStream(new FileInputStream(file))) {
    for (int b; (b = inputStream.read()) != -1;) {
        String s = Integer.toHexString(b).toUpperCase();
        if (s.length() == 1) {
            sb.append('0');
        sb.append(s).append(' ');
...
CollectionfileToString(File file)
Reads a File and places the contents into a collection of Strings.
BufferedReader br;
try {
    br = new BufferedReader(new FileReader(file));
} catch (FileNotFoundException fnfe) {
    return null;
String line;
try {
...
StringfileToString(File file)
Reads a given file and stores its contents into a String
FileReader fr = new FileReader(file);
BufferedReader br = new BufferedReader(fr);
StringBuilder result = new StringBuilder();
String line = br.readLine();
while (line != null) {
    result.append(line + "\n");
    line = br.readLine();
fr.close();
br.close();
return result.toString();