java复制文件并转码_java 读写文件,转码

packageutils;importorg.mozilla.intl.chardet.nsDetector;importorg.mozilla.intl.chardet.nsICharsetDetectionObserver;import java.io.*;importjava.util.ArrayList;importjava.util.List;public classGbk2utf8 {public static voidgbk2Utf8(String fileName) {

BufferedReader reader= null;

BufferedWriter writer= null;try{

StringBuffer sb= newStringBuffer();

reader= new BufferedReader(new InputStreamReader(new FileInputStream(fileName), "GBK"));

writer= new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileName+"tmp"), "UTF-8"));

String str;while ((str = reader.readLine()) != null) {

sb.append(str).append("\r\n");

writer.write(sb.toString());

}

}catch(IOException e) {

e.printStackTrace();

}finally{if (reader != null) {try{

reader.close();

}catch(IOException e) {

e.printStackTrace();

}

}if (writer != null) {try{

writer.close();

}catch(IOException e) {

e.printStackTrace();

}

}

}

}/*** 递归获取指定目录下所有指定类型文件

*

*@paramstrPath

* 文件夹地址

*@paramsuffix

* 文件名后缀

*@return

*/

public static ListgetFileList(String strPath, String suffix) {

List filelist = new ArrayList();

getFileList(strPath, suffix, filelist);returnfilelist;

}public static void getFileList(String strPath, String suffix,ListfileList) {

File dir= newFile(strPath);

File[] files= dir.listFiles(); //该文件目录下文件全部放入数组

if (files != null) {for (int i = 0; i < files.length; i++) {

String fileName=files[i].getName();if (files[i].isDirectory()) { //如果是文件夹就递归调用

getFileList(files[i].getAbsolutePath(), suffix,fileList);

}else if(fileName.endsWith(suffix)) {

fileList.add(files[i]);

}

}

}

}//是否找到匹配字符集

private static boolean isFind = false;//如果完全匹配某个字符集检测算法, 则该属性保存该字符集的名称. 否则(如二进制文件)其值就为默认值 null

private static String encoding = null;/*** 获取文件的编码

*

*@paramfile

*@return文件编码,若无,则返回null

*@throwsIOException*/

private static String guessFileCharset(File file) throwsIOException {

nsDetector det= newnsDetector();

det.Init(newnsICharsetDetectionObserver() {public voidNotify(String charset) {

isFind= true;

encoding=charset;

}

});

BufferedInputStream bis= new BufferedInputStream(newFileInputStream(file));byte[] buf = new byte[1024];intlen;boolean done = false;boolean isAscii = true;while ((len = bis.read(buf, 0, buf.length)) != -1) {if(isAscii) {

isAscii=det.isAscii(buf, len);

}else if (!done) {

done= det.DoIt(buf, len, false);

}

}

det.DataEnd();if(isAscii) {

encoding= "ASCII";

isFind= true;

}else if (!isFind) {

String prob[]=det.getProbableCharsets();if (prob.length > 0) {

encoding= prob[0]; //在没有发现情况下,则取第一个可能的编码

}

}returnencoding;

}public static voidmain(String[] args) {

List files = getFileList("F:\\ppppppppp", ".sql");for (int i = 0; i < files.size(); i++) {

String charset= null;try{

System.out.print("正在获取第"+i+"个文件的编码"+"==="+files.get(i).getAbsoluteFile());

charset=guessFileCharset(files.get(i).getAbsoluteFile());

}catch(IOException e) {

System.err.println("获取文件编码发生异常!");

}

System.out.println("====="+files.get(i).getAbsoluteFile() + "[" + charset + "]");if ("GB2312".equals(charset)) {

System.out.println("正在获取第"+i+"个文件的转化"+"==="+files.get(i).getAbsoluteFile());

gbk2Utf8(files.get(i).getAbsolutePath());

files.get(i).delete();

}

}

System.out.println("文件总数:"+files.size());

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值