文本文件编码识别
作用:识别不同编码格式的文本文件
依据:根据文本文件开头几个字节来判断文件的编码格式(txt文件的开头会多出几个字节,分别是FF、FE(Unicode),
FE、FF(Unicode big endian),EF、BB、BF(UTF-8))
工具类:FileUtil
Code
1package cn.com.liuwt.file;
2import java.io.InputStreamReader;
3import java.io.FileInputStream;
4
5public class FileUtil {
6 public FileUtil() {
7 }
8 ///根据文件路径返回编码格式
9 public static String getFileEncode(String path)
10
11 throws java.io.IOException
12 {
13 java.io.RandomAccessFile raf=null;
14 String encode="";
15 try
16 {
17 raf=new java.io.RandomAccessFile(path,"r");
18 raf.seek(0);
19 int flag1=0;
20 int flag2=0;
21 int flag3=0;
22 if(raf.length()>=2)
23 {
24 flag1 = raf.readUnsignedByte();
25 flag2 = raf.readUnsignedByte();
26 }
27 if(raf.length()>=3)
28 {
29 flag3 = raf.readUnsignedByte();
30 }
31 encode = getEncode(flag1,flag2,flag3);
32 }
33 finally
34 {
35 if(raf!=null)
36 raf.close();
37 return encode;
38 }
39
40 }
41 private static String getEncode(int flag1,int flag2,int flag3)
42 {
43 String encode="";
44// txt文件的开头会多出几个字节,分别是FF、FE(Unicode),
45
46//FE、FF(Unicode big endian),EF、BB、BF(UTF-8)
47 if(flag1==255&&flag2==254)
48 {
49 encode="Unicode";
50 }
51 else if(flag1==254&&flag2==255)
52 {
53 encode="UTF-16";
54 }
55 else if(flag1==239&&flag2==187&&flag3==191)
56 {
57 encode="UTF-8";
58 }
59
60 return encode;
61 }
62 ///根据文件路径直接返回文件流
63 public static InputStreamReader getInputStreamReader(String path)
64
65 throws java.io.IOException
66 {
67 java.io.InputStreamReader isr=null;
68 String encode=getFileEncode(path);
69 if(encode.equals(""))
70 {
71 isr = new java.io.InputStreamReader(
72
73 new java.io.FileInputStream(path));
74 }
75 else
76 {
77 isr=new java.io.InputStreamReader(
78
79 new java.io.FileInputStream(path),encode);
80 }
81
82 return isr;
83 }
84 public static void main(String[] args) throws Exception {
85 FileUtil fileutil = new FileUtil();
86 getFileEncode("D:\\tztest.axl");
87 }
88}
89
90
91
1package cn.com.liuwt.file;
2import java.io.InputStreamReader;
3import java.io.FileInputStream;
4
5public class FileUtil {
6 public FileUtil() {
7 }
8 ///根据文件路径返回编码格式
9 public static String getFileEncode(String path)
10
11 throws java.io.IOException
12 {
13 java.io.RandomAccessFile raf=null;
14 String encode="";
15 try
16 {
17 raf=new java.io.RandomAccessFile(path,"r");
18 raf.seek(0);
19 int flag1=0;
20 int flag2=0;
21 int flag3=0;
22 if(raf.length()>=2)
23 {
24 flag1 = raf.readUnsignedByte();
25 flag2 = raf.readUnsignedByte();
26 }
27 if(raf.length()>=3)
28 {
29 flag3 = raf.readUnsignedByte();
30 }
31 encode = getEncode(flag1,flag2,flag3);
32 }
33 finally
34 {
35 if(raf!=null)
36 raf.close();
37 return encode;
38 }
39
40 }
41 private static String getEncode(int flag1,int flag2,int flag3)
42 {
43 String encode="";
44// txt文件的开头会多出几个字节,分别是FF、FE(Unicode),
45
46//FE、FF(Unicode big endian),EF、BB、BF(UTF-8)
47 if(flag1==255&&flag2==254)
48 {
49 encode="Unicode";
50 }
51 else if(flag1==254&&flag2==255)
52 {
53 encode="UTF-16";
54 }
55 else if(flag1==239&&flag2==187&&flag3==191)
56 {
57 encode="UTF-8";
58 }
59
60 return encode;
61 }
62 ///根据文件路径直接返回文件流
63 public static InputStreamReader getInputStreamReader(String path)
64
65 throws java.io.IOException
66 {
67 java.io.InputStreamReader isr=null;
68 String encode=getFileEncode(path);
69 if(encode.equals(""))
70 {
71 isr = new java.io.InputStreamReader(
72
73 new java.io.FileInputStream(path));
74 }
75 else
76 {
77 isr=new java.io.InputStreamReader(
78
79 new java.io.FileInputStream(path),encode);
80 }
81
82 return isr;
83 }
84 public static void main(String[] args) throws Exception {
85 FileUtil fileutil = new FileUtil();
86 getFileEncode("D:\\tztest.axl");
87 }
88}
89
90
91
简单示例:
Code
1package cn.com.liuwt.file;
2import java.io.*;
3public class FileOper {
4 public FileOper() {
5 }
6 public static void main(String[] args)
7
8 throws java.io.IOException
9 {
10 java.io.InputStreamReader isr=null;
11 java.io.BufferedReader br=null;
12 try
13 {
14 String path="D:\\tt.txt";
15 isr=FileUtil.getInputStreamReader(path);
16 br=new BufferedReader(isr);
17 String str=null;
18 while((str=br.readLine())!=null)
19 {
20 System.out.println(str);
21 }
22 }
23 finally
24 {
25 if(isr!=null)
26 isr.close();
27 if(br!=null)
28 br.close();
29 }
30 }
31}
32
33
1package cn.com.liuwt.file;
2import java.io.*;
3public class FileOper {
4 public FileOper() {
5 }
6 public static void main(String[] args)
7
8 throws java.io.IOException
9 {
10 java.io.InputStreamReader isr=null;
11 java.io.BufferedReader br=null;
12 try
13 {
14 String path="D:\\tt.txt";
15 isr=FileUtil.getInputStreamReader(path);
16 br=new BufferedReader(isr);
17 String str=null;
18 while((str=br.readLine())!=null)
19 {
20 System.out.println(str);
21 }
22 }
23 finally
24 {
25 if(isr!=null)
26 isr.close();
27 if(br!=null)
28 br.close();
29 }
30 }
31}
32
33