在开发过程中,可能会遇到文件编码的转换,尽管说开发工具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进行文件编码转换的更多相关文章

  1. 在Vim中查看文件编码和文件编码转换

    在Vim中查看文件编码和文件编码转换 风亡小窝 关注  0.2 2016.09.26 22:43* 字数 244 阅读 5663评论 0喜欢 2 在Vim中查看文件编码 :set fileencodi ...

  2. Linux查看文件编码格式及文件编码转换

    Linux查看文件编码格式及文件编码转换   如果你需要在Linux 中操作windows下的文件,那么你可能会经常遇到文件编码转换的问题.Windows中默认的文件格式是GBK(gb2312),而L ...

  3. PHP 支持中文目录和文件的的遍历:文件编码转换

    在使用 readdir() 遍历指定目录时,使中文目录和文件名都正常显示需要使用 iconv() 进行文件编码转换: <?php header("Content-type:text/h ...

  4. java实现文件编码监测(转)

    chardet是mozilla自动字符集探测算法代码的java移植.这个算法的最初作者是frank Tang,C++源代码在http://lxr.mozilla.org/mozilla/source/ ...

  5. java实现文件编码监测

    java实现文件编码监测 最近在做一个文档的翻译项目,可文档的编码不知道,听头疼的.尝试了很多方法最后发现JCharDet这个工具可以轻松解决这个问题.于是作此笔记希望日后提醒自己以及帮助又需要的人. ...

  6. iconv 文件编码转换

    iconv 文件编码转换 http://www.cnblogs.com/xuxm2007/archive/2010/11/09/1872379.html 查看iconv的支持的编码: $ iconv  ...

  7. (转载)Linux查看文件编码格式及文件编码转换

    Linux查看文件编码格式及文件编码转换 时间:2011-04-08作者:woyoo分类:linux评论:0 我友分享: 新浪微博 腾讯微博 搜狐微博 网易微博 开心网 QQ空间 msn 如果你需要在 ...

  8. windows linux 文件编码转换

    查看文件编码在Linux中查看文件编码可以通过以下几种方式:1.在Vim中可以直接查看文件编码:set fileencoding即可显示文件编码格式.如果你只是想查看其它编码格式的文件或者想解决用Vi ...

  9. Linux查看文件编码格式及文件编码转换<转>

    如果你需要在Linux 中操作windows下的文件 ,那么你可能会经常遇到文件 编码 转换的问题.Windows中默认的文件 格式是GBK(gb2312),而Linux 一般都是UTF-8.下面介绍 ...

随机推荐

  1. SVN - 配置

    版本控制器 1.创建文件夹 svn 2.打开终端 进入该文件夹 3.输入 svnadmin 如果有错 xcrun: error: active developer path ("/Appli ...

  2. HTML5 push

    http://blog.csdn.net/yo548720570/article/details/8599947 http://www.oschina.net/question/82993_63312 ...

  3. Best Sequence

    poj1699:http://poj.org/problem?id=1699 题意:给你nge串,让你求出这些串组成的最小的串重叠部分只算一次. 题解:我的做法是DFS,因为数据范围只有10,就算是n ...

  4. 只看Delphi自带的WnAPI帮助似乎不够

    比如,MessageBox在Delphi自带帮助的参数说明中,对其第四个参数的MB_类型说明只有最常见的6种类型,这么多年搞得我天经地义的以为MessageBox就是这么简单.今天看了一位前辈写的老代 ...

  5. haskell Types 和 Typeclasses

    Algebraic Data Types 入门 在前面的章节中,我们谈了一些 Haskell 内置的类型和 Typeclass.而在本章中,我们将学习构造类型和 Typeclass 的方法. 我们已经 ...

  6. Java Entry使用

    参考: http://blog.csdn.net/sunmenggmail/article/details/8952712 http://www.cnblogs.com/fstang/archive/ ...

  7. ETL构建数据仓库五步法

    原文:http://huangy82.blog.163.com/blog/static/49069827200923034638409/ ETL构建企业级数据仓库五步法 在数据仓库构建中,ETL贯穿于 ...

  8. Hay Points

    Hay Points TimeLimit: 1 Second   MemoryLimit: 32 Megabyte Totalsubmit: 1022   Accepted: 602 Descript ...

  9. zoj 3620 Escape Time II

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4744 Escape Time II Time Limit: 2 Seconds ...

  10. HDU-2562 奇偶位互换

    http://acm.hdu.edu.cn/showproblem.php?pid=2562 奇偶位互换 Time Limit: 3000/1000 MS (Java/Others)    Memor ...