ImageVerifierCode 换一换
格式:DOCX , 页数:7 ,大小:16.63KB ,
资源ID:8182622      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/8182622.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(JAVA文件操作类和文件夹的操作.docx)为本站会员(b****6)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

JAVA文件操作类和文件夹的操作.docx

1、JAVA文件操作类和文件夹的操作JAVA文件操作类和文件夹的操作 packagecom.gamvan.tools;importjava.io.BufferedReader;importjava.io.File;importjava.io.FileInputStream;importjava.io.FileOutputStream;importjava.io.FileWriter;importjava.io.IOException;importjava.io.InputStream;importjava.io.InputStreamReader;importjava.io.PrintWriter

2、;importjava.util.StringTokenizer;publicclassFileOperateprivateStringmessage;publicFileOperate()/*读取文本文件内容*paramfilePathAndName带有完整绝对路径的文件名*paramencoding文本文件打开的编码方式*return返回文本文件的内容*/publicStringreadTxt(StringfilePathAndName,Stringencoding)throwsIOExceptionencoding=encoding.trim();StringBufferstr=newS

3、tringBuffer();Stringst=;tryFileInputStreamfs=newFileInputStream(filePathAndName);InputStreamReaderisr;if(encoding.equals()isr=newInputStreamReader(fs);elseisr=newInputStreamReader(fs,encoding);BufferedReaderbr=newBufferedReader(isr);tryStringdata=;while(data=br.readLine()!=null)str.append(data+);cat

4、ch(Exceptione)str.append(e.toString();st=str.toString();catch(IOExceptiones)st=;returnst;/*新建目录*paramfolderPath目录*return返回目录创建后的路径*/publicStringcreateFolder(StringfolderPath)Stringtxt=folderPath;tryjava.io.FilemyFilePath=newjava.io.File(txt);txt=folderPath;if(!myFilePath.exists()myFilePath.mkdir();c

5、atch(Exceptione)message=创建目录操作出错;returntxt;/*多级目录创建*paramfolderPath准备要在本级目录下创建新目录的目录路径例如c:myf*parampaths无限级目录参数,各级目录以单数线区分例如a|b|c*return返回创建文件后的路径例如c:myfa_c*/publicStringcreateFolders(StringfolderPath,Stringpaths)Stringtxts=folderPath;tryStringtxt;txts=folderPath;StringTokenizerst=newStringTokenizer

6、(paths,|);for(inti=0;st.hasMoreTokens();i+)txt=st.nextToken().trim();if(txts.lastIndexOf(/)!=-1)txts=createFolder(txts+txt);elsetxts=createFolder(txts+txt+/);catch(Exceptione)message=创建目录操作出错!;returntxts;/*新建文件*paramfilePathAndName文本文件完整绝对路径及文件名*paramfileContent文本文件内容*return*/publicvoidcreateFile(St

7、ringfilePathAndName,StringfileContent)tryStringfilePath=filePathAndName;filePath=filePath.toString();FilemyFilePath=newFile(filePath);if(!myFilePath.exists()myFilePath.createNewFile();FileWriterresultFile=newFileWriter(myFilePath);PrintWritermyFile=newPrintWriter(resultFile);StringstrContent=fileCon

8、tent;myFile.println(strContent);myFile.close();resultFile.close();catch(Exceptione)message=创建文件操作出错;/*有编码方式的文件创建*paramfilePathAndName文本文件完整绝对路径及文件名*paramfileContent文本文件内容*paramencoding编码方式例如GBK或者UTF-8*return*/publicvoidcreateFile(StringfilePathAndName,StringfileContent,Stringencoding)tryStringfilePa

9、th=filePathAndName;filePath=filePath.toString();FilemyFilePath=newFile(filePath);if(!myFilePath.exists()myFilePath.createNewFile();PrintWritermyFile=newPrintWriter(myFilePath,encoding);StringstrContent=fileContent;myFile.println(strContent);myFile.close();catch(Exceptione)message=创建文件操作出错;/*删除文件*par

10、amfilePathAndName文本文件完整绝对路径及文件名*returnBoolean成功删除返回true遭遇异常返回false*/publicbooleandelFile(StringfilePathAndName)booleanbea=false;tryStringfilePath=filePathAndName;FilemyDelFile=newFile(filePath);if(myDelFile.exists()myDelFile.delete();bea=true;elsebea=false;message=(filePathAndName+删除文件操作出错);catch(Ex

11、ceptione)message=e.toString();returnbea;/*删除文件夹*paramfolderPath文件夹完整绝对路径*return*/publicvoiddelFolder(StringfolderPath)trydelAllFile(folderPath);/删除完里面所有内容StringfilePath=folderPath;filePath=filePath.toString();java.io.FilemyFilePath=newjava.io.File(filePath);myFilePath.delete();/删除空文件夹catch(Exception

12、e)message=(删除文件夹操作出错);/*删除指定文件夹下所有文件*parampath文件夹完整绝对路径*return*return*/publicbooleandelAllFile(Stringpath)booleanbea=false;Filefile=newFile(path);if(!file.exists()returnbea;if(!file.isDirectory()returnbea;StringtempList=file.list();Filetemp=null;for(inti=0;itempList.length;i+)if(path.endsWith(File.s

13、eparator)temp=newFile(path+tempListi);elsetemp=newFile(path+File.separator+tempListi);if(temp.isFile()temp.delete();if(temp.isDirectory()delAllFile(path+/+tempListi);/先删除文件夹里面的文件delFolder(path+/+tempListi);/再删除空文件夹bea=true;returnbea;/*复制单个文件*paramoldPathFile准备复制的文件源*paramnewPathFile拷贝到新绝对路径带文件名*retu

14、rn*/publicvoidcopyFile(StringoldPathFile,StringnewPathFile)tryintbytesum=0;intbyteread=0;Fileoldfile=newFile(oldPathFile);if(oldfile.exists()/文件存在时InputStreaminStream=newFileInputStream(oldPathFile);/读入原文件FileOutputStreamfs=newFileOutputStream(newPathFile);bytebuffer=newbyte1444;while(byteread=inStr

15、eam.read(buffer)!=-1)bytesum+=byteread;/字节数文件大小System.out.println(bytesum);fs.write(buffer,0,byteread);inStream.close();catch(Exceptione)message=(复制单个文件操作出错);/*复制整个文件夹的内容*paramoldPath准备拷贝的目录*paramnewPath指定绝对路径的新目录*return*/publicvoidcopyFolder(StringoldPath,StringnewPath)trynewFile(newPath).mkdirs();

16、/如果文件夹不存在则建立新文件夹Filea=newFile(oldPath);Stringfile=a.list();Filetemp=null;for(inti=0;ifile.length;i+)if(oldPath.endsWith(File.separator)temp=newFile(oldPath+filei);elsetemp=newFile(oldPath+File.separator+filei);if(temp.isFile()FileInputStreaminput=newFileInputStream(temp);FileOutputStreamoutput=newFi

17、leOutputStream(newPath+/+(temp.getName().toString();byteb=newbyte1024*5;intlen;while(len=input.read(b)!=-1)output.write(b,0,len);output.flush();output.close();input.close();if(temp.isDirectory()/如果是子文件夹copyFolder(oldPath+/+filei,newPath+/+filei);catch(Exceptione)message=复制整个文件夹内容操作出错;/*移动文件*paramoldPath*paramnewPath*return*/publicvoidmoveFile(StringoldPath,StringnewPath)copyFile(oldPath,newPath);delFile(oldPath);/*移动目录*paramoldPath*paramnewPath*return*/publicvoidmoveFolder(StringoldPath,StringnewPath)copyFolder(oldPath,newPath);delFolder(oldPath);publicStringgetMessage()returnthis.message;

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1