使用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.下面介绍 ...
随机推荐
- BZOJ 1690: [Usaco2007 Dec]奶牛的旅行
Description 作为对奶牛们辛勤工作的回报,Farmer John决定带她们去附近的大城市玩一天.旅行的前夜,奶牛们在兴奋地讨论如何最好地享受这难得的闲暇. 很幸运地,奶牛们找到了一张详细的城 ...
- kav卡巴斯基2014 优化设置
- 算法练习之:Biorhythms
Biorhythms Time Limit: 1000MS Memory Limit: 10000KB Problem Description Some people believe that th ...
- *[hackerrank]Tree Covering
https://www.hackerrank.com/contests/illuminati/challenges/tree-covering 这道题先是在上次交流讨论了一下,然后两位百度的朋友先写完 ...
- 【Spring】Spring IOC原理及源码解析之scope=request、session
一.容器 1. 容器 抛出一个议点:BeanFactory是IOC容器,而ApplicationContex则是Spring容器. 什么是容器?Collection和Container这两个单词都有存 ...
- Android JNI使用方法
经过几天的努力终于搞定了android JNI部分,下面将我的这个小程序和大家分享一下.android JNI是连接android Java部分和C/C++部分的纽带,完整使用JNI需要Java代码和 ...
- Hibernate:1对1关系总结。
QQ和QQ空间是1对1关系:配置如下 一.主键关联,双方共同维护表关系,以主键关联 public class QQ { private Long id; private QQZone zone; } ...
- Android源码之Matrix
Matrix类在Android中主要用来进行矩阵变换,其可操作的对象包括图像.点阵.Vector(向量).矩形等. Matrix支持的变换类型主要有以下几种: 1.Translate:平移变换 2.R ...
- 原始启动log&新log
root@Taiyear:/# U-Boot 1.1.3 (Dec 27 2013 - 09:14:28) SoC:MediaTek MT7620 DRAM: Memory Testing..655 ...
- bzoj3669
不难想到从小到大穷举a,判断在携带不超过a个B型精灵的情况下最少携带多少个B型精灵这是一个经典的问题,显然要求出最小生成树,树上1到N路径上最大的边即是答案所以我们要动态维护一个最小生成树可以用lin ...