`
java-mans
  • 浏览: 11420122 次
文章分类
社区版块
存档分类
最新评论

java 文件及目录操作

 
阅读更多

importjava.io.*;­

publicclassFileOperate{­

publicFileOperate(){­

/**­

*新建目录­

*@paramfolderPathString如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();­

/**­

*新建文件­

*@paramfilePathAndNameString文件路径及名称如c:/fqf.txt­

*@paramfileContentString文件内容­

*@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();­

/**­

*删除文件­

*@paramfilePathAndNameString文件路径及名称如c:/fqf.txt­

*@paramfileContentString­

*@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();­

/**­

*删除文件夹­

*@paramfilePathAndNameString文件夹路径及名称如c:/fqf­

*@paramfileContentString­

*@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();­

/**­

*删除文件夹里面的所有文件­

*@parampathString文件夹路径如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]);//再删除空文件夹­

/**­

*复制单个文件­

*@paramoldPathString原文件路径如:c:/fqf.txt­

*@paramnewPathString复制后路径如: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();­

/**­

*复制整个文件夹内容­

*@paramoldPathString原文件路径如:c:/fqf­

*@paramnewPathString复制后路径如: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();­

/**­

*移动文件到指定目录­

*@paramoldPathString如:c:/fqf.txt­

*@paramnewPathString如:d:/fqf.txt­

*/­

publicvoidmoveFile(StringoldPath,StringnewPath){­

copyFile(oldPath,newPath);­

delFile(oldPath);­

/**­

*移动文件到指定目录­

*@paramoldPathString如:c:/fqf.txt­

*@paramnewPathString如:d:/fqf.txt­

*/­

publicvoidmoveFolder(StringoldPath,StringnewPath){­

copyFolder(oldPath,newPath);­

delFolder(oldPath);­

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics