您的位置:首页 > 编程语言 > Java开发

java程序 一次改变指定目录下所有文件编码(包括子目录中的文件)

2016-02-01 13:23 731 查看
package transCoding;

import java.io.BufferedReader;

import java.io.BufferedWriter;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.FilenameFilter;

import java.io.IOException;

import java.io.InputStreamReader;

import java.io.OutputStreamWriter;

public class TransCoding {

//需要转编码的目录

private static String path = “d:/action”;

//定义另存目录。若不赋值(但此属性的值必须为字符串,如”“,不能为null)

//即当前目录下文件转变编码后会覆盖,包括子目录及其文件

private static String directory = “d:/abc”;

//定义要转的文件类型。若不赋值(但此属性的值必须为字符串,如”“,不能为null)

//即转变当前目录下所有类型文件,包括子目录及其文件。

private static String typeName = “”;

//要转的文件当前编码,默认GBK。因为一般硬盘文件默认ANSI,在中国即是GBK编码。

private static String codeBefore = “GBK”;

//要转的文件转后编码,默认UTF-8。

//需要注意的是如果被编码的文件是全英文的,没有汉字,那么即使下面指定UTF-8,

//有些计算机也不能生成UTF-8文件,可能与windows的转码有关,但全英文不影响性能。

private static String codeAfter = “UTF-8”;

public static void main(String[] args){
File dir = new File(path);
File dirPath = new File(directory);
change(dir,dirPath,typeName);

}

public static void change(File dir,File dirPath,final String typeName){
BufferedReader bufferedReader = null;
BufferedWriter bufferedWriter =null;
//获得当前目录下的所有符合条件的对象,包括目录。
File[]   files = dir.listFiles(new FilenameFilter(){
public boolean accept(File dir,String name){
//如果是目录,直接返回true,意味着可以直接加入listFiles方法里的集合中。
if(new File(dir,name).isDirectory()){
return true;
};
return name.endsWith(typeName);
}
});

for (File file : files) {
//如果没有指定另存目录名,此为当前目录的绝对路径
String pathAbsolute = null;
//如果没有指定另存目录名,此为当前文件编码后的绝对路径
File fileModify = null;
//定义另存目录对象
File createDir = null;
//定义另存目录中的文件对象
File createFile = null;
//如果当前file对象是一个目录,再调用此方法,递归。
if(file.isDirectory()){
//获取此目录绝对路径
String path = file.getAbsolutePath();
//截掉当前目录
String path2 = path.substring(0,path.lastIndexOf("\\"));
//获取到上级目录
String path3 = path2.substring(path2.lastIndexOf("\\"));
//封装成对象方便传递。
File file2 =null;
if(dirPath.getName()==null||dirPath.getName().trim()==""){
change(file,dirPath,typeName);
}else{
file2 = new File(dirPath,path3);
change(file,file2,typeName);
}

}else{//不是目录,直接转码
try {
//读取目录下文件,并按指定编码读取。
bufferedReader = new BufferedReader(new InputStreamReader(
new FileInputStream(file.getAbsolutePath()),codeBefore));
//如果另存目录为空,表示存放到当前目录。
if(dirPath.getName()==null||dirPath.getName()==""){
//绝对路径名 如 D:\action\1.txt
pathAbsolute = file.getAbsolutePath();
//截取后的路径 如D:\action\
String path1 = pathAbsolute.substring(0,
pathAbsolute.lastIndexOf(file.getName()));
//新路径 如 D:\action\11.txt
String pathModify = path1+"1"+file.getName();
fileModify = new File(pathModify);
bufferedWriter = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream(fileModify),codeAfter));
}else{//否则,将转码后的文件写入指定的目录

String path = file.getAbsolutePath();
String fileName = file.getName();
//获取文件所在的绝对路径目录
String path2 = file.getAbsolutePath().substring(0,
path.lastIndexOf(fileName)-1);
//获取文件所在的上一级目录
String path3 = path2.substring(path2.lastIndexOf("\\"));
//创建另存目录
createDir = new File(dirPath,path3);
//这里是创建多级目录,防止条件转码时(例如后缀名为.java或.txt),
//符合条件的文件太深,造成目录创建失败,导致IO写入异常。
createDir.mkdirs();
createFile = new File(createDir,fileName);
bufferedWriter = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream(createFile),codeAfter));
}
String line = null;
while((line=bufferedReader.readLine())!=null){
bufferedWriter.write(line);
bufferedWriter.newLine();
}
//我觉得这里写不写都一样,最终关闭流的时候也会刷新。
//如果写入while循环里会降低效率,每行都要刷新。
//因为那样写入磁盘的次数就增多了
bufferedWriter.flush();

} catch (Exception e) {
if(createDir!=null)
createDir.deleteOnExit();
if(createFile!=null)
createFile.deleteOnExit();
throw new RuntimeException("读写错误"+e);
}
finally{
if(bufferedReader!=null){
try {
bufferedReader.close();

} catch (IOException e) {
throw new RuntimeException("输入流关闭错误");
}
}
if(bufferedWriter!=null){
try {
bufferedWriter.close();
if(fileModify!=null){
//将原文件删除
file.delete();
//创建一个和原文件同名的File对象
File file3 = new File(pathAbsolute);
//将编码后的文件名改成原文件名
fileModify.renameTo(file3);
}
} catch (IOException e) {
throw new RuntimeException("输出流关闭错误");
}
}
}

}
}
}


}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  编码 java