用java将GBK工程转为uft8的方法实例

2025-05-29 0 89

本文介绍了用java将GBK工程转为uft8,分享给大家,具体如下:

windows下的默认编码为GBK还有gb2312,如何把gbk的java工程转为utf8的呢,如果直接修改工程编码,其实里面的java文件中中文是会乱码的,写了个批量转换java工程的程序,消遣一下。

为什么要转码?

有些老的项目,或者朋友的项目之前没注意在windows上不是utf8,而你有需要看注释或者什么,总不能一个文件一个文件的去改编码属性吧。

本程序试用范围

gbk的代码,或者gb2312的工程均可以转换

编码转换的思路

本来想做成一个通用的会自动检测编码,自动转换的程序。但是由于判断编码类型不准,所以做成了针对GBK的转换。

  1. 制定gbk编码把文件流读进来,加载到内存,转为String类型的内容
  2. 将String内容转为utf8的String
  3. 将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

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 用java将GBK工程转为uft8的方法实例 https://www.kuaiidc.com/115429.html

相关文章

发表评论
暂无评论