(資料圖片)
博主發(fā)表的文章,有的是自己原創(chuàng),有的是這些年本人從網(wǎng)上積累的,方便大家學(xué)習(xí)。
[[178775]]
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{/***創(chuàng)建目錄**@paramdir欲創(chuàng)建目錄路徑*@return創(chuàng)建成功返回true,目錄已存在或創(chuàng)建失敗返回false*/publicstaticbooleancreateDirectory(Stringdir){Filef=newFile(dir);if(!f.exists()){f.mkdirs();returntrue;}returnfalse;}/***創(chuàng)建文件**@paramfileDirectoryAndName路徑*@paramfileContent內(nèi)容*/publicstaticvoidcreateNewFile(StringfileDirectoryAndName,StringfileContent){try{//創(chuàng)建File對(duì)象,參數(shù)為String類型,表示目錄名FilemyFile=newFile(fileDirectoryAndName);//判斷文件是否存在,如果不存在則調(diào)用createNewFile()方法創(chuàng)建新目錄,否則跳至異常處理代碼if(!myFile.exists())myFile.createNewFile();else//如果不存在則扔出異常thrownewException("Thenewfilealreadyexists!");//下面把數(shù)據(jù)寫入創(chuàng)建的文件write(fileContent,fileDirectoryAndName);}catch(Exceptionex){System.out.println("無(wú)法創(chuàng)建新文件!");ex.printStackTrace();}}/***保存信息到指定文件**@paramphysicalPath保存文件物理路徑*@paraminputStream目標(biāo)文件的輸入流*@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保存物理路徑*@paramcontent欲保存的字符串*/publicstaticvoidsaveAsFileOutputStream(StringphysicalPath,Stringcontent){Filefile=newFile(physicalPath);booleanb=file.getParentFile().isDirectory();if(!b){Filetem=newFile(file.getParent());tem.mkdirs();//創(chuàng)建目錄}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);}}}/***向文件添加信息(不會(huì)覆蓋原文件內(nèi)容)**@paramtivoliMsg要寫入的信息*@paramlogFileName目標(biāo)文件*/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:這是一個(gè)日志輸出。*2016/01/0817:46:55:001:這是一個(gè)日志輸出。**@paramlogFile日志文件*@parambatchId處理編號(hào)*@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目標(biāo)文件*@return文件內(nèi)容*/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;}/***復(fù)制文件**@paramsrcFile源文件路徑*@paramtargetFile目標(biāo)文件路徑*/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();}finally{try{fi.close();in.close();fo.close();out.close();}catch(IOExceptione){e.printStackTrace();}}}}/***復(fù)制文件夾**@paramsourceDirString源文件夾*@paramdestDirString目標(biāo)路徑*/publicstaticvoidcopyDir(StringsourceDir,StringdestDir){FilesourceFile=newFile(sourceDir);StringtempSource;StringtempDest;StringfileName;if(newFile(destDir).getParentFile().isDirectory()){newFile(destDir).mkdirs();}File[]files=sourceFile.listFiles();for(inti=0;i1){for(inti=0;i 【本文是清一色專欄作者張勇波的原創(chuàng)文章,轉(zhuǎn)載請(qǐng)通過(guò)清一色獲取作者授權(quán)】
標(biāo)簽: