Java BufferedReader Read readFile(String filePath)

Here you can find the source of readFile(String filePath)

Description

Read text from a file in one line.

License

Open Source License

Parameter

Parameter Description
filePath the path to the file to read.

Exception

Parameter Description
IOException an exception

Return

the read string.

Declaration

public static String readFile(String filePath) throws IOException 

Method Source Code


//package com.java2s;
/*/*from   w w w.j a va2 s.  c  om*/
 * JGrass - Free Open Source Java GIS http://www.jgrass.org 
 * (C) HydroloGIS - www.hydrologis.com 
 * 
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Library General Public License as published by the Free
 * Software Foundation; either version 2 of the License, or (at your option) any
 * later version.
 * 
 * This library 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 Library General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU Library General Public License
 * along with this library; if not, write to the Free Foundation, Inc., 59
 * Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */

import java.io.*;

public class Main {
    /**
     * Read text from a file in one line.
     *
     * @param filePath the path to the file to read.
     *
     * @return the read string.
     *
     * @throws IOException
     */
    public static String readFile(String filePath) throws IOException {
        return readFile(new File(filePath));
    }

    /**
     * Read text from a file in one line.
     *
     * @param file the file to read.
     *
     * @return the read string.
     *
     * @throws IOException
     */
    public static String readFile(File file) throws IOException {
        try (BufferedReader br = new BufferedReader(new FileReader(file))) {

            StringBuilder sb = new StringBuilder(200);
            String line;
            while ((line = br.readLine()) != null) {
                sb.append(line);
                sb.append("\n"); //$NON-NLS-1$
            }
            return sb.toString();
        }
    }
}

Related

  1. readFile(String fileNm)
  2. readFile(String filePath)
  3. readFile(String filePath)
  4. readFile(String filePath)
  5. readFile(String filePath)
  6. readFile(String filePath)
  7. readFile(String filePath, Boolean replaceNewLineWithBr)
  8. readFile(String fullPath)
  9. readFile(String fullPath)