Java BufferedReader Read readFile(File file)

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

Description

Read a file fully into a string buffer.

License

Open Source License

Parameter

Parameter Description
file a parameter

Exception

Parameter Description
IOException an exception

Declaration

public static StringBuffer readFile(File file) throws IOException 

Method Source Code

//package com.java2s;
/**//from   www .  ja  v a 2 s .c  o  m
(C) Copyright 1998-2007 Hewlett-Packard Development Company, LP
    
This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
    
You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    
For more information: www.smartfrog.org
*/

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

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

public class Main {
    /**
     * Read a file fully into a string buffer.
     * @param file
     * @return
     * @throws IOException
     */
    public static StringBuffer readFile(File file) throws IOException {
        StringBuffer buf = new StringBuffer();

        if (null == file) {
            return null;
        }

        if (!file.exists() || !file.canRead()) {
            throw new IOException("Error! File is not accessible : " + file);
        }

        BufferedReader reader = new BufferedReader(new FileReader(file));
        String str = null;
        while (null != (str = reader.readLine())) {
            buf.append(str + "\n");
        }
        reader.close();
        return buf;
    }
}

Related

  1. readFile(File f, String newLineChar)
  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)