Java File to String fileToString(File file)

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

Description

Reads a given file and stores its contents into a String

License

Open Source License

Parameter

Parameter Description
file a parameter

Exception

Parameter Description
IOException an exception

Declaration

public static String fileToString(File file) throws IOException 

Method Source Code


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

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

public class Main {
    /**/*from  ww  w  .ja v a  2 s  .  c o  m*/
     * Reads a given file and stores its contents into a String
     * @param file
     * @return
     * @throws IOException
     */
    public static String fileToString(File file) throws IOException {
        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();
    }
}

Related

  1. fileToString(File file)
  2. fileToString(File file)
  3. fileToString(File file)
  4. fileToString(File file)
  5. fileToString(File file)
  6. fileToString(File file, int max)
  7. fileToString(File file, String charsetName)
  8. fileToString(File file, String charsetName)
  9. fileToString(File files[])