1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| public static String getFileCharacterEnding(File file) throws Exception { InputStream inputStream = new FileInputStream(file); byte[] head = new byte[3]; inputStream.read(head); String code = "gbk"; if (head[0] == -1 && head[1] == -2){ code = "UTF-16"; } if (head[0] == -2 && head[1] == -1){ code = "Unicode"; } if (head[0] == -17 && head[1] == -69 && head[2] == -65){ code = "UTF-8"; } return code; }
|