java判断文本文件编码格式

上篇文章需要读取当前java或者配置文件的编码格式,这里主要支持UTF-8、GBK、UTF-16、Unicode等

1/** 2 * 判断文件的编码格式 3 * @param fileName :file 4 * @return 文件编码格式 5 * @throws Exception 6 */ 7 public static String codeString(File fileName) throws Exception{ 8 BufferedInputStream bin = new BufferedInputStream( 9 new FileInputStream(fileName)); 10 int p = (bin.read() << 8) + bin.read(); 11 String code = null; 12 13 switch (p) { 14 case 0xefbb: 15 code = "UTF-8"; 16 break; 17 case 0xfffe: 18 code = "Unicode"; 19 break; 20 case 0xfeff: 21 code = "UTF-16BE"; 22 break; 23 default: 24 code = "GBK"; 25 } 26 IOUtils.closeQuietly(bin); 27 return code; 28 }

上面这段代码只能判断带bom的文本,如果非bom文本,还有两种方式 1、 轮询常用的编码,知道找到匹配的,如下面一段测试代码

1/* 2 * Copyright 2010 Georgios Migdos <cyberpython@gmail.com>. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 * under the License. 16 */ 17 18import java.io.BufferedInputStream; 19import java.io.File; 20import java.io.FileInputStream; 21import java.io.FileNotFoundException; 22import java.io.IOException; 23import java.io.InputStreamReader; 24import java.nio.ByteBuffer; 25import java.nio.charset.CharacterCodingException; 26import java.nio.charset.Charset; 27import java.nio.charset.CharsetDecoder; 28 29/** 30 * 31 * @author Georgios Migdos <cyberpython@gmail.com> 32 */ 33public class CharsetDetector { 34 35 public Charset detectCharset(File f, String[] charsets) { 36 37 Charset charset = null; 38 39 for (String charsetName : charsets) { 40 charset = detectCharset(f, Charset.forName(charsetName)); 41 if (charset != null) { 42 break; 43 } 44 } 45 46 return charset; 47 } 48 49 private Charset detectCharset(File f, Charset charset) { 50 try { 51 BufferedInputStream input = new BufferedInputStream(new FileInputStream(f)); 52 53 CharsetDecoder decoder = charset.newDecoder(); 54 decoder.reset(); 55 56 byte[] buffer = new byte[512]; 57 boolean identified = false; 58 while ((input.read(buffer) != -1) && (!identified)) { 59 identified = identify(buffer, decoder); 60 } 61 62 input.close(); 63 64 if (identified) { 65 return charset; 66 } else { 67 return null; 68 } 69 70 } catch (Exception e) { 71 return null; 72 } 73 } 74 75 private boolean identify(byte[] bytes, CharsetDecoder decoder) { 76 try { 77 decoder.decode(ByteBuffer.wrap(bytes)); 78 } catch (CharacterCodingException e) { 79 return false; 80 } 81 return true; 82 } 83 84 public static void main(String[] args) { 85 File f = new File("example.txt"); 86 87 String[] charsetsToBeTested = {"UTF-8", "windows-1253", "ISO-8859-7"}; 88 89 CharsetDetector cd = new CharsetDetector(); 90 Charset charset = cd.detectCharset(f, charsetsToBeTested); 91 92 if (charset != null) { 93 try { 94 InputStreamReader reader = new InputStreamReader(new FileInputStream(f), charset); 95 int c = 0; 96 while ((c = reader.read()) != -1) { 97 System.out.print((char)c); 98 } 99 reader.close(); 100 } catch (FileNotFoundException fnfe) { 101 fnfe.printStackTrace(); 102 }catch(IOException ioe){ 103 ioe.printStackTrace(); 104 } 105 106 }else{ 107 System.out.println("Unrecognized charset."); 108 } 109 } 110} 111

2、 使用谷歌依赖库来进行判断 https://code.google.com/archive/p/juniversalchardet/

点赞
收藏

评论区

加载中...

相关推荐

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

java的char类型真的可以存汉字么?

今天偶尔看到一句话:ANSI编码表示英文字符时用一个字节,表示中文用两个字节,而unicode不管表示英文字符还是中文都是用两个字节来表示。我突然间对自己之前对java变量以Unicode编码存储产生了疑问。到底是以Unicode编码存储的还是和源文件使用的编码格式相同呢?联想到之前的一个问题java的char类型是否可以存储汉字,这个问题是

皕杰报表在传参过程中乱码

当web项目的编码格式为UTF8的,在tomcat上集成部署,传参时中文乱码时,需要修改下列编码格式。​​解决方案:1、更改D:/Tomcat/conf/server.xml,指定浏览器的编码格式为“UTF8”:2、更改web项目WEBINF\resources下的Congfig.xml里的            UTF83、更改jsp中编码格式为utf8​

Jenkins maven 构建乱码,修改file.encoding系统变量编码为UTF

一切都是windows的控制台默认编码GBK问题情景:使用jenkins构建,console输出的中文乱码。代码编码格式是utf8,因为Jenkins会默认读取当前系统的编码格式,导致构建日志乱码和selenium自动化测试输入的中文乱码。控制台输出乱码!(https://oscimg.oschina.net/oscnet/4

Eclipse为不同的文件类型设置编码格式和编辑器

不知道大家遇到项目中编码格式不统一的情况没有,哈哈,我们就是,比如java的编码格式是GBK,html等编码是UTF8,这样会导致很多问题,比如提交了一个UTF8的java文件到SVN,会导致后端编译错误。如果是项目建立的时候,大家一定要注意保持编码格式的统一呀,最好用UTF8。我记录一下,怎么在Eclipse中为不同文本设置不同编码格式和编辑器。

Java 使用 ResourceBundle 类读取 properties 文件中文乱码的解决方案

Java使用java.util.ResourceBundle类的方式来读取properties文件时不支持中文,要想支持中文必须将文件设置为ISO88591编码格式,这对于开发工具默认为UTF8来说很不友好,而且就算用ISO88591编码,当其他人将这个项目导入开发工具时很容易出现这个properties文件中的内容有乱码(前提是该文件中包含中文)