java中获取TXT文件编码格式

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

 

posted @ 2013-12-19 09:55  NúllPòinterExcêptīon  阅读(1634)  评论(0)    收藏  举报