Here you can find the source of readFile(String filePath)
Parameter | Description |
---|---|
filePath | the path to the file to read. |
Parameter | Description |
---|---|
IOException | an exception |
public static String readFile(String filePath) throws IOException
//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(); } } }