首页 > 快讯 >

每日信息:JAVA文件操作工具类

2023-05-04 20:16:39 来源:清一色财经


(相关资料图)

博主发表的文章,有的是自己原创,有的是这些年本人从网上积累的,方便大家学习。

[[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{/***创建目录**@paramdir欲创建目录路径*@return创建成功返回true,目录已存在或创建失败返回false*/publicstaticbooleancreateDirectory(Stringdir){Filef=newFile(dir);if(!f.exists()){f.mkdirs();returntrue;}returnfalse;}/***创建文件**@paramfileDirectoryAndName路径*@paramfileContent内容*/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("无法创建新文件!");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保存物理路径*@paramcontent欲保存的字符串*/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目标文件*@return文件内容*/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;}/***复制文件**@paramsrcFile源文件路径*@paramtargetFile目标文件路径*/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();}}}}/***复制文件夹**@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;i1){for(inti=0;i

【本文是清一色专栏作者张勇波的原创文章,转载请通过清一色获取作者授权】

标签:
x 广告
x 广告

Copyright ©  2015-2022 纵横创投网版权所有  备案号:浙ICP备2022016517号-12   联系邮箱:51 46 76 11 3 @qq.com

返回顶部