Java InputStreamReader Read readFile(File file, String default_encoding)

Here you can find the source of readFile(File file, String default_encoding)

Description

read File

License

Apache License

Declaration

public static String readFile(File file, String default_encoding) throws IOException 

Method Source Code


//package com.java2s;
/*//from   w  w w .j  a  va 2 s .  c  o m
 *
 *  Licensed to the Apache Software Foundation (ASF) under one or more
 *  contributor license agreements.  See the NOTICE file distributed with
 *  this work for additional information regarding copyright ownership.
 *  The ASF licenses this file to You 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.
 *
 */

import java.io.*;

public class Main {
    public static String readFile(File file, String default_encoding) throws IOException {
        FileInputStream fileInputStream = new FileInputStream(file);

        try {
            StringBuilder returnVal = new StringBuilder((int) file.length());
            BufferedInputStream in = new BufferedInputStream(fileInputStream);
            in.mark(3);

            Reader reader = new InputStreamReader(in, consumeBOM(in, default_encoding));

            char[] line = new char[2000];
            int count = 0;

            while ((count = reader.read(line, 0, line.length)) >= 0) {
                returnVal.append(line, 0, count);
            }

            return returnVal.toString();
        } finally {
            fileInputStream.close();
        }
    }

    public static String readFile(String filename, String default_encoding) throws IOException {
        return readFile(new File(filename), default_encoding);
    }

    /**
     * @param in input stream
     * @param default_encoding default encoding. null or "" => system default
     * @return file encoding..
     * @throws IOException
     */
    public static final String consumeBOM(InputStream in, String default_encoding) throws IOException {
        return consumeBOM(in, default_encoding, false);
    }

    /**
     * @param in input stream
     * @param default_encoding default encoding. null or "" => system default
     * @param alwaysConsumeBOM If true, then consume the UTF-16 BOM. 
     *                         If false use the previous behavior that consumes
     *                         a UTF-8 BOM but not a UTF-16 BOM.
     *                         This flag is useful when reading a file into
     *                         a string that is then passed to a parser. The parser may 
     *                         not know to strip out the BOM. 
     * @return file encoding..
     * @throws IOException
     */
    public static final String consumeBOM(InputStream in, String default_encoding, boolean alwaysConsumeBOM)
            throws IOException {
        in.mark(3);
        // Determine file encoding...
        // ASCII - no header (use the supplied encoding)
        // UTF8  - EF BB BF
        // UTF16 - FF FE or FE FF (decoder chooses endian-ness)
        if (in.read() == 0xef && in.read() == 0xbb && in.read() == 0xbf) {
            // UTF-8 reader does not consume BOM, so do not reset
            if (System.getProperty("flex.platform.CLR") != null) {
                return "UTF8";
            } else {
                return "UTF-8";
            }
        } else {
            in.reset();
            int b0 = in.read();
            int b1 = in.read();
            if (b0 == 0xff && b1 == 0xfe || b0 == 0xfe && b1 == 0xff) {
                // If we don't consume the BOM is its assumed a
                // UTF-16 reader will consume BOM
                if (!alwaysConsumeBOM) {
                    in.reset();
                }

                if (System.getProperty("flex.platform.CLR") != null) {
                    return "UTF16";
                } else {
                    return "UTF-16";
                }
            } else {
                // no BOM found
                in.reset();
                if (default_encoding != null && default_encoding.length() != 0) {
                    return default_encoding;
                } else {
                    return System.getProperty("file.encoding");
                }
            }
        }
    }
}

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, String encoding)
  7. readFile(File file, String encoding)
  8. readFile(File file, String encoding)
  9. readFile(File file, String encoding)