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 IOException 

Method Source Code

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

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

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

import java.io.Reader;
import java.io.StringWriter;
import java.io.UnsupportedEncodingException;
import java.io.Writer;

public class Main {
    public static String readFile(File file) throws IOException {
        Reader in = new FileReader(file);
        StringWriter out = new StringWriter();
        copy(in, out);/* w  ww. j  ava  2  s . com*/
        return out.toString();
    }

    public static String readFile(File file, String encoding)
            throws IOException {
        InputStream inputStream = new FileInputStream(file);
        return toString(encoding, inputStream);
    }

    public static void copy(Reader reader, Writer writer)
            throws IOException {
        char[] buf = new char[8192];
        int n = 0;
        while ((n = reader.read(buf)) != -1) {
            writer.write(buf, 0, n);
        }
    }

    public static void copy(InputStream in, OutputStream out)
            throws IOException {
        byte[] buf = new byte[8192];
        int n = 0;
        while ((n = in.read(buf)) != -1) {
            out.write(buf, 0, n);
        }
    }

    public static String toString(InputStream inputStream)
            throws UnsupportedEncodingException, IOException {
        Reader reader = new InputStreamReader(inputStream);
        StringWriter writer = new StringWriter();
        copy(reader, writer);
        return writer.toString();
    }

    public static String toString(String encoding, InputStream inputStream)
            throws UnsupportedEncodingException, IOException {
        Reader reader = new InputStreamReader(inputStream, encoding);
        StringWriter writer = new StringWriter();
        copy(reader, writer);
        return writer.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)