java实现文件编码转换的方法

2025-05-29 0 60

在开发过程中,可能会遇到文件编码的转换,虽然说开发工具eclipse可以转换编码,但是有的情况却很不方便。比如,原来文件本身的编码是gbk,现在要转换成utf-8,如果直接在eclipse中把文件编码修改成utf-8,恭喜你,是乱码,因为不能直接从gbk到utf-8进行转换,这时就需要我们手动的来转换编码。下面是一个文件编码转换的工具类。

?

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

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158
package com.mikan.stuff;

import java.io.file;

import java.io.fileinputstream;

import java.io.fileoutputstream;

import java.io.filenamefilter;

import java.io.inputstream;

import java.io.inputstreamreader;

import java.io.outputstream;

import java.io.outputstreamwriter;

import java.nio.charset.charset;

import java.nio.charset.unsupportedcharsetexception;

public class filecharsetconverter {

public static void main(string[] args) throws exception {

convert("d:\\\\stuff\\\\src\\\\main\\\\java\\\\com\\\\mikan\\\\stuff\\\\test.txt",

"gbk", "utf-8", new filenamefilter() {

@override

public boolean accept(file dir, string name) {

return name.endswith("txt");

}

});

}

/**

* 把指定文件或目录转换成指定的编码

*

* @param filename

* 要转换的文件

* @param fromcharsetname

* 源文件的编码

* @param tocharsetname

* 要转换的编码

* @throws exception

*/

public static void convert(string filename, string fromcharsetname,

string tocharsetname) throws exception {

convert(new file(filename), fromcharsetname, tocharsetname, null);

}

/**

* 把指定文件或目录转换成指定的编码

*

* @param file

* 要转换的文件或目录

* @param fromcharsetname

* 源文件的编码

* @param tocharsetname

* 要转换的编码

* @throws exception

*/

public static void convert(file file, string fromcharsetname,

string tocharsetname) throws exception {

convert(file, fromcharsetname, tocharsetname, null);

}

/**

* 把指定文件或目录转换成指定的编码

*

* @param file

* 要转换的文件或目录

* @param fromcharsetname

* 源文件的编码

* @param tocharsetname

* 要转换的编码

* @param filter

* 文件名过滤器

* @throws exception

*/

public static void convert(string filename, string fromcharsetname,

string tocharsetname, filenamefilter filter) throws exception {

convert(new file(filename), fromcharsetname, tocharsetname, filter);

}

/**

* 把指定文件或目录转换成指定的编码

*

* @param file

* 要转换的文件或目录

* @param fromcharsetname

* 源文件的编码

* @param tocharsetname

* 要转换的编码

* @param filter

* 文件名过滤器

* @throws exception

*/

public static void convert(file file, string fromcharsetname,

string tocharsetname, filenamefilter filter) throws exception {

if (file.isdirectory()) {

file[] filelist = null;

if (filter == null) {

filelist = file.listfiles();

} else {

filelist = file.listfiles(filter);

}

for (file f : filelist) {

convert(f, fromcharsetname, tocharsetname, filter);

}

} else {

if (filter == null

|| filter.accept(file.getparentfile(), file.getname())) {

string filecontent = getfilecontentfromcharset(file,

fromcharsetname);

savefile2charset(file, tocharsetname, filecontent);

}

}

}

/**

* 以指定编码方式读取文件,返回文件内容

*

* @param file

* 要转换的文件

* @param fromcharsetname

* 源文件的编码

* @return

* @throws exception

*/

public static string getfilecontentfromcharset(file file,

string fromcharsetname) throws exception {

if (!charset.issupported(fromcharsetname)) {

throw new unsupportedcharsetexception(fromcharsetname);

}

inputstream inputstream = new fileinputstream(file);

inputstreamreader reader = new inputstreamreader(inputstream,

fromcharsetname);

char[] chs = new char[(int) file.length()];

reader.read(chs);

string str = new string(chs).trim();

reader.close();

return str;

}

/**

* 以指定编码方式写文本文件,存在会覆盖

*

* @param file

* 要写入的文件

* @param tocharsetname

* 要转换的编码

* @param content

* 文件内容

* @throws exception

*/

public static void savefile2charset(file file, string tocharsetname,

string content) throws exception {

if (!charset.issupported(tocharsetname)) {

throw new unsupportedcharsetexception(tocharsetname);

}

outputstream outputstream = new fileoutputstream(file);

outputstreamwriter outwrite = new outputstreamwriter(outputstream,

tocharsetname);

outwrite.write(content);

outwrite.close();

}

}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持快网idc。

原文链接:https://blog.csdn.net/mhmyqn/article/details/37917947

收藏 (0) 打赏

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

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

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

快网idc优惠网 建站教程 java实现文件编码转换的方法 https://www.kuaiidc.com/111586.html

相关文章

发表评论
暂无评论