Java BufferedReader Read readFile(File file)

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

Description

Read string from a file

License

Open Source License

Parameter

Parameter Description
file to be read

Exception

Parameter Description
IOException an exception

Return

string containing file content or empty string on file not found

Declaration

public static String readFile(File file) throws IOException 

Method Source Code

//package com.java2s;
/*//from  w w  w .j  a v  a 2s . c  o  m
 *  This file is part of easyFPGA.
 *  Copyright 2013-2015 os-cillation GmbH
 *
 *  easyFPGA is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  easyFPGA 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 General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with easyFPGA.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

import java.io.BufferedReader;
import java.io.File;

import java.io.FileNotFoundException;

import java.io.FileReader;
import java.io.IOException;

public class Main {
    /** system-dependent line separator */
    public static final String LS = System.getProperty("line.separator");

    /**
     * Read string from a file
     *
     * @param file to be read
     * @return string containing file content or empty string on file not found
     * @throws IOException
     */
    public static String readFile(File file) throws IOException {
        BufferedReader reader;
        try {
            reader = new BufferedReader(new FileReader(file));
        } catch (FileNotFoundException e) {
            return "";
        }
        StringBuilder sb = new StringBuilder();
        String line = reader.readLine();

        while (line != null) {
            sb.append(line);
            sb.append(LS);
            line = reader.readLine();
        }
        String fileString = sb.toString();
        reader.close();
        return fileString;
    }
}

Related

  1. readFile(File f)
  2. readFile(File f)
  3. ReadFile(File f, boolean keep_newline)
  4. readFile(File f, boolean preserveLineBreaks)
  5. readFile(File f, String newLineChar)
  6. readFile(File file)
  7. readFile(File file)
  8. readFile(File file)
  9. readFile(File file)