Java BufferedReader Read readFile(final String filePath)

Here you can find the source of readFile(final 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(final String filePath) throws IOException 

Method Source Code


//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();
        }
    }
}

Related

  1. readFile(final File paramFile, final boolean paramWhitespaces)
  2. readFile(final String file)
  3. readFile(final String fileName)
  4. readFile(final String filename)
  5. readFile(final String fileName)
  6. readFile(final String name)
  7. readFile(final String path)
  8. readFile(final String pFile)
  9. readFile(List fieldNames, String filename)