使用java进行文件编码转换
在开发过程中,可能会遇到文件编码的转换,尽管说开发工具eclipse能够转换编码,可是有的情况却非常不方便。比方,原来文件本身的编码是GBK,如今要转换成UTF-8,假设直接在eclipse中把文件编码改动成UTF-8,恭喜你,是乱码,由于不能直接从GBK到UTF-8进行转换,这时就须要我们手动的来转换编码。以下是一个文件编码转换的工具类。
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();
}
}
使用java进行文件编码转换的更多相关文章
- 在Vim中查看文件编码和文件编码转换
在Vim中查看文件编码和文件编码转换 风亡小窝 关注 0.2 2016.09.26 22:43* 字数 244 阅读 5663评论 0喜欢 2 在Vim中查看文件编码 :set fileencodi ...
- Linux查看文件编码格式及文件编码转换
Linux查看文件编码格式及文件编码转换 如果你需要在Linux 中操作windows下的文件,那么你可能会经常遇到文件编码转换的问题.Windows中默认的文件格式是GBK(gb2312),而L ...
- PHP 支持中文目录和文件的的遍历:文件编码转换
在使用 readdir() 遍历指定目录时,使中文目录和文件名都正常显示需要使用 iconv() 进行文件编码转换: <?php header("Content-type:text/h ...
- java实现文件编码监测(转)
chardet是mozilla自动字符集探测算法代码的java移植.这个算法的最初作者是frank Tang,C++源代码在http://lxr.mozilla.org/mozilla/source/ ...
- java实现文件编码监测
java实现文件编码监测 最近在做一个文档的翻译项目,可文档的编码不知道,听头疼的.尝试了很多方法最后发现JCharDet这个工具可以轻松解决这个问题.于是作此笔记希望日后提醒自己以及帮助又需要的人. ...
- iconv 文件编码转换
iconv 文件编码转换 http://www.cnblogs.com/xuxm2007/archive/2010/11/09/1872379.html 查看iconv的支持的编码: $ iconv ...
- (转载)Linux查看文件编码格式及文件编码转换
Linux查看文件编码格式及文件编码转换 时间:2011-04-08作者:woyoo分类:linux评论:0 我友分享: 新浪微博 腾讯微博 搜狐微博 网易微博 开心网 QQ空间 msn 如果你需要在 ...
- windows linux 文件编码转换
查看文件编码在Linux中查看文件编码可以通过以下几种方式:1.在Vim中可以直接查看文件编码:set fileencoding即可显示文件编码格式.如果你只是想查看其它编码格式的文件或者想解决用Vi ...
- Linux查看文件编码格式及文件编码转换<转>
如果你需要在Linux 中操作windows下的文件 ,那么你可能会经常遇到文件 编码 转换的问题.Windows中默认的文件 格式是GBK(gb2312),而Linux 一般都是UTF-8.下面介绍 ...
随机推荐
- Pentaho Data Integration (三) Pan
官网连接: http://wiki.pentaho.com/display/EAI/Pan+User+Documentation Pan Pan 是一个可以执行使用Spoon编辑的transforma ...
- ACdream训练赛系列のJava专场
/* * this code is made by mhy12345 * Problem: 1669 * Verdict: Accepted * Submission Date: 2015-04-21 ...
- 得到bundle seed id
- (NSString *)bundleSeedID { NSDictionary *query = [NSDictionary dictionaryWithObjectsAndKeys: (__br ...
- 【BZOJ 3122】 [Sdoi2013]随机数生成器 (BSGS)
3122: [Sdoi2013]随机数生成器 Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 1442 Solved: 552 Description ...
- 利用TEA算法进行数据加密
TEA(Tiny Encryption Algorithm)是一种小型的对称加密解密算法,最初是由剑桥计算机实验室的 David Wheeler 和 Roger Needham 在 1994 年设计. ...
- 没有document.getElementByName
首先声明的是: document.getElementByName方法没有.document.getElementsByName得到的是标签的数组 document.getElementId得到的是某 ...
- js常用操作代码
页面前进后退<input type=button value=刷新 onclick="window.location.reload()"><input type= ...
- Jersey+Spring+Maven(转)
spring和maven的搭建参考相关文档.本文只介绍与jersey有关配置. 一.jersey在maven中的依赖包 <!-- jersey --> <dependency> ...
- Ajax长连接应用
所谓的长连接,就是不断去发送请求,把请求阻塞在服务器端,每次超过请求时间就去重新发送请求,保持连接,随时获取服务器端的响应的数据 function connection(){ $.ajax({ typ ...
- ubuntu12.04 mysql服务器乱码问题的解决办法
网上方法太杂乱,有些甚至很复杂,其实ubuntu下只需要修改一个配置文件即可. sudo vi /etc/mysql/my.cnf 在[client]下加入 default-character-set ...