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