Here you can find the source of readFile(final String filePath)
Parameter | Description |
---|---|
filePath | the path to the file to read. |
Parameter | Description |
---|---|
IOException | an exception |
public static String readFile(final String filePath) throws IOException
//package com.java2s; /*//from w ww.j av a 2 s .c o m * Debrief - the Open Source Maritime Analysis Application * http://debrief.info * * (C) 2000-2014, PlanetMayo Ltd * * This library is free software; you can redistribute it and/or * modify it under the terms of the Eclipse Public License v1.0 * (http://www.eclipse.org/legal/epl-v10.html) * * 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. */ import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.IOException; 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(final 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(final File file) throws IOException { BufferedReader br = null; try { br = new BufferedReader(new FileReader(file)); final StringBuilder sb = new StringBuilder(200); String line = null; while ((line = br.readLine()) != null) { sb.append(line); sb.append("\n"); //$NON-NLS-1$ } return sb.toString(); } finally { if (br != null) br.close(); } } }