packagecom.rte.util;importorg.apache.tools.ant.Project;importorg.apache.tools.ant.taskdefs.Zip;importorg.apache.tools.ant.types.FileSet;importjava.io.*;importjava.nio.channels.FileChannel;importjava.text.DateFormat;importjava.text.MessageFormat;importjava.util.*;importjava.util.zip.ZipEntry;importjava.util.zip.ZipFile;/***文件操作工具类*Createdbyzybon16/1/8.*/publicclassFileUtil{/***创建目录**@paramdir要创建目录路径*@return创建成功返回true,目录已存在或失败返回false待创建*/publicstaticbooleancreateDirectory(Stringdir){Filef=newFile(dir);if(!f.exists()){f.mkdirs();returntrue;}returnfalse;}/***创建文件**@paramfileDirectoryAndName路径*@paramfileContentcontent*/publicstaticvoidcreateNewFile(StringfileDirectoryAndName,StringfileContent){try{//创建一个File对象,参数为String类型,表示目录名FilemyFile=newFile(fileDirectoryAndName);//判断文件是否存在,如果不存在,调用createNewFile()方法创建新目录,否则跳转到异常处理代码if(!myFile.exists())myFile.createNewFile();else//如果不存在,则抛出异常thrownewException("Thenewfilealreadyexists!");//将数据写入下面创建的文件中write(fileContent,fileDirectoryAndName);}catch(Exceptionex){System.out.println("Unabletocreateanewfile!");ex.printStackTrace();}}/***保存信息到指定文件**@paramphysicalPath保存文件物理路径*@paraminputStream目标文件的输入流*@return如果保存成功则返回true,否则返回false*/publicstaticbooleansaveFileByPhysicalDir(StringphysicalPath,InputStreaminputStream){booleanflag=false;try{OutputStreamos=newFileOutputStream(physicalPath);intreadBytes=0;bytebuffer[]=newbyte[8192];while((readBytes=inputStream.read(buffer,0,8192))!=-1){os.write(buffer,0,readBytes);}os.close();flag=true;}catch(FileNotFoundExceptione){e.printStackTrace();}catch(IOExceptione){e.printStackTrace();}returnflag;}/***保存字符串到指定路径**@paramphysicalPath保存物理路径*@paramcontentSavedString*/publicstaticvoidsaveAsFileOutputStream(StringphysicalPath,Stringcontent){Filefile=newFile(physicalPath);booleanb=file.getParentFile().isDirectory();if(!b){Filetem=newFile(file.getParent());tem.mkdirs();//创建目录}FileOutputStreamfoutput=null;try{foutput=newFileOutputStream(physicalPath);foutput.write(content.getBytes("UTF-8"));}catch(IOExceptionex){ex.printStackTrace();thrownewRuntimeException(ex);}finally{try{foutput.flush();foutput.close();}catch(IOExceptionex){ex.printStackTrace();thrownewRuntimeException(ex);}}}/***向文件添加信息(不会覆盖原文件内容)**@paramtivoliMsg信息到写入*@paramlogFileName目标文件*/publicstaticvoidwrite(StringtivoliMsg,StringlogFileName){try{byte[]bMsg=tivoliMsg.getBytes("UTF-8");FileOutputStreamfOut=newFileOutputStream(logFileName,true);fOut.write(bMsg);fOut。close();}catch(IOExceptione){}}/***日志写入*示例:*2016/01/0817:46:42:001:这是日志输出*2016/01/0817:46:55:001:这是日志输出。**@paramlogFile日志文件*@parambatchId处理数*@paramexceptionInfo异常信息*/publicstaticvoidwriteLog(StringlogFile,StringbatchId,StringexceptionInfo){DateFormatdf=DateFormat.getDateTimeInstance(DateFormat.DEFAULT,DateFormat.DEFAULT,Locale.JAPANESE);Objectargs[]={df.format(newDate()),batchId,exceptionInfo};StringfmtMsg=MessageFormat.format("{0}:{1}:{2}",args);try{Filelogfile=newFile(logFile);if(!logfile.exists()){logfile.createNewFile();}FileWriterfw=newFileWriter(logFile,true);fw.write(fmtMsg);fw.write(System.getProperty("line.separator"));fw.flush();fw.close();}catch(Exceptione){}}/***读取文件信息**@paramrealPath目标文件*@返回文件内容*/publicstaticStringreadTextFile(StringrealPath)throwsException{Filefile=newFile(realPath);if(!file.exists()){System.out.println("Filenotexist!");returnnull;}BufferedReaderbr=newBufferedReader(newInputStreamReader(newFileInputStream(realPath),"UTF-8"));Stringtemp="";Stringtxt="";while((temp=br.readLine())!=null){txt+=temp;}br.close();returntxt;}/***复制文件**@paramsrcFilesource文件路径*@paramtargetFiletarget文件路径*/publicstaticvoidcopyFile(StringsrcFile,StringtargetFile)throwsIOException{Filescrfile=newFile(srcFile);if(checkExist(srcFile)){FileInputStreamfi=null;FileOutputStreamfo=null;FileChannelin=null;FileChannelout=null;try{fi=newFileInputStream(srcFile);fo=newFileOutputStream(targetFile);in=fi.getChannel();out=fo.getChannel();in.transferTo(0,in.size(),out);}catch(IOExceptione){e.printStackTrace();}最后{try{fi.close();in.close();fo.close();out.close();}catch(IOExceptione){e.printStackTrace();}}}}/***复制文件夹**@paramsourceDirString源文件夹*@paramdestDirString目标路径*/publicstaticvoidcopyDir(StringsourceDir,StringdestDir){FilesourceFile=newFile(sourceDir);StringtempSource;StringtempDest;StringfileName;if(newFile(destDir).getParentFile().isDirectory()){newFile(destDir).mkdirs();}File[]files=sourceFile.listFiles();for(inti=0;i
