今天想把两个工程合并,但发现一个是GBK格式,一个是UTF-8格式的,合并后出现乱码。
霎时间感到蛋极为不舒服。。。。。
由于文件数量很多,懒得手动一个一个去把代码复制粘贴,所以写了这个文件格式互转的代码(见下方代码)。。。
大家如果以后用得上就留着吧。
功能:将单文件或者整个文件目录树下的文件进行GBK与UTF-8的互转。
(ps:1,目录树下SVN的".svn"目录我是直接复制过去的,没有转换。。。但没能把它设置成隐藏的比较蛋疼。
2,svn文件略过功能如果不需要,可以自行删除对应代码。
3.实例参考main函数。
4,deleteFile和copyFile这两个函数是从自己以前的代码复制过来,寥寥修改了一下,代码有些冗余
5,如想扩展其他格式转换,可以修改两个private的转换函数)
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Writer;
/**
* 文件或者文件夹目录下文件的类型转换:UTF-8与GBK 互转
* @author weidong
*
*/
public class FormatInterchange {
/**
* 将UTF-8格式的source文件或者此路径下的所有子文件转换成GBK格式,并存放到destination路径下<br>
* (若是文件夹,树形结构保持不变)<br>
* (SVN的.svn目录则直接复制,不转换)
*
* @param source
* 需要转换的UTF-8文件或者文件夹(文件夹下所有文件需都是UTF-8格式)
* @param destination
* 转换成的GBK文件存放的路径(无需包含文件名,此函数将按照原有的文件名生成对应文件)
* @return true 转换成功; false 转换失败
*/
public boolean transfer_UTF8_To_GBK(String source, String destination) {
File src = new File(source);
if (!src.exists()) {
System.out.println("文件源: " + source + " 不存在!");
return false;
}
// 单个文件,则直接转换
if (src.isFile()) {
if (!destination.endsWith(File.separator))
destination += File.separator;
File dest = new File(destination);
if (!dest.exists()){
dest.mkdirs();
}
try {
this.transfer_UTF8_to_GBK(source, destination + src.getName());
return true;
} catch (IOException e) {
e.printStackTrace();
return false;
}
} else {// 如果是目录,则按照原来树形结构迭代进行转换和保存
File[] files = src.listFiles();
if (files == null) {
System.out.println("文件源: " + source + " 下没有文件!");
return false;
}
for (File temp : files) {
if (!destination.endsWith(File.separator))
destination += File.separator;
if(temp.getName().equals(".svn")){
this.copyFile(temp.getAbsolutePath(), destination
+ temp.getParentFile().getName());
continue;
}
this.transfer_UTF8_To_GBK(temp.getAbsolutePath(), destination
+ temp.getParentFile().getName());
}
}
return true;
}
/**
* 将GBK格式的source文件或者此路径下的所有子文件转换成UTF-8格式,并存放到destination路径下<br>
* (若是文件夹,树形结构保持不变)<br>
* (SVN的.svn目录则直接复制,不转换)
*
* @param source
* 需要转换的GBK文件或者文件夹(文件夹下所有文件需都是GBK格式)
* @param destination
* 转换成的UTF-8文件存放的路径(无需包含文件名,此函数将按照原有的文件名生成对应文件)
* @return true 转换成功; false 转换失败
*/
public boolean transfer_GBK_To_UTF8(String source, String destination) {
File src = new File(source);
if (!src.exists()) {
System.out.println("文件源: " + source + " 不存在!");
return false;
}
// 单个文件,则直接转换
if (src.isFile()) {
if (!destination.endsWith(File.separator))
destination += File.separator;
File dest = new File(destination);
if (!dest.exists()){
dest.mkdirs();
}
try {
this.transfer_GBK_to_UTF8(source, destination + src.getName());
return true;
} catch (IOException e) {
e.printStackTrace();
return false;
}
} else {// 如果是目录,则按照原来树形结构迭代进行转换和保存
File[] files = src.listFiles();
if (files == null) {
System.out.println("文件源: " + source + " 下没有文件!");
return false;
}
for (File temp : files) {
if (!destination.endsWith(File.separator))
destination += File.separator;
if(temp.getName().equals(".svn")){
this.copyFile(temp.getAbsolutePath(), destination
+ temp.getParentFile().getName());
continue;
}
this.transfer_GBK_To_UTF8(temp.getAbsolutePath(), destination
+ temp.getParentFile().getName());
}
}
return true;
}
/**
* 转换格式:GBK to UTF-8
*
* @param srcFileName
* 需要转换的GBK文件路径位置
* @param destFileName
* 转换成的UTF-8文件存放的路径位置
* @throws IOException
*/
private void transfer_GBK_to_UTF8(String srcFileName, String destFileName)
throws IOException {
String lineSeparator = System.getProperty("line.separator");
FileInputStream fis = new FileInputStream(srcFileName);
StringBuffer content = new StringBuffer();
DataInputStream in = new DataInputStream(fis);
BufferedReader d = new BufferedReader(new InputStreamReader(in, "GBK"));
String line = null;
while ((line = d.readLine()) != null) {
content.append(line + lineSeparator);
}
d.close();
in.close();
fis.close();
Writer ow = new OutputStreamWriter(new FileOutputStream(destFileName),
"utf-8");
ow.write(content.toString());
ow.close();
}
/**
* 转换格式:UTF-8 to GBK
*
* @param srcFileName
* 需要转换的UTF-8文件路径位置
* @param destFileName
* 转换成的GBK文件存放的路径位置
* @throws IOException
*/
private void transfer_UTF8_to_GBK(String srcFileName, String destFileName)
throws IOException {
String lineSeparator = System.getProperty("line.separator");
FileInputStream fis = new FileInputStream(srcFileName);
StringBuffer content = new StringBuffer();
DataInputStream in = new DataInputStream(fis);
BufferedReader d = new BufferedReader(
new InputStreamReader(in, "utf-8"));
String line = null;
while ((line = d.readLine()) != null) {
content.append(line + lineSeparator);
}
d.close();
in.close();
fis.close();
Writer ow = new OutputStreamWriter(new FileOutputStream(destFileName),
"GBK");
ow.write(content.toString());
ow.close();
}
/**
* 复制文件或文件夹,如果遇到同名的文件或文件夹,则覆盖
* @param sourcePath 源文件或文件夹的完整路径名
* @param targetPath 目标文件夹的完整路径
* @return
*/
private boolean copyFile(String sourcePath, String targetPath) {
//返回值
boolean error = true;
//执行过程
try {
if(!targetPath.endsWith("/") || !targetPath.endsWith("\\"))
targetPath += "\\";
File dir = new File(targetPath);
File file = new File(sourcePath);
if(file.exists()) {
if(file.isFile()) {
//删除目标路径上的同名文件
String dirPath = dir.getAbsolutePath();
if(!dirPath.endsWith(File.separator)) {
dirPath += File.separator;
}
//移动文件
if(file.renameTo(new File(dir, file.getName()))) {
error = error && true;
}
else {
error = error && false;
}
}
else if(file.isDirectory()) {
//删除目标路径上的同名文件夹
String dirPath = dir.getAbsolutePath();
if(!dirPath.endsWith(File.separator)) {
dirPath += File.separator;
}
this.deleteFile(dirPath + file.getName());
//在目标路径上新建一个空的同名文件夹
File newDir = new File(dirPath + file.getName());
error = error && newDir.mkdirs();
//移动文件夹的内容
boolean error_d = true;
File[] files = file.listFiles();
for(File tempFile : files) {
error_d = error_d && this.copyFile(tempFile.getAbsolutePath(), dirPath + file.getName());
}
error = error && error_d;
}
else {
error = false;
}
}
else {
error = false;
}
}
catch(Exception e) {
error = false;
}
//返回
return error;
}
/**
* 删除文件或文件夹
* @param path 要删除文件的完整路径名,或文件夹的完整路径名
* @return
*/
private boolean deleteFile(String path) {
//返回值
boolean error = true;
//执行过程
try {
File file = new File(path);
if(file.exists()) {
if(file.isDirectory()) {
boolean error_d = true;
//删除文件夹的内容
File[] files = file.listFiles();
for(File tempFile : files) {
error_d = error_d && this.deleteFile(tempFile.getAbsolutePath());
}
//删除空文件夹
if(file.delete()) {
error = error_d && true;
}
else {
error = error_d && false;
}
}
else if(file.isFile()) {
if(file.delete()) {
}
else {
error = false;
}
}
else {
error = false;
}
}
else {
error = false;
}
}
catch(Exception e) {
error = false;
}
//返回
return error;
}
public static void main(String[] args) {
FormatInterchange fi = new FormatInterchange();
//将C:\test\src目录下的所有文件转换成UTF-8格式,存放到C:\test\UTF8目录下,文件目录树形结构保持不变(SVN的.svn目录则直接复制,不转换)
fi.transfer_GBK_To_UTF8("C:\\test\\src", "C:\\test\\UTF8");
// //将C:\test\UTF8\src目录下的所有文件转换成GBK格式,存放到C:\test目录下,文件目录树形结构保持不变(SVN的.svn目录则直接复制,不转换)
// fi.transfer_UTF8_To_GBK("C:\\test\\UTF8\\src", "C:\\test");
}
}
分享到:
相关推荐
文件包含:gbk2utf-8.bat、utf-82gbk.bat、iconv.exe及使用说明.txt四个文件 使用说明:1把要转换的所有.h和.c文件拷入该目录下,双击相应的bat文件即可。 2转换结果会保存在utf-8Res或gbkRes目录下。 3如果转换除...
源代码-gb2312和utf-8文件编码互换源码 v1.05.zip 源代码-gb2312和utf-8文件编码互换源码 v1.05.zip 源代码-gb2312和utf-8文件编码互换源码 v1.05.zip 源代码-gb2312和utf-8文件编码互换源码 v1.05.zip 源代码-gb...
文件编码转换(utf8与gbk相互转换) 可以将utf8(gbk)的文件转换成gbk(utf8)格式
本程序实现utf-8和gb2312编码互换 特色: 1、可以自定义要转换的文件扩展名 2、可以自定义要转换的文件的路径(相对,绝对) 3、自动识别编码格式 4、转换时自动将结果输出到指定的文件夹。不会覆盖原因的...
gb2312和utf-8文件编码互换工具
asp源码—gb2312和utf-8文件编码互换源码asp版 v1.05.zip asp源码—gb2312和utf-8文件编码互换源码asp版 v1.05.zip asp源码—gb2312和utf-8文件编码互换源码asp版 v1.05.zip asp源码—gb2312和utf-8文件编码互换源码...
本程序实现utf-8和gb2312编码互换 特色: 1、可以自定义要转换的文件扩展名 2、可以自定义要转换的文件的路径(相对,绝对) 3、自动识别编码格式 4、转换时自动将结果输出到指定的文件夹。不会覆盖原因的文件。...
三星手机电话本UTF-8与GB2312互换工具,方便阅读导出的UTF-8的VCF电话本文件,可以看到中文格式内容。也可以修改了再导回手机。不想用360或腾讯管家的可以用这个。有处理命令和示例可以参考
这是个可以实现UTF-8与GB2312之间的互换类,已封装成类,调用简单,使用方便!(推荐下载)
这就是 ASCII 编码的由来,ASCII 编码是美国信息互换标准代码的缩写。 然而,世界上其他国家的语言里有很多是 ASCII 里没有的,为了可以在计算机保存这些文字,人们决定采纳 127 号之后的空位来表示这些新的字母、...
用javascript实现gb2312转utf-8的脚本 本文讲解如何使用javascript实现gb2312编码到utf-8编码的转换。gb2312是中国国家标准的汉字编码字符集,共分两级,第一级为常用字,有3755字,第二级为次常用字,有3008字。...
描述了UTF-8与GB2312之间的互换方法
用来对代码文件格式进行转换,支持GB2312、GBK与UTF-8编码进行互换功能,资源来源于网络,请使用者遵守相关法律。
UTF-8 GB2312 UCS 码详细说名以及其转代码
编码互换工具 UTF-8_GB2312_BIG5,有三种编码:utf8、gb2312、big5,此软件可以快速的把不同编码的网页文件进行重新编码、可以说是一款小而强大的编码软件!
ASP源码—gb2312和utf-8文件编码互换源码 v1.05.zip
基于ASP的gb2312和utf-8文件编码互换源码 v1.05.zip
ASP源码,压缩包解压密码:www.cqlsoft.com
ASP实例开发源码—gb2312和utf-8文件编码互换源码asp版 v1.05.zip
自己写的VB.NET小工具, 输入数字/中英文字符,实现Unicode/GBK/UTF8对应的16进制编码,可选空格/0x/&H/H分隔字节,方便复制到程序中直接使用.