Java BufferedReader Read readFile(File file)

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

Description

Simply read a file's contents into a string object.

License

BSD License

Parameter

Parameter Description
file The file to read.

Exception

Parameter Description
IOException If the file is not readable.

Return

The contents of the file.

Declaration

public static String readFile(File file) throws IOException 

Method Source Code

//package com.java2s;
/**/*from   w  w w .j a  va  2s  .c om*/
 * @author Charles McGarvey
 * The TopCoder Arena editor plug-in providing support for Vim.
 * 
 * Distributable under the terms and conditions of the 2-clause BSD license;
 * see the file COPYING for a complete text of the license.
 */

import java.io.*;

public class Main {
    /**
     * Simply read a file's contents into a string object.
     * @param file The file to read.
     * @return The contents of the file.
     * @throws IOException If the file is not readable.
     */
    public static String readFile(File file) throws IOException {
        StringBuilder text = new StringBuilder();

        BufferedReader reader = new BufferedReader(new FileReader(file.getPath()));
        try {
            String line = null;

            while ((line = reader.readLine()) != null) {
                text.append(line + System.getProperty("line.separator"));
            }
        } finally {
            reader.close();
        }

        return text.toString();
    }
}

Related

  1. readFile(File file)
  2. readFile(File file)
  3. readFile(File file)
  4. readFile(File file)
  5. readFile(File file)
  6. readFile(File file)
  7. readFile(File file)
  8. readFile(File file)
  9. readFile(File file)