Java BufferedReader Read readFile(File file)

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

Description

read File

License

Open Source License

Declaration

public static String readFile(File file) throws IOException 

Method Source Code

//package com.java2s;
/******************************************************************************
 * Copyright (c) 2014 Masatomi KINO and others. 
 * 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
 * Contributors://from   w  w w . ja  v  a  2s . co m
 *      Masatomi KINO - initial API and implementation
 * $Id$
 ******************************************************************************/

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.StringWriter;

public class Main {
    public static String readFile(File file) throws IOException {
        StringWriter out = new StringWriter();
        FileInputStream in = null;
        try {
            in = new FileInputStream(file);
            int i;
            while ((i = in.read()) != -1) {
                out.write(i);
            }
        } finally {
            in.close();
        }
        return out.toString();

        // StringBuffer buffer = new StringBuffer();
        // BufferedReader reader = null;
        // try {
        // reader = new BufferedReader(new FileReader(file));
        // String line;
        // while ((line = reader.readLine()) != null) {
        // buffer.append(line);
        // }
        // } finally {
        // if (reader != null) {
        // try {
        // reader.close();
        // } catch (IOException e) {
        // e.printStackTrace();
        // }
        // reader = null;
        // }
        // }
        // String string = new String(buffer);
        // return string;
    }
}

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)