Java Text File Read by Charset read(InputStream in, String charsetName)

Here you can find the source of read(InputStream in, String charsetName)

Description

read

License

Open Source License

Parameter

Parameter Description
in the InputStream to read to completion.
charsetName The optional name of a supported java.nio.charset.Charset </code>charset<code>

Exception

Parameter Description
IOException an exception

Return

the String read from the InputStream.

Declaration

public static String read(InputStream in, String charsetName) throws IOException 

Method Source Code


//package com.java2s;
/*//w  w w.  j av  a2s .  c o m
 * @(#)IOUtils.java
 *
 * $Date: 2015-07-22 23:37:55 -0400 (Wed, 22 Jul 2015) $
 *
 * Copyright (c) 2011 by Jeremy Wood.
 * All rights reserved.
 *
 * The copyright of this software is owned by Jeremy Wood. 
 * You may not use, copy or modify this software, except in  
 * accordance with the license agreement you entered into with  
 * Jeremy Wood. For details see accompanying license terms.
 * 
 * This software is probably, but not necessarily, discussed here:
 * https://javagraphics.java.net/
 * 
 * That site should also contain the most recent official version
 * of this software.  (See the SVN repository for more details.)
 */

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

import java.io.FileInputStream;

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

public class Main {
    /** Read data into the destination array.
     * 
     * @param in the InputStream to read.
     * @param dest the destination to write to
     * @return the number of bytes read (note this will be less than dest.length when the end of the stream is reached).
     * @throws IOException
     */
    public static int read(InputStream in, byte[] dest) throws IOException {
        int length = dest.length;
        int read = 0;
        int k = in.read(dest, read, length - read);
        while (k != -1 && read < dest.length) {
            read += k;
            k = in.read(dest, read, dest.length - read);
        }
        if (k != -1) {
            read += k;
        }
        return read;
    }

    /** @return the contents of a file as a String, or null if
     * the file does not exists.
     * @param file the file to read
     * @throws IOException if an IO problem occurs.
     */
    public static String read(File file) throws IOException {
        if (file == null || (!file.exists()))
            return null;

        try (FileInputStream in = new FileInputStream(file)) {
            return read(in);
        }
    }

    /**
     * 
     * @param in the InputStream to read to completion.
     * 
     * @return the String read from the InputStream.
     * @throws IOException
     */
    public static String read(InputStream in) throws IOException {
        return read(in, (String) null);
    }

    /**
     * 
     * @param in the InputStream to read to completion.
     * 
      * @param  charsetName
      *         The optional name of a supported
      *         {@link java.nio.charset.Charset </code>charset<code>}
      *         
     * @return the String read from the InputStream.
     * @throws IOException
     */
    public static String read(InputStream in, String charsetName) throws IOException {
        try (InputStreamReader inputReader = charsetName == null ? new InputStreamReader(in)
                : new InputStreamReader(in, charsetName)) {
            try (BufferedReader br = new BufferedReader(inputReader)) {
                StringBuffer sb = null;
                String s = br.readLine();
                while (s != null) {
                    if (sb == null) {
                        sb = new StringBuffer();
                        sb.append(s);
                    } else {
                        sb.append("\n");
                        sb.append(s);
                    }
                    s = br.readLine();
                }
                if (sb == null)
                    return "";
                return sb.toString();
            }
        }
    }
}

Related

  1. openNewCompressedInputReader(final InputStream inputStream, final Charset charset)
  2. read(File file, Charset charset)
  3. read(InputStream _is, CharsetDecoder _decoder)
  4. read(InputStream in, Charset charset)
  5. read(InputStream in, Charset cs, Appendable appendable, int capacity)
  6. readAll(InputStream in, Charset charset)
  7. readAll(InputStream inputStream, Charset charset)
  8. readAll(InputStream inputStream, Charset encoding)
  9. readAllLines(File file, Charset cs, String newLineDelimiter)