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;importjava.nio.channels.FileChannel;importjava.nio.charset.Charset;/**IO工具类*/publicclassIOCVUtils{/**源文件编码*/publicstaticStringsourceEncoding="GBK";/**目标编码*/publicstaticStringtargetEncoding="UTF-8";/***文件内容转编码*@paramsourceFile*@paramtargetFile*@throwsUnsupportedEncodingException*@throwsFileNotFoundException*@throwsIOException*/publicstaticvoidchangeEncoding(FilesourceFile,FiletargetFile)throwsUnsupportedEncodingException,FileNotFoundException,IOException{FileInputStreamfin=null;FileOutputStreamfout=null;FileChannelfcin=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();ByteBufferbuffer=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!=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*@throwsFileNotFoundException*@throwsIOException*/publicstaticvoidchangeEncoding(StringsourceFile,StringtargetFile)throwsUnsupportedEncodingException,FileNotFoundException,IOException{Filefl1=newFile(sourceFile);Filefo1=newFile(targetFile);changeEncoding(fl1,fo1);}/***文件内容转编码*@paramsourceFile*@paramtargetFile*@paramsourceEncoding源文件编码默认源文件的系统存储编码System.getProperty("file.encoding");*@paramtargetEncoding目标编码默认utf-8*@throwsUnsupportedEncodingException*@throwsFileNotFoundException*@throwsIOException*/publicstaticvoidchangeEncoding(StringsourceFile,StringtargetFile,StringsourceEncoding,StringtargetEncoding)throwsUnsupportedEncodingException,FileNotFoundException,IOException{IOCVUtils.sourceEncoding=sourceEncoding;IOCVUtils.targetEncoding=targetEncoding;changeEncoding(sourceFile,targetFile);}}或者packagecom.folkSeal.test;importjava.io.BufferedReader;importjava.io.IOException;importjava.io.InputStream;importjava.io.InputStreamReader;publicclassStreamGobblerextendsThread{ InputStreamis; Stringtype; StreamGobbler(InputStreamis,Stringtype){ this.is=is; this.type=type; } publicvoidrun(){ try{ InputStreamReaderisr=newInputStreamReader(is); BufferedReaderbr=newBufferedReader(isr); Stringline=null; while((line=br.readLine())!=null) System.out.println(type">"line); }catch(IOExceptionioe){ ioe.printStackTrace(); } } } packagecom.folkSeal.test;importjava.io.File;/***用于文件夹下的java文件的编码自动转换,如gbk转utf-8**@authorWenFuqiang*@companyFayhongTechnologyCo.,Ltd.*@date2010-1-26*/publicclassEncodingConverter{/***main方法入口**@paramargs* args[0]给定需要转换的文件夹args[1]指定需要转换的编码,如utf-8等*/publicstaticvoidmain(String[]args){//if(args.length<2){//System.out.println("pleaseinputpathoffolderandencodingname");//System.exit(1);//}//else{//ec.convertEncode(args[0],args[1]);//}EncodingConverterec=newEncodingConverter();//暂时用src_path替换args[0],encoding_name替换arg[1]Stringsrc_path="E:\\Workspaces\\folkSeal\\WebRoot\\file\\seal_templet_file\\xml\\2\\2\\";Stringencoding_name="utf-8";ec.convertEncode(src_path,encoding_name);}publicvoidconvertEncode(StringsourceFloder,Stringencoding_name){Filefile=newFile(sourceFloder);String[]files=file.list();for(Strings:files){if(s.indexOf('.')==-1){//
表
关于同志近三年现实表现材料材料类招标技术评分表图表与交易pdf视力表打印pdf用图表说话 pdf
明这是个子目录,回归调用此函数convertEncode(file.getAbsolutePath()"\\"s,encoding_name);}else{if(s.endsWith("xml")){//只处理以Java结尾的文件doConvertEncode(file.getAbsolutePath()"\\"s,file.getAbsolutePath()"\\"s,encoding_name);}}}}/***完成具体的编码转换工作**@paraminputFile* 输入文件*@paramoutputFile* 输出文件*@paramencoding_name* 需要转成的编码格式*/publicvoiddoConvertEncode(StringinputFile,StringoutputFile,Stringencoding_name){Runtimert=Runtime.getRuntime();Stringcmd[]={"native2ascii.exe","-reverse","-encoding",encoding_name,inputFile,outputFile};System.out.println("Execingconvertcommandfor"inputFile"...");try{Processproc=rt.exec(cmd);//anyerrormessage?StreamGobblererrorGobbler=newStreamGobbler(proc.getErrorStream(),"ERROR");//anyoutput?StreamGobbleroutputGobbler=newStreamGobbler(proc.getInputStream(),"OUTPUT");//kickthemofferrorGobbler.start();outputGobbler.start();//anyerror???intexitVal=proc.waitFor();System.out.println("ExitValue:"exitVal);}catch(Throwablee){e.printStackTrace();}}}