Java文件编码格式转换
转自博文《Java文件编码格式转换》:
默认被转换的格式为GBK,转换成的格式为UTF-8
import info.monitorenter.cpdetector.CharsetPrinter;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.io.Writer;
public class EncodeFormatTransfer {
public static String DefaultSrcEncodeFormat = "GBK";
public static String DefaultDestEncodeFormat = "UTF-8";
public static String UnsupportedEncodingExceptionError = "编码格式错误!";
public static String FileNotFoundExceptionError = "文件不存在!";
public static String IOExceptionError = "文件读写错误!";
public static String IsUtf8File = "文件是UTF-8编码格式!";
public static String IsNotUtf8File = "文件不是UTF-8编码格式!";
public static String readFile(String path,String encodeFormat){
if((encodeFormat==null || encodeFormat.equals(""))){
if(isUTF8File(path))
encodeFormat = DefaultDestEncodeFormat;
else
encodeFormat = DefaultSrcEncodeFormat;
}
try {
String context = "";
InputStreamReader isr;
isr = new InputStreamReader(new FileInputStream(path),encodeFormat);
BufferedReader br=new BufferedReader(isr);
String line;
while((line=br.readLine())!=null){
context += line + "\r\n";
System.out.println(line);
}
br.close();
return context;
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
System.out.println(UnsupportedEncodingExceptionError);
e.printStackTrace();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
System.out.println(FileNotFoundExceptionError);
e.printStackTrace();
}catch (IOException e) {
// TODO Auto-generated catch block
System.out.println(IOExceptionError);
e.printStackTrace();
};
return "";
}
/*public static boolean isUTF8File(String path){
try {
File file = new File(path);
CharsetPrinter detector = new CharsetPrinter();
String charset = detector.guessEncoding(file);
InputStream in = new java.io.FileInputStream(file);
byte[] b = new byte[3];
in.read(b);
in.close();
System.out.println(b[0] + " " + b[1] + " " + b[2]);
if (b[0] == 0xEF && b[1] == 0xBB && b[2] == 0XBF){
System.out.println(IsUtf8File);
return true;
}
if (b[0] == -17 && b[1] == -69 && b[2] == -65){
System.out.println(IsUtf8File);
return true;
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println(FileNotFoundExceptionError);
}catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println(IOExceptionError);
}
System.out.println(IsNotUtf8File);
return false;
}*/
public static boolean isUTF8File(String path){
try {
File file = new File(path);
CharsetPrinter detector = new CharsetPrinter();
String charset = detector.guessEncoding(file);
if(charset.equalsIgnoreCase(DefaultDestEncodeFormat)){
System.out.println(IsUtf8File);
return true;
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println(FileNotFoundExceptionError);
}catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println(IOExceptionError);
}
System.out.println(IsNotUtf8File);
return false;
}
public static String transfer(String context,String encodeFormat) {
if(encodeFormat==null || encodeFormat.equals(""))
encodeFormat = DefaultDestEncodeFormat;
try {
byte[] content = context.getBytes();
String result = new String(content,encodeFormat);
return result;
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
System.out.println(UnsupportedEncodingExceptionError);
e.printStackTrace();
}
return "";
}
public static void writeFile(String context,String path,String destEncode){
File file = new File(path);
if(file.exists())
file.delete();
BufferedWriter writer;
try {
FileOutputStream fos = new FileOutputStream(path,true);
writer = new BufferedWriter(new OutputStreamWriter(fos, destEncode));
writer.append(context);
writer.close();
} catch (IOException e) {
System.out.println(IOExceptionError);
e.printStackTrace();
}
}
public static void writeFile(String context,String path){
File file = new File(path);
if(file.exists())
file.delete();
Writer writer;
try {
writer = new FileWriter(file, true);
writer.append(context);
writer.close();
} catch (IOException e) {
System.out.println(IOExceptionError);
e.printStackTrace();
}
}
public static void transfer(String srcPath,String destPath,String srcEncode,String destEncode){
if(destPath==null || destPath.equals(""))
destPath = srcPath;
String context = readFile(srcPath,srcEncode);
context = transfer(context,destEncode);
writeFile(context,destPath,destEncode);
}
public static void transfer(String srcPath,String destPath,String destEncode){
if(isUTF8File(srcPath)){
transfer(srcPath,destPath,DefaultDestEncodeFormat,destEncode);
}else{
transfer(srcPath,destPath,DefaultSrcEncodeFormat,destEncode);
}
}
public static void main(String args[]){
String path1 = "f:/Notepad1.java";
String path2 = "f:/Notepad2.java";
transfer(path1,path2,"UTF-8");
transfer(path1,path2,"UTF-8","UTF-8");
}
}
主要jar包:cpdetector.jar
下载地址: http://cpdetector.sourceforge.net/
同时还需jchardet-1.0.jar这个包,否则detector.add(cpdetector.io.JChardetFacade.getInstance()); 会报错;
下载地址: http://www.jarfinder.com/index.php/jars/versionInfo/40297
还有一个antlr.jar,不然运行过程中detector.add(new ParsingDetector(false));会报错;
下载地址: http://www.java2s.com/Code/Jar/ABC/Downloadantlrjar.htm
Java文件编码格式转换的更多相关文章
- FilesCodingConvert--批量文件编码格式转换工具
FilesCodingConvert–批量文件编码格式转换工具 简介 最近开始学习使用Android Studio,因为它的方便易用,我打算以后就不在使用ADT的方式编写Android项目了.当从Ec ...
- java项目编码格式转换(如GBK转UTF-8)
昨天突然下了个Java项目,把项目导入到eclipse中,发现项目是gbk编码格式想把项目变为utf-8,但是发现转换格式比较麻烦就写了这个代码,后面改进了下,想到说不定有人也需要就把它写了出来 代码 ...
- Linux 文件编码格式转换
如果需要在Linux 中操作windows下的文件,那么经常遇到文件编码转换的问题. Windows中默认的文件格式是GBK(gb2312),而Linux一般都是UTF-. 查看文件编码 在vim 中 ...
- Linux下查看文件编码,文件编码格式转换和文件名编码转换
linux相关 2008-10-07 10:46 阅读1392 评论0 字号: 大大 中中 小小 如果你需要在Linux中 操作windows下的文件,那么你可能会经常遇到文件编 ...
- MacOS 自带文件编码格式转换工具
[命令功能]iconv 是Linux操作系统用于将文本编码格式从一种转外另外一种的工具命令.[使用方法] iconv [OPTION...] [-f ENCODING] [-t ENCODING] [ ...
- ubuntu 文件编码格式 转换
正在学习jquery,之前在windows下弄的编码到了 ubuntu下,乱码: 找到一个方法: iconv : 源文件:a.htm 格式:gbk: 目标: a.html 格式:utf8: ic ...
- 在Vim中查看文件编码和文件编码转换
在Vim中查看文件编码和文件编码转换 风亡小窝 关注 0.2 2016.09.26 22:43* 字数 244 阅读 5663评论 0喜欢 2 在Vim中查看文件编码 :set fileencodi ...
- 解决eclipse中的Java文件,使用idea打开的乱码问题
吐槽: 在克隆一些Github上面资源的时候,使用idea打开,会出现乱码的情况 而使用eclipse打开,这种情况就会消失.「是因为eclipse使用的是GBK编码,idea使用的是utf-8」 这 ...
- Linux查看文件编码格式及文件编码转换
Linux查看文件编码格式及文件编码转换 如果你需要在Linux 中操作windows下的文件,那么你可能会经常遇到文件编码转换的问题.Windows中默认的文件格式是GBK(gb2312),而L ...
随机推荐
- python中的类中属性元素加self.和不加self.的区别
在类中,self只能在函数中使用,表示的是实例属性,就是每个实例可以设置不值,而不相互影响.如果在类级别使用没有self的属性,是类属性,一般作为全局变量来用的.事实上:就是一个是类属性 一个是对象属 ...
- 03_Spring工厂接口
Spring工厂接口 1.BeanFactory 接口 和 ApplicationContext 接口区别 ? * ApplicationContext 接口继承BeanFactory接口, ...
- Python基础学习笔记(八)常用字典内置函数和方法
参考资料: 1. <Python基础教程> 2. http://www.runoob.com/python/python-dictionary.html 3. http://www.lia ...
- 《Linux内核设计的艺术》学习笔记(二)INT 0x13中断
参考资料: 1. <IBM-PC汇编语言程序设计> 2. http://blog.sina.com.cn/s/blog_5028978101008wk2.html 3. http://ww ...
- XAF应用开发教程(三)业务对象模型之引用类型与关联关系
本节介绍信息系统开发中最常见的问题,引用关系,一对多关系,多对多关系. 以客户信息为例,客户通常需要客户分类,如VIP客户,普通客户,潜在客户.当然,我们可以定义枚举类型进行定义出这个类型,并在客户类 ...
- 删除List中制定的值的方法
/** * * @param args */ public static void main(String[] args) { List<String> list = new ArrayL ...
- 期权交易基本原理——买进看跌期权(Long Put),卖出看跌期权(Short Put)
期权交易基本原理--买进看跌期权(Long Put),卖出看跌期权(Short Put) 来源:中电投先融期货-青岛 浏览:13508次2014-07-25 14:25:55 3 第三节 买进看跌期权 ...
- [转载] 根据多年经验整理的《互联网MySQL开发规范》
原文: http://weibo.com/p/2304181380b3f180102vsg5 根据多年经验整理的<互联网MySQL开发规范> 写在前面:无规矩不成方圆.对于刚加入互联网的朋 ...
- 事件冒泡与事件委托 -Tom
事件冒泡 事件冒泡,就是事件触发的时候通过DOM向上冒泡,首先要知道不是所有的事件都有冒泡.事件在一个目标元素上触发的时候,该事件将触发祖先节点元素,直到最顶层的元素: 如图所示,如果a连接被点击,触 ...
- Oracle一列的多行数据拼成一行显示字符
Oracle一列的多行数据拼成一行显示字符 oracle 提供了两个函数WMSYS.WM_CONCAT 和 ListAgg函数. www.2cto.com 先介绍:WMSYS.WM_CO ...