在開發(fā)過程中,可能會遇到文件編碼的轉(zhuǎn)換,雖然說開發(fā)工具eclipse可以轉(zhuǎn)換編碼,但是有的情況卻很不方便。比如,原來文件本身的編碼是gbk,現(xiàn)在要轉(zhuǎn)換成utf-8,如果直接在eclipse中把文件編碼修改成utf-8,恭喜你,是亂碼,因?yàn)椴荒苤苯訌膅bk到utf-8進(jìn)行轉(zhuǎn)換,這時就需要我們手動的來轉(zhuǎn)換編碼。下面是一個文件編碼轉(zhuǎn)換的工具類。
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" ); } }); } /** * 把指定文件或目錄轉(zhuǎn)換成指定的編碼 * * @param filename * 要轉(zhuǎn)換的文件 * @param fromcharsetname * 源文件的編碼 * @param tocharsetname * 要轉(zhuǎn)換的編碼 * @throws exception */ public static void convert(string filename, string fromcharsetname, string tocharsetname) throws exception { convert( new file(filename), fromcharsetname, tocharsetname, null ); } /** * 把指定文件或目錄轉(zhuǎn)換成指定的編碼 * * @param file * 要轉(zhuǎn)換的文件或目錄 * @param fromcharsetname * 源文件的編碼 * @param tocharsetname * 要轉(zhuǎn)換的編碼 * @throws exception */ public static void convert(file file, string fromcharsetname, string tocharsetname) throws exception { convert(file, fromcharsetname, tocharsetname, null ); } /** * 把指定文件或目錄轉(zhuǎn)換成指定的編碼 * * @param file * 要轉(zhuǎn)換的文件或目錄 * @param fromcharsetname * 源文件的編碼 * @param tocharsetname * 要轉(zhuǎn)換的編碼 * @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); } /** * 把指定文件或目錄轉(zhuǎn)換成指定的編碼 * * @param file * 要轉(zhuǎn)換的文件或目錄 * @param fromcharsetname * 源文件的編碼 * @param tocharsetname * 要轉(zhuǎn)換的編碼 * @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); } } } /** * 以指定編碼方式讀取文件,返回文件內(nèi)容 * * @param file * 要轉(zhuǎn)換的文件 * @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 * 要轉(zhuǎn)換的編碼 * @param content * 文件內(nèi)容 * @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(); } } |
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。
原文鏈接:https://blog.csdn.net/mhmyqn/article/details/37917947