Java FileReader Read readFile(String filename)

Here you can find the source of readFile(String filename)

Description

Read a file's contents into a string.

License

Open Source License

Parameter

Parameter Description
filename The filename to read from.

Exception

Parameter Description
IOException Thrown when an I/O error occurs.

Return

The contents of filename.

Declaration

public static String readFile(String filename) throws IOException 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2004, 2006 The Board of Trustees of Stanford University.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU General Public License
 * which is available at http://www.gnu.org/licenses/gpl.txt.
 *******************************************************************************/

import java.io.File;

import java.io.FileReader;

import java.io.IOException;

public class Main {
    /**/*from   w  w  w  .jav a  2s  .c om*/
     * Read a file's contents into a string.
     * 
     * (copied from csli.agent.manager.DisplayFile)
     * 
     * @param filename
     *            The filename to read from.
     * @return The contents of <i>filename</i>.
     * 
     * @throws IOException
     *             Thrown when an I/O error occurs.
     */
    public static String readFile(String filename) throws IOException {
        File file = new File(filename);
        StringBuffer buf = new StringBuffer((int) file.length() + 2);
        FileReader in = new FileReader(file);

        int c;
        while ((c = in.read()) != -1) {
            buf.append((char) c);
        }
        in.close();

        return (buf.toString());
    }
}

Related

  1. readFile(String fileName)
  2. readFile(String fileName)
  3. readFile(String fileName)
  4. readFile(String filename)
  5. readFile(String fileName)
  6. readFile(String filename)
  7. readFile(String fileName)
  8. readFile(String fileName, StringBuffer buffer)
  9. readFileAsArray(String filename)