Java InputStreamReader Read readFile(File file)

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

Description

read File

License

Apache License

Declaration

public static String readFile(File file)
            throws UnsupportedEncodingException, FileNotFoundException, IOException 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

import java.io.IOException;
import java.io.InputStreamReader;

import java.io.Reader;
import java.io.UnsupportedEncodingException;

public class Main {
    private static final int BUFFER = 2048;

    public static String readFile(File file)
            throws UnsupportedEncodingException, FileNotFoundException, IOException {
        final char[] buffer = new char[BUFFER];
        final StringBuilder out = new StringBuilder();
        try (final Reader in = new InputStreamReader(new FileInputStream(file), "UTF-8")) {
            return readFromStream(buffer, out, in);
        }/*from   ww w.j  a va 2  s .  c  o  m*/
    }

    public static String readFromStream(final char[] buffer, final StringBuilder out, final Reader in)
            throws IOException {
        while (true) {
            final int read = in.read(buffer, 0, BUFFER);
            if (read <= 0) {
                break;
            }
            out.append(buffer, 0, read);
        }
        return out.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, String default_encoding)
  8. readFile(File file, String encoding)
  9. readFile(File file, String encoding)