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