Skip to content

Commit e655026

Browse files
author
fuzhipeng
committed
aaaa
1 parent c4bfbe6 commit e655026

6 files changed

Lines changed: 144 additions & 0 deletions

File tree

‎JavaTest_Zone/.classpath‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
<classpathentry kind="lib" path="lib/xom-1.0d10.jar"/>
1212
<classpathentry kind="lib" path="lib/gson-2.3.1.jar" sourcepath="lib/gson-2.3.1-sources.jar"/>
1313
<classpathentry kind="lib" path="lib/gson-2.3.1-sources.jar"/>
14+
<classpathentry kind="lib" path="lib/41d75b54-56a1-31fc-aa8e-af7c6b8b9ed1.jar"/>
15+
<classpathentry kind="lib" path="lib/53ffafec-781c-3146-b362-86017eb0ad06.jar"/>
16+
<classpathentry kind="lib" path="lib/9333c93a-2e60-364d-ab9f-78ca7ab32ef9.jar"/>
1417
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
1518
<classpathentry combineaccessrules="false" kind="src" path="/JavaLib_Zone"/>
1619
<classpathentry kind="output" path="bin"/>
26 KB
Binary file not shown.
50.2 KB
Binary file not shown.
508 KB
Binary file not shown.
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package 编码工具;
2+
3+
import java.io.File;
4+
import java.io.FilenameFilter;
5+
import java.io.UnsupportedEncodingException;
6+
import java.nio.charset.Charset;
7+
8+
import Java.Zone.IO.IOUtils;
9+
10+
public class CodeChange {
11+
// private static Charset charset = Charset.forName("utf-8");
12+
private static Charset charset = Charset.forName("gbk");
13+
14+
public static void main(String[] args) throws UnsupportedEncodingException {
15+
File folder=new File("D:/Users/Zone/Github/GbkToUTf");
16+
circlrFolder(folder);
17+
}
18+
19+
private static void circlrFolder(File folder) throws UnsupportedEncodingException {
20+
if(folder.exists()){
21+
if(folder.isDirectory()){
22+
File[] files = folder.listFiles(new FilenameFilter() {
23+
24+
@Override
25+
public boolean accept(File dir, String name) {
26+
if(name.endsWith(".jar")||name.endsWith(".zip")||name.endsWith(".xml")||name.endsWith(".jpg")||name.endsWith(".png")){
27+
return false;
28+
}
29+
return true;
30+
}
31+
});
32+
for (File item : files) {
33+
circlrFolder(item);
34+
}
35+
}else{
36+
//不是目录
37+
Charset charsetFolder = JudgeFileCode.getFileCode(folder);
38+
if(charsetFolder!=null&&!charset.name().equals(charsetFolder.name())){
39+
String str=IOUtils.read(folder, charsetFolder.name());
40+
// IOUtils.write(folder, new String(str.getBytes(charsetFolder.name()),charset), charset.name());
41+
IOUtils.write(folder, str, charset.name());
42+
System.out.println("转码成功:"+folder.getAbsolutePath());
43+
}
44+
}
45+
}
46+
}
47+
48+
}
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
package 编码工具;
2+
import info.monitorenter.cpdetector.io.ASCIIDetector;
3+
import info.monitorenter.cpdetector.io.CodepageDetectorProxy;
4+
import info.monitorenter.cpdetector.io.JChardetFacade;
5+
import info.monitorenter.cpdetector.io.ParsingDetector;
6+
import info.monitorenter.cpdetector.io.UnicodeDetector;
7+
8+
import java.io.File;
9+
import java.nio.charset.Charset;
10+
11+
/**
12+
*
13+
*/
14+
15+
/**
16+
* @author weict
17+
*
18+
*/
19+
public class JudgeFileCode {
20+
/**
21+
* @param args
22+
*/
23+
public static Charset getFileCode(File f) {
24+
/*------------------------------------------------------------------------
25+
26+
27+
detector是探测器,它把探测任务交给具体的探测实现类的实例完成。
28+
29+
cpDetector内置了一些常用的探测实现类,这些探测实现类的实例可以通过add方法
30+
31+
加进来,如ParsingDetector、 JChardetFacade、ASCIIDetector、UnicodeDetector。
32+
33+
detector按照“谁最先返回非空的探测结果,就以该结果为准”的原则返回探测到的
34+
35+
字符集编码。
36+
37+
--------------------------------------------------------------------------*/
38+
39+
CodepageDetectorProxy detector = CodepageDetectorProxy.getInstance();
40+
41+
/*-------------------------------------------------------------------------
42+
43+
ParsingDetector可用于检查HTML、XML等文件或字符流的编码,构造方法中的参数用于
44+
45+
指示是否显示探测过程的详细信息,为false不显示。
46+
47+
---------------------------------------------------------------------------*/
48+
49+
detector.add(new ParsingDetector(false));//如果不希望判断xml的encoding,而是要判断该xml文件的编码,则可以注释掉
50+
51+
/*--------------------------------------------------------------------------
52+
53+
JChardetFacade封装了由Mozilla组织提供的JChardet,它可以完成大多数文件的编码
54+
55+
测定。所以,一般有了这个探测器就可满足大多数项目的要求,如果你还不放心,可以
56+
57+
再多加几个探测器,比如下面的ASCIIDetector、UnicodeDetector等。
58+
59+
---------------------------------------------------------------------------*/
60+
61+
detector.add(JChardetFacade.getInstance());
62+
63+
// ASCIIDetector用于ASCII编码测定
64+
65+
detector.add(ASCIIDetector.getInstance());
66+
67+
// UnicodeDetector用于Unicode家族编码的测定
68+
69+
detector.add(UnicodeDetector.getInstance());
70+
71+
Charset charset = null;
72+
73+
try {
74+
75+
charset = detector.detectCodepage(f.toURL());
76+
77+
} catch (Exception ex) {
78+
ex.printStackTrace();
79+
}
80+
81+
if (charset != null) {
82+
83+
System.out.println(f.getName() + "编码是:" + charset.name());
84+
85+
} else {
86+
87+
System.out.println(f.getName() + "未知");
88+
}
89+
return charset;
90+
91+
}
92+
93+
}

0 commit comments

Comments
 (0)