Java BufferedReader Read readFile(File file)

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

Description

Read content from file

License

Open Source License

Parameter

Parameter Description
file file to be read

Exception

Parameter Description
IOException an exception

Return

file contents

Declaration

static public String readFile(File file) throws IOException 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2000, 2011. All rights reserved. This program and the
 * accompanying materials are made available under the terms of the Eclipse
 * Public License v1.0 which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * //from www.  ja v  a  2s  .  c om
 * Contributors: 
 *       Fizer Khan, Yasmine - initial API and implementation
 *******************************************************************************/

import java.io.BufferedReader;

import java.io.File;
import java.io.FileReader;

import java.io.IOException;

public class Main {
    /**
     * Read content from file
     * 
     * @param file
     *            file to be read
     * @return file contents
     * @throws IOException
     */
    static public String readFile(File file) throws IOException {
        StringBuilder contents = new StringBuilder();

        BufferedReader input = new BufferedReader(new FileReader(file));
        try {
            String line = null;
            while ((line = input.readLine()) != null) {
                contents.append(line);
                contents.append(System.getProperty("line.separator"));
            }
        } finally {
            input.close();
        }

        return contents.toString();
    }
}

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)