在开发过程中,可能会遇到文件编码的转换,虽然说开发工具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
相关文章
猜你喜欢
- 64M VPS建站:是否适合初学者操作和管理? 2025-06-10
- ASP.NET自助建站系统中的用户注册和登录功能定制方法 2025-06-10
- ASP.NET自助建站系统的域名绑定与解析教程 2025-06-10
- 个人服务器网站搭建:如何选择合适的服务器提供商? 2025-06-10
- ASP.NET自助建站系统中如何实现多语言支持? 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 55
-
2025-05-27 84
-
2025-05-29 70
-
2025-05-29 30
-
2025-06-04 106
热门评论

