Java File to String fileToString(File file)

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

Description

file To String

License

Open Source License

Declaration

public static String fileToString(File file) throws Exception 

Method Source Code

//package com.java2s;
/** // w w  w. j a  va 2 s . c om
 * Author:  anthony.fodor@gmail.com    
 * This code is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version,
* provided that any use properly credits the author.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details at http://www.gnu.org * * */

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.StringWriter;

public class Main {
    public static String fileToString(File file) throws Exception {
        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();
        return writer.toString();
    }
}

Related

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