Java BufferedReader Read readFile(File file)

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

Description

Read text from a file in one line.

License

Open Source License

Parameter

Parameter Description
file the file to read.

Exception

Parameter Description
IOException an exception

Return

the read string.

Declaration

public static String readFile(File file) throws IOException 

Method Source Code


//package com.java2s;
/*/*from   w ww  .  j  a va  2s .c  om*/
 * Stage - Spatial Toolbox And Geoscript Environment 
 * (C) HydroloGIS - www.hydrologis.com 
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * (http://www.eclipse.org/legal/epl-v10.html).
 */

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(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 {
        BufferedReader br = null;
        try {
            br = new BufferedReader(new FileReader(file));
            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(File file)
  2. readFile(File file)
  3. readFile(File file)
  4. readFile(File file)
  5. readFile(File file)
  6. readFile(File file)
  7. readFile(File file)
  8. readFile(File file)
  9. readFile(File file)