Java BufferedReader Read readFile(File file)

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

Description

readFile read a file given File object and return the contents as a String

License

Open Source License

Parameter

Parameter Description
file the file handle to read

Exception

Parameter Description
IOException propagated

Return

the contents of the file

Declaration

public static String readFile(File file) throws java.io.IOException 

Method Source Code


//package com.java2s;
/*/* w  w w. j av a 2  s . c  o m*/
 * Copyright (c) 2010. Agwego Enterprises Inc.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 *
 * If you modify this software a credit would be nice
 */

import java.io.*;

public class Main {
    /**
     * readFile read a file given a file name and return the contents as a String
     *
     * @param file_name the file to read
     * @return the contents of file_name
     * @throws IOException propagated
     */
    public static String readFile(final String file_name) throws java.io.IOException {
        return readFile(new File(file_name));
    }

    /**
     * readFile read a file given File object and return the contents as a String
     *
     * @param file the file handle to read
     * @return the contents of the file
     * @throws IOException propagated
     */
    public static String readFile(File file) throws java.io.IOException {
        StringBuffer sb = new StringBuffer();
        BufferedReader br = new BufferedReader(new FileReader(file));

        String str;
        while ((str = br.readLine()) != null)
            sb.append(str);

        br.close();

        return sb.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, boolean useSystemLineSeparator)
  7. readFile(File file, int header)
  8. readFile(File filename)
  9. readFile(File from)