Java FileReader 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) 

Method Source Code

//package com.java2s;
/*//from ww w  .jav  a 2  s  .  c o  m
 * Copyright (c) 2014 Eike Stepper (Berlin, Germany) 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:
 *    Eike Stepper - initial API and implementation
 */

import java.io.Closeable;
import java.io.File;

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

import java.io.Reader;

public class Main {
    public static String readFile(File file) {
        char[] buffer = new char[(int) file.length()];
        Reader reader = null;

        try {
            reader = new FileReader(file);
            reader.read(buffer);
            return new String(buffer);
        } catch (IOException ex) {
            throw new RuntimeException(ex);
        } finally {
            close(reader);
        }
    }

    public static void close(Closeable closeable) {
        if (closeable != null) {
            try {
                closeable.close();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    }
}

Related

  1. readFile(File file)
  2. readFile(File file)
  3. readFile(File file)
  4. readFile(File file)
  5. readFile(File file)
  6. readFile(File iFile)
  7. readFile(final String fileName)
  8. readFile(Reader reader)
  9. readFile(String file)