Java Text File Read by Charset read(InputStream in, Charset cs, Appendable appendable, int capacity)

Here you can find the source of read(InputStream in, Charset cs, Appendable appendable, int capacity)

Description

Read the specified text input stream and fill the content to the specified appendable.

License

Open Source License

Parameter

Parameter Description
in the input stream to read
cs the character set of the input stream, <code>null</code> if use environment character set
appendable the appendable to append
capacity the capacity of the buffer

Exception

Parameter Description
IOException if an IO error occurs

Declaration

public static void read(InputStream in, Charset cs, Appendable appendable, int capacity) throws IOException 

Method Source Code

//package com.java2s;

import java.io.File;

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

import java.nio.CharBuffer;
import java.nio.charset.Charset;

public class Main {
    /**//w  ww  .  java  2s. com
     * Read the specified text input stream and fill the content to the
     * specified appendable.
     * <p>
     * The input stream will be read to the end and will be closed after read.
     * </p>
     * 
     * @param in
     *            the input stream to read
     * @param cs
     *            the character set of the input stream, <code>null</code> if
     *            use environment character set
     * @param appendable
     *            the appendable to append
     * @param capacity
     *            the capacity of the buffer
     * @throws IOException
     *             if an IO error occurs
     */
    public static void read(InputStream in, Charset cs, Appendable appendable, int capacity) throws IOException {
        InputStreamReader isr = new InputStreamReader(in, cs == null ? Charset.defaultCharset() : cs);
        CharBuffer cbuf = CharBuffer.allocate(capacity);
        int r = 0;
        while (r > -1) {
            cbuf.clear();
            appendable.append(cbuf, 0, r);
            r = isr.read(cbuf);
        }
        isr.close();
    }

    /**
     * Read the specified file and fill the content to the specified appendable.
     * 
     * @param file
     *            the file to read
     * @param cs
     *            the character set of the input stream, <code>null</code> if
     *            use environment character set
     * @param appendable
     *            the appendable to append
     * @param capacity
     *            the capacity of the buffer
     * @throws IOException
     *             if an IO error occurs
     */
    public static void read(File file, Charset cs, Appendable appendable, int capacity) throws IOException {
        read(new java.io.FileInputStream(file), cs, appendable, capacity);
    }

    /**
     * Read the specified file and fill the content to the specified appendable.
     * 
     * @param filename
     *            the name of the file to read
     * @param cs
     *            the character set of the input stream, <code>null</code> if
     *            use environment character set
     * @param appendable
     *            the appendable to append
     * @param capacity
     *            the capacity of the buffer
     * @throws IOException
     *             if an IO error occurs
     */
    public static void read(String filename, Charset cs, Appendable appendable, int capacity) throws IOException {
        read(new java.io.FileInputStream(filename), cs, appendable, capacity);
    }

    /**
     * Read the specified file and returns the file content.
     * 
     * @param filename
     *            the name of the file to read
     * @param cs
     *            the character set of the input stream, <code>null</code> if
     *            use environment character set
     * @param appendable
     *            the appendable to append
     * @param capacity
     *            the capacity of the buffer
     * @return the file content
     * @throws IOException
     *             if an IO error occurs
     */
    public static String read(String filename, Charset cs, int capacity) throws IOException {
        StringBuilder sb = new StringBuilder();
        read(filename, cs, sb, capacity);
        return sb.toString();
    }

    /**
     * Read the specified file and returns the file content.
     * 
     * @param file
     *            the file to read
     * @param cs
     *            the character set of the input stream, <code>null</code> if
     *            use environment character set
     * @param capacity
     *            the capacity of the buffer
     * @return the file content
     * @throws IOException
     *             if an IO error occurs
     */
    public static String read(File file, Charset cs, int capacity) throws IOException {
        StringBuilder sb = new StringBuilder();
        read(file, cs, sb, capacity);
        return sb.toString();
    }

    /**
     * Read the specified file and returns the file content with default
     * character set.
     * 
     * @param file
     *            the specified file
     * @return the file content
     * @throws IOException
     *             if an IO error occurs
     */
    public static String read(File file) throws IOException {
        return read(file, Charset.defaultCharset(), 8192);
    }

    /**
     * Read the specified file and returns the file content with default
     * character set.
     * 
     * @param filename
     *            the filename of the specified file
     * @return the file content
     * @throws IOException
     *             if an IO error occurs
     */
    public static String read(String filename) throws IOException {
        return read(new File(filename), Charset.defaultCharset(), 8192);
    }

    /**
     * Read the specified file and fill the content to the specified appendable
     * with default character set.
     * 
     * @param filename
     *            the name of the file to read
     * @param appendable
     *            the appendable to append
     * @throws IOException
     *             if an IO error occurs
     */
    public static void read(String filename, Appendable appendable) throws IOException {
        read(filename, Charset.defaultCharset(), appendable, 8192);
    }
}

Related

  1. newUTF8CharSetReader(String filename)
  2. openNewCompressedInputReader(final InputStream inputStream, final Charset charset)
  3. read(File file, Charset charset)
  4. read(InputStream _is, CharsetDecoder _decoder)
  5. read(InputStream in, Charset charset)
  6. read(InputStream in, String charsetName)
  7. readAll(InputStream in, Charset charset)
  8. readAll(InputStream inputStream, Charset charset)
  9. readAll(InputStream inputStream, Charset encoding)