Java Is UTF8 isUTF8(File f)

Here you can find the source of isUTF8(File f)

Description

is UTF

License

Apache License

Declaration

public static boolean isUTF8(File f) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.io.*;

public class Main {
    private final static byte[] UTF_8_BOM = { (byte) 0xEF, (byte) 0xBB, (byte) 0xBF };

    public static boolean isUTF8(File f) {
        boolean isUtf8 = false;
        try {/*from ww w.j  a  va 2  s  . co m*/
            FileInputStream fis = new FileInputStream(f);
            byte[] bom = new byte[3];
            fis.read(bom);
            fis.close();
            if (null != bom && bom.length > 2 && bom[0] == UTF_8_BOM[0] && bom[1] == UTF_8_BOM[1]
                    && bom[2] == UTF_8_BOM[2]) {
                isUtf8 = true;
            }
        } catch (Exception e) {
            return false;
        }
        return isUtf8;
    }
}

Related

  1. isUTF8Encoding(String path)
  2. isUTF8StringLengthValid(int min, int max, String string)