Java File to String fileToString(File f)

Here you can find the source of fileToString(File f)

Description

file To String

License

Open Source License

Declaration

public static String fileToString(File f) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.io.*;

public class Main {
    public static String fileToString(File f) {
        try {/* w w w .j a  v  a2 s  . com*/
            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;
                j++;
            }
            fr.close();
            String ret = new String(tmp);
            return ret;
        } catch (Exception e) {
            System.err.println("failed to read file: \"" + f.getName() + "\"!");
            return "";
        }
    }
}

Related

  1. fileToStream(final File file)
  2. fileToStream(String filename)
  3. fileToString(File f)
  4. fileToString(File f)
  5. fileToString(File f)
  6. fileToString(File f)
  7. fileToString(File f)
  8. fileToString(File f)
  9. fileToString(File f, String encoding)