Java BufferedReader Read readFile(final File file)

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

Description

read File

License

Open Source License

Declaration

protected static String readFile(final File file) throws IOException 

Method Source Code


//package com.java2s;
// The MIT License

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

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

import java.io.Reader;

public class Main {
    protected static String readFile(final File file) throws IOException {
        StringBuilder stringBuilder = new StringBuilder();
        FileReader reader = null;
        try {//w w w  .j  a  v  a 2s .  co m
            reader = new FileReader(file);
            BufferedReader bufferedReader = new BufferedReader(reader);
            while (bufferedReader.ready()) {
                stringBuilder.append(bufferedReader.readLine());
                stringBuilder.append(System.getProperty("line.separator"));
            }
            bufferedReader.close();
        } catch (IOException e) {
            throw e; // TODO: remove the IOE from method signature and throw a new
                     // exception extending runtime
        } finally {
            closeQuietly(reader);
        }
        return stringBuilder.toString();
    }

    public static void closeQuietly(Reader reader) {
        if (null != reader) {
            try {
                reader.close();
            } catch (Exception e) {
                // ignore all exceptions
            }
        }
    }

    public static void closeQuietly(InputStream inputStream) {
        if (null != inputStream) {
            try {
                inputStream.close();
            } catch (Exception e) {
                // ignore all exceptions
            }
        }
    }
}

Related

  1. readFile(final File argFile)
  2. readFile(final File file)
  3. readFile(final File file)
  4. readFile(final File file)
  5. readFile(final File file)
  6. readFile(final File paramFile, final boolean paramWhitespaces)
  7. readFile(final String file)
  8. readFile(final String fileName)
  9. readFile(final String filename)