JAVA文件操作相关:
- packagecom.bytecode.openexcel.util;
- importjava.io.*;
- publicclassFileOperate{
- publicFileOperate(){
- }
- /**
- *新建目录
- *
- *@paramfolderPath
- *String如c:/fqf
- *@returnboolean
- */
- publicvoidnewFolder(StringfolderPath){
- try{
- StringfilePath=folderPath;
- filePath=filePath.toString();
- java.io.FilemyFilePath=newjava.io.File(filePath);
- if(!myFilePath.exists()){
- myFilePath.mkdir();
- }
- }catch(Exceptione){
- System.out.println("新建目录操作出错");
- e.printStackTrace();
- }
- }
- /**
- *新建文件
- *
- *@paramfilePathAndName
- *String文件路径及名称如c:/fqf.txt
- *@paramfileContent
- *String文件内容
- *@returnboolean
- */
- publicvoidnewFile(StringfilePathAndName,StringfileContent){
- try{
- StringfilePath=filePathAndName;
- filePath=filePath.toString();
- FilemyFilePath=newFile(filePath);
- if(!myFilePath.exists()){
- myFilePath.createNewFile();
- }
- FileWriterresultFile=newFileWriter(myFilePath);
- PrintWritermyFile=newPrintWriter(resultFile);
- StringstrContent=fileContent;
- myFile.println(strContent);
- resultFile.close();
- }catch(Exceptione){
- System.out.println("新建文件操作出错");
- e.printStackTrace();
- }
- }
- /**
- *删除文件
- *
- *@paramfilePathAndName
- *String文件路径及名称如c:/fqf.txt
- *@paramfileContent
- *String
- *@returnboolean
- */
- publicvoiddelFile(StringfilePathAndName){
- try{
- StringfilePath=filePathAndName;
- filePath=filePath.toString();
- java.io.FilemyDelFile=newjava.io.File(filePath);
- myDelFile.delete();
- }catch(Exceptione){
- System.out.println("删除文件操作出错");
- e.printStackTrace();
- }
- }
- /**
- *删除文件夹
- *
- *@paramfilePathAndName
- *String文件夹路径及名称如c:/fqf
- *@paramfileContent
- *String
- *@returnboolean
- */
- publicvoiddelFolder(StringfolderPath){
- try{
- delAllFile(folderPath);//删除完里面所有内容
- StringfilePath=folderPath;
- filePath=filePath.toString();
- java.io.FilemyFilePath=newjava.io.File(filePath);
- myFilePath.delete();//删除空文件夹
- }catch(Exceptione){
- System.out.println("删除文件夹操作出错");
- e.printStackTrace();
- }
- }
- /**
- *删除文件夹里面的所有文件
- *
- *@parampath
- *String文件夹路径如c:/fqf
- */
- publicvoiddelAllFile(Stringpath){
- Filefile=newFile(path);
- if(!file.exists()){
- return;
- }
- if(!file.isDirectory()){
- return;
- }
- String[]tempList=file.list();
- Filetemp=null;
- for(inti=0;i<tempList.length;i++){
- if(path.endsWith(File.separator)){
- temp=newFile(path+tempList[i]);
- }else{
- temp=newFile(path+File.separator+tempList[i]);
- }
- if(temp.isFile()){
- temp.delete();
- }
- if(temp.isDirectory()){
- delAllFile(path+"/"+tempList[i]);//先删除文件夹里面的文件
- delFolder(path+"/"+tempList[i]);//再删除空文件夹
- }
- }
- }
- /**
- *复制单个文件
- *
- *@paramoldPath
- *String原文件路径如:c:/fqf.txt
- *@paramnewPath
- *String复制后路径如:f:/fqf.txt
- *@returnboolean
- */
- publicvoidcopyFile(StringoldPath,StringnewPath){
- try{
- intbytesum=0;
- intbyteread=0;
- Fileoldfile=newFile(oldPath);
- if(oldfile.exists()){//文件存在时
- InputStreaminStream=newFileInputStream(oldPath);//读入原文件
- FileOutputStreamfs=newFileOutputStream(newPath);
- byte[]buffer=newbyte[1444];
- intlength;
- while((byteread=inStream.read(buffer))!=-1){
- bytesum+=byteread;//字节数文件大小
- System.out.println(bytesum);
- fs.write(buffer,0,byteread);
- }
- inStream.close();
- }
- }catch(Exceptione){
- System.out.println("复制单个文件操作出错");
- e.printStackTrace();
- }
- }
- /**
- *复制整个文件夹内容
- *
- *@paramoldPath
- *String原文件路径如:c:/fqf
- *@paramnewPath
- *String复制后路径如:f:/fqf/ff
- *@returnboolean
- */
- publicvoidcopyFolder(StringoldPath,StringnewPath){
- try{
- (newFile(newPath)).mkdirs();//如果文件夹不存在则建立新文件夹
- Filea=newFile(oldPath);
- String[]file=a.list();
- Filetemp=null;
- for(inti=0;i<file.length;i++){
- if(oldPath.endsWith(File.separator)){
- temp=newFile(oldPath+file[i]);
- }else{
- temp=newFile(oldPath+File.separator+file[i]);
- }
- if(temp.isFile()){
- FileInputStreaminput=newFileInputStream(temp);
- FileOutputStreamoutput=newFileOutputStream(newPath
- +"/"+(temp.getName()).toString());
- byte[]b=newbyte[1024*5];
- intlen;
- while((len=input.read(b))!=-1){
- output.write(b,0,len);
- }
- output.flush();
- output.close();
- input.close();
- }
- if(temp.isDirectory()){//如果是子文件夹
- copyFolder(oldPath+"/"+file[i],newPath+"/"
- +file[i]);
- }
- }
- }catch(Exceptione){
- System.out.println("复制整个文件夹内容操作出错");
- e.printStackTrace();
- }
- }
- /**
- *移动文件到指定目录
- *
- *@paramoldPath
- *String如:c:/fqf.txt
- *@paramnewPath
- *String如:d:/fqf.txt
- */
- publicvoidmoveFile(StringoldPath,StringnewPath){
- copyFile(oldPath,newPath);
- delFile(oldPath);
- }
- /**
- *移动文件到指定目录
- *
- *@paramoldPath
- *String如:c:/fqf.txt
- *@paramnewPath
- *String如:d:/fqf.txt
- */
- publicvoidmoveFolder(StringoldPath,StringnewPath){
- copyFolder(oldPath,newPath);
- delFolder(oldPath);
- }
- }