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

StringfileToStr(String strInput)
file To Str
File f = new File(strInput);
StringBuilder sb = new StringBuilder(new Long(f.length()).intValue());
FileInputStream fIn = new FileInputStream(strInput);
BufferedReader br = new BufferedReader(new InputStreamReader(fIn));
String line = null;
while ((line = br.readLine()) != null) {
    sb.append(line);
    sb.append("\n");
...
voidfileToStream(File source, OutputStream target)
file To Stream
try (InputStream is = new BufferedInputStream(new FileInputStream(source));
        OutputStream os = new BufferedOutputStream(target)) {
    byte[] buffer = new byte[1024];
    int bytesRead = 0;
    while (bytesRead != -1) {
        bytesRead = is.read(buffer);
        if (bytesRead > 0) {
            os.write(buffer, 0, bytesRead);
...
InputStreamfileToStream(final File file)
Reads a file and returns it as a stream.
try {
    return new BufferedInputStream(new FileInputStream(file));
} catch (final IOException ioEx) {
    throw new RuntimeException(ioEx);
InputStreamfileToStream(String filename)
file To Stream
FileInputStream fis = new FileInputStream(filename);
DataInputStream dis = new DataInputStream(fis);
byte[] bytes = new byte[dis.available()];
dis.readFully(bytes);
ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
return bais;
StringfileToString(File f)
file To String
return fileToString(f, Integer.MAX_VALUE);
StringfileToString(File f)
file To String
if (f.exists()) {
    try {
        int bufsize = (int) f.length();
        InputStream fis = new FileInputStream(f);
        return streamToString(fis, bufsize);
    } catch (Exception e) {
        return null;
} else
    return null;
StringfileToString(File f)
file To String
Scanner m = new Scanner(f);
String s = "";
while (m.hasNextLine()) {
    s = s + m.nextLine().trim() + "\n";
m.close();
return s.trim();
StringfileToString(File f)
file To String
try {
    FileReader fr = new FileReader(f);
    char[] tmp = new char[(int) f.length()];
    char c;
    int j = 0;
    for (int i = fr.read(); i != -1; i = fr.read()) {
        c = (char) i;
        tmp[j] = c;
...
StringfileToString(File f)
Reads the contents of an File into a String
StringBuilder sb = new StringBuilder();
String line;
BufferedReader br = new BufferedReader(new FileReader(f));
while ((line = br.readLine()) != null) {
    sb.append(line);
return sb.toString();
String[]fileToString(File f)
Read a file and return its contents as a string.
try {
    StringBuffer buff = new StringBuffer();
    BufferedReader in = new BufferedReader(new FileReader(f));
    String line = null;
    while ((line = in.readLine()) != null) {
        buff.append(line);
        buff.append("\n");
    in.close();
    return new String[] { buff.toString() };
} catch (Exception e) {
    throw new RuntimeException(e);