Java File Read by Charset getFileContents(File file, String charset)

Here you can find the source of getFileContents(File file, String charset)

Description

Reads the contents of a text file.

License

Open Source License

Parameter

Parameter Description
file the file to read
charset the character encoding of the file

Exception

Parameter Description
IOException if there's a problem reading the file

Return

the file contents

Declaration

public static String getFileContents(File file, String charset) throws IOException 

Method Source Code


//package com.java2s;
import java.io.ByteArrayOutputStream;
import java.io.Closeable;
import java.io.File;
import java.io.FileInputStream;

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

import java.nio.charset.Charset;

public class Main {
    /**/* w  w w  .  j a v  a2  s . c  o m*/
     * Reads the contents of a text file.
     * @param file the file to read
     * @return the file contents
     * @throws IOException if there's a problem reading the file
     */
    public static String getFileContents(File file) throws IOException {
        return getFileContents(file, Charset.defaultCharset().name());
    }

    /**
     * Reads the contents of a text file.
     * @param file the file to read
     * @param charset the character encoding of the file
     * @return the file contents
     * @throws IOException if there's a problem reading the file
     */
    public static String getFileContents(File file, String charset) throws IOException {
        byte[] bytes = toByteArray(new FileInputStream(file), true);
        return new String(bytes, charset);
    }

    /**
     * Reads all the bytes from an input stream.
     * @param in the input stream
     * @return the bytes
     * @throws IOException if there's a problem reading from the input stream
     */
    public static byte[] toByteArray(InputStream in) throws IOException {
        return toByteArray(in, false);
    }

    /**
     * Reads all the bytes from an input stream.
     * @param in the input stream
     * @param close true to close the input stream when done, false not to
     * @return the bytes
     * @throws IOException if there's a problem reading from the input stream
     */
    public static byte[] toByteArray(InputStream in, boolean close) throws IOException {
        try {
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            byte[] buffer = new byte[4096];
            int read;
            while ((read = in.read(buffer)) != -1) {
                out.write(buffer, 0, read);
            }
            return out.toByteArray();
        } finally {
            if (close) {
                closeQuietly(in);
            }
        }
    }

    /**
     * Closes a closeable resource, catching its {@link IOException}.
     * @param closeable the resource to close (can be null)
     */
    public static void closeQuietly(Closeable closeable) {
        try {
            if (closeable != null) {
                closeable.close();
            }
        } catch (IOException e) {
            //ignore
        }
    }
}

Related

  1. findPattern(File file, Charset charset, String patternString)
  2. fixture(String filename, Charset charset)
  3. getContent(File file, String charsetName)
  4. getEOL(File file, Charset charset)
  5. getFileContent(File file, String charsetName)
  6. getFileEncodingCharset()
  7. getFileText(File file, Charset charset)
  8. getNumberOfNonEmptyLines(File file, Charset charset)
  9. getPatchFileCharset()