您好,欢迎访问三七文档
当前位置:首页 > IT计算机/网络 > 数据库 > java文件操作大全
1.创建文件夹//importjava.io.*;FilemyFolderPath=newFile(%%1);try{if(!myFolderPath.exists()){myFolderPath.mkdir();}}catch(Exceptione){System.out.println(新建目录操作出错);e.printStackTrace();}2.创建文件//importjava.io.*;FilemyFilePath=newFile(%%1);try{if(!myFilePath.exists()){myFilePath.createNewFile();}FileWriterresultFile=newFileWriter(myFilePath);PrintWritermyFile=newPrintWriter(resultFile);myFile.println(%%2);myFile.flush();resultFile.close();}catch(Exceptione){System.out.println(新建文件操作出错);e.printStackTrace();}3.删除文件//importjava.io.*;FilemyDelFile=newFile(%%1);try{myDelFile.delete();}catch(Exceptione){System.out.println(删除文件操作出错);e.printStackTrace();}4.删除文件夹/*importjava.io.*;importjava.util.*;*/LinkedListfolderList=newLinkedListString();folderList.add(%%1);while(folderList.size()0){Filefile=newFile(folderList.poll());File[]files=file.listFiles();ArrayListFilefileList=newArrayListFile();for(inti=0;ifiles.length;i++){if(files[i].isDirectory()){folderList.add(files[i].getPath());}else{fileList.add(files[i]);}}for(Filef:fileList){f.delete();}}folderList=newLinkedListString();folderList.add(%%1);while(folderList.size()0){Filefile=newFile(folderList.getLast());if(file.delete()){folderList.removeLast();}else{File[]files=file.listFiles();for(inti=0;ifiles.length;i++){folderList.add(files[i].getPath());}}}5.删除一个文件下夹所有的文件夹/*importjava.io.*;privatestaticLinkedListStringfolderList=null;*/Filedelfile=newFile(%%1);File[]files=delfile.listFiles();for(inti=0;ifiles.length;i++){if(files[i].isDirectory()){if(!files[i].delete()){folderList=newLinkedListString();folderList.add(files[i]);while(folderList.size()0){Filefile=newFile(folderList.poll());File[]files=file.listFiles();ArrayListFilefileList=newArrayListFile();for(inti=0;ifiles.length;i++){if(files[i].isDirectory()){folderList.add(files[i].getPath());}else{fileList.add(files[i]);}}for(Filef:fileList){f.delete();}}folderList=newLinkedListString();folderList.add(files[i]);while(folderList.size()0){Filefile=newFile(folderList.getLast());if(file.delete()){folderList.removeLast();}else{File[]files=file.listFiles();for(inti=0;ifiles.length;i++){folderList.add(files[i].getPath());}}}}}}6.清空文件夹//importjava.io.*;Filedelfilefolder=newFile(%%1);try{if(!delfilefolder.exists()&&!delfilefolder.delete()){LinkedListfolderList=newLinkedListString();folderList.add(delfilefolder);while(folderList.size()0){Filefile=newFile(folderList.poll());File[]files=file.listFiles();ArrayListFilefileList=newArrayListFile();for(inti=0;ifiles.length;i++){if(files[i].isDirectory()){folderList.add(files[i].getPath());}else{fileList.add(files[i]);}}for(Filef:fileList){f.delete();}}folderList=newLinkedListString();folderList.add(delfilefolder);while(folderList.size()0){Filefile=newFile(folderList.getLast());if(file.delete()){folderList.removeLast();}else{File[]files=file.listFiles();for(inti=0;ifiles.length;i++){folderList.add(files[i].getPath());}}}}delfilefolder.mkdir();}catch(Exceptione){System.out.println(清空目录操作出错);e.printStackTrace();}7.读取文件//importjava.io.*;//逐行读取数据FileReaderfr=newFileReader(%%1);BufferedReaderbr=newBufferedReader(fr);String%%2=br.readLine();while(%%2!=null){%%3%%2=br.readLine();}br.close();fr.close();8.写入文件//importjava.io.*;//将数据写入文件try{FileWriterfw=newFileWriter(%%1);fw.write(%%2);fw.flush();fw.close();}catch(IOExceptione){e.printStackTrace();}9.写入随机文件//importjava.io.*;try{RandomAcessFilelogFile=newRandomAcessFile(%%1,rw);longlg=logFile.length();logFile.seek(%%2);logFile.writeByte(%%3);}catch(IOExceptionioe){System.out.println(无法写入文件:+ioe.getMessage());}10.读取文件属性//importjava.io.*;//文件属性的取得Fileaf=newFile(%%1);if(af.exists()){System.out.println(f.getName()+的属性如下:文件长度为:+f.length());System.out.println(f.isFile()?是文件:不是文件);System.out.println(f.isDirectory()?是目录:不是目录);System.out.println(f.canRead()?可读取:不);System.out.println(f.canWrite()?是隐藏文件:);System.out.println(文件夹的最后修改日期为:+newDate(f.lastModified()));}else{System.out.println(f.getName()+的属性如下:);System.out.println(f.isFile()?是文件:不是文件);System.out.println(f.isDirectory()?是目录:不是目录);System.out.println(f.canRead()?可读取:不);System.out.println(f.canWrite()?是隐藏文件:);System.out.println(文件的最后修改日期为:+newDate(f.lastModified()));}if(f.canRead()){%%2}if(f.canWrite()){%%3}11.写入属性//importjava.io.*;Filefilereadonly=newFile(%%1);try{booleanb=filereadonly.setReadOnly();}catch(Exceptione){System.out.println(拒绝写访问:+e.printStackTrace());}12.枚举一个文件夹中的所有文件/*importjava.io.*;importjava.util.*;*/LinkedListStringfolderList=newLinkedListString();folderList.add(%%1);while(folderList.size()0){Filefile=newFile(folderList.poll());File[]files=file.listFiles();ListFilefileList=newArrayListFile();for(inti=0;ifiles.length;i++){if(files[i].isDirectory()){folderList.add(files[i].getPath());}else{fileList.add(files[i]);}}for(Filef:fileList){%%2=f.getAbsoluteFile();%%3}}13.复制文件夹/*importjava.io.*;importjava.util.*;*/LinkedListStringfolderList=newLinkedListString();folderList.add(%%1);LinkedListStringfolderList2=newLinkedListString();folderList
本文标题:java文件操作大全
链接地址:https://www.777doc.com/doc-6179865 .html