本文介绍了用java将GBK工程转为uft8,分享给大家,具体如下:
windows下的默认编码为GBK还有gb2312,如何把gbk的java工程转为utf8的呢,如果直接修改工程编码,其实里面的java文件中中文是会乱码的,写了个批量转换java工程的程序,消遣一下。
为什么要转码?
有些老的项目,或者朋友的项目之前没注意在windows上不是utf8,而你有需要看注释或者什么,总不能一个文件一个文件的去改编码属性吧。
本程序试用范围
gbk的代码,或者gb2312的工程均可以转换
编码转换的思路
本来想做成一个通用的会自动检测编码,自动转换的程序。但是由于判断编码类型不准,所以做成了针对GBK的转换。
- 制定gbk编码把文件流读进来,加载到内存,转为String类型的内容
- 将String内容转为utf8的String
- 将String内容写入文件
核心代码:
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
|
public class TransferProject{
public static void transferFile(String pathName,intdepth)throwsException{
File dirFile = new File(pathName);
if (!isValidFile(dirFile)) return ;
//获取此目录下的所有文件名与目录名
String[] fileList = dirFile.list();
int currentDepth = depth + 1 ;
for ( int i = 0 ; i < fileList.length; i++) {
String string = fileList[i];
File file = new File(dirFile.getPath(), string);
String name = file.getName();
//如果是一个目录,搜索深度depth++,输出目录名后,进行递归
if (file.isDirectory()) {
//递归
transferFile(file.getCanonicalPath(), currentDepth);
} else {
if (name.contains( ".java" ) || name.contains( ".properties" ) || name.contains( ".xml" )) {
readAndWrite(file);
System.out.println(name + " has converted to utf8 " );
}
}
}
}
private static boolean isValidFile(File dirFile)throwsIOException{
if (dirFile.exists()) {
System.out.println( "file exist" );
return true ;
}
if (dirFile.isDirectory()) {
if (dirFile.isFile()) {
System.out.println(dirFile.getCanonicalFile());
}
return true ;
}
return false ;
}
private static void readAndWrite(File file)throwsException{
String content = FileUtils.readFileByEncode(file.getPath(), "GBK" );
FileUtils.writeByBufferedReader(file.getPath(), new String(content.getBytes( "UTF-8" ), "UTF-8" ));
}
public static void main(String[] args)throwsException{
//程序入口,制定src的path
String path = "/Users/mac/Downloads/unit06_jdbc/src" ;
transferFile(path, 1 );
}
}
|
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
public class FileUtils{
public static void writeByBufferedReader(String path, String content){
try {
File file = new File(path);
file.delete();
if (!file.exists()) {
file.createNewFile();
}
FileWriter fw = new FileWriter(file, false );
BufferedWriter bw = new BufferedWriter(fw);
bw.write(content);
bw.flush();
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public staticStringreadFileByEncode(String path, String chatSet)throwsException{
InputStream input = new FileInputStream(path);
InputStreamReader in = new InputStreamReader(input, chatSet);
BufferedReader reader = new BufferedReader(in);
StringBuffer sb = new StringBuffer();
String line = reader.readLine();
while (line != null ) {
sb.append(line);
sb.append( "\\r\\n" );
line = reader.readLine();
}
return sb.toString();
}
}
|
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持快网idc。
原文链接:http://geeksblog.cc/trandsferProject.html?utm_source=tuicool&utm_medium=referral
相关文章
猜你喜欢
- 个人网站搭建:如何挑选具有弹性扩展能力的服务器? 2025-06-10
- 个人服务器网站搭建:如何选择适合自己的建站程序或框架? 2025-06-10
- 64M VPS建站:能否支持高流量网站运行? 2025-06-10
- 64M VPS建站:怎样选择合适的域名和SSL证书? 2025-06-10
- 64M VPS建站:怎样优化以提高网站加载速度? 2025-06-10
TA的动态
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
您的支持,是我们最大的动力!
热门文章
-
2025-05-29 74
-
2025-05-27 71
-
spring boot+vue 的前后端分离与合并方案实例详解
2025-05-29 76 -
2025-05-27 59
-
2025-05-29 62
热门评论