Java Text File Read by Charset readFile(File f, Charset chst)

Here you can find the source of readFile(File f, Charset chst)

Description

read File

License

Apache License

Declaration

public static String readFile(File f, Charset chst) throws IOException 

Method Source Code

//package com.java2s;
/**//from   w ww . j a v a  2  s.  c o m
 * Copyright 2014-2017 Functional Genomics Development Team, European Bioinformatics Institute
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
 * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations under the License.
 *
 * @author Mikhail Gostev <gostev@gmail.com>
 **/

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

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

import java.io.Reader;
import java.nio.charset.Charset;

public class Main {
    public static String readFile(File f) throws IOException {
        return readFile(f, Charset.defaultCharset());
    }

    public static String readFile(File f, Charset chst) throws IOException {
        FileInputStream fis = new FileInputStream(f);

        return readStream(fis, chst, f.length());
    }

    public static String readStream(Reader fis) throws IOException {
        StringBuilder sb = new StringBuilder();

        char[] buff = new char[64 * 1024];

        int n;
        while ((n = fis.read(buff)) > 0) {
            sb.append(buff, 0, n);
        }

        return sb.toString();
    }

    public static String readStream(InputStream fis, Charset chst, long sz) throws IOException {
        ByteArrayOutputStream baos = null;

        if (sz > 0 && sz < Integer.MAX_VALUE) {
            baos = new ByteArrayOutputStream((int) sz);
        } else {
            baos = new ByteArrayOutputStream();
        }

        try {
            byte[] buff = new byte[64 * 1024];
            int n;

            while ((n = fis.read(buff)) != -1) {
                baos.write(buff, 0, n);
            }

            buff = baos.toByteArray();

            int offs = 0;
            if ((buff[0] == (byte) 0xEF && buff[1] == (byte) 0xBB && buff[2] == (byte) 0xBF)
                    && chst.displayName().equalsIgnoreCase("UTF-8")) {
                offs = 3;
            }

            return new String(buff, offs, buff.length - offs, chst);
        } finally {
            if (fis != null) {
                try {
                    fis.close();
                } catch (Exception e) {
                }
            }

            baos.close();
        }

    }
}

Related

  1. readCharsWithEncoding(File file, Charset charset)
  2. reader(Class baseClass, String resourceName, Charset charset)
  3. reader(InputStream in, String charsetName)
  4. readerBuffered(final File file, final Charset charset)
  5. readFile(Bundle bundle, String path, Charset cs)
  6. readFile(File file, Charset charset)
  7. readFile(File file, Charset charset)
  8. readFile(File file, Charset cs)
  9. readFile(File file, String charsetName)