欢迎来到天天文库
浏览记录
ID:6031826
大小:51.50 KB
页数:6页
时间:2017-12-31
《java 文件编码转换》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、java文件编码转换packagecom.folkSeal.util;importjava.io.File;importjava.io.FileInputStream;importjava.io.FileNotFoundException;importjava.io.FileOutputStream;importjava.io.IOException;importjava.io.UnsupportedEncodingException;importjava.nio.ByteBuffer;importj
2、ava.nio.channels.FileChannel;importjava.nio.charset.Charset;/**IO工具类*/publicclassIOCVUtils{/**源文件编码*/publicstaticStringsourceEncoding="GBK";/**目标编码*/publicstaticStringtargetEncoding="UTF-8";/***文件内容转编码*@paramsourceFile*@paramtargetFile*@throwsUnsupporte
3、dEncodingException*@throwsFileNotFoundException*@throwsIOException*/publicstaticvoidchangeEncoding(FilesourceFile,FiletargetFile)throwsUnsupportedEncodingException,FileNotFoundException,IOException{FileInputStreamfin=null;FileOutputStreamfout=null;FileC
4、hannelfcin=null;FileChannelfcout=null;if(sourceEncoding==null){IOCVUtils.sourceEncoding=System.getProperty("file.encoding");}try{fin=newFileInputStream(sourceFile);fout=newFileOutputStream(targetFile);fcin=fin.getChannel();fcout=fout.getChannel();ByteBu
5、fferbuffer=ByteBuffer.allocateDirect(1024);while(true){buffer.clear();intr=fcin.read(buffer);if(r==-1){break;}buffer.flip();fcout.write(ByteBuffer.wrap(Charset.forName(sourceEncoding).decode(buffer).toString().getBytes(targetEncoding)));}}finally{if(fin
6、!=null){fin.close();fin=null;}if(fcin!=null){fcin.close();fcin=null;}if(fout!=null){fout.close();fout=null;}if(fcout!=null){fcout.close();fcout=null;}}}/***文件内容转编码*@paramsourceFile*@paramtargetFile*@throwsUnsupportedEncodingException*@throwsFileNotFound
7、Exception*@throwsIOException*/publicstaticvoidchangeEncoding(StringsourceFile,StringtargetFile)throwsUnsupportedEncodingException,FileNotFoundException,IOException{Filefl1=newFile(sourceFile);Filefo1=newFile(targetFile);changeEncoding(fl1,fo1);}/***文件内容
8、转编码*@paramsourceFile*@paramtargetFile*@paramsourceEncoding源文件编码默认源文件的系统存储编码System.getProperty("file.encoding");*@paramtargetEncoding目标编码默认utf-8*@throwsUnsupportedEncodingException*@throwsFileNotFoundException*@throwsIOException*/
此文档下载收益归作者所有