I am walking...

on the way...
posts - 0, comments - 0, trackbacks - 0, articles - 1
    这里首先是文件的生成:

 
 1 
 2 import java.io.*;
 3 
 4 public class Demo
 5 {
 6 public static void main( String[] args)
 7 {
 8 File dirFile;
 9 File tempFile;
10 boolean bFile;
11 String sFileName;
12 
13 bFile = false;
14 
15 try
16 {
17 dirFile = new File("E:\\test");
18 bFile = dirFile.exists();
19 
20 if( bFile == true )
21 {
22 System.out.println("The folder exists.");
23 }
24 else
25 {
26 System.out.println("The folder do not exist,now trying to create a one");
27 bFile = dirFile.mkdir();
28 if( bFile == true )
29 {
30 System.out.println("Create successfully!");
31 }
32 else
33 {
34 System.out.println("Disable to make the folder,please check the disk is full or not.");
35 System.exit(1);
36 }
37 }
38 
39 System.out.println("Now we put files in to the folder");
40 
41 for(int i=0; i<5; i++)
42 {
43 sFileName = new String("E:\\test\\");
44 sFileName += String.valueOf(i);
45 tempFile = new File(sFileName);
46 bFile = tempFile.createNewFile();
47 }
48 }catch(IOException e)
49 {
50 // Exception hadler
51 }
52 
53 if( bFile == true )
54 System.out.println("Successfully put files in to the folder!");
55 else
56 System.out.println("Sorry sir,i don't finish the job!");
57 }
58 }
59

   java删除文件夹时该文件夹必须是空的,写了以下2个方法,运用递归方法来删除欲删除的文件夹下的所有子文件夹以及文件。
在程序中需要删除的地方调用下面任一方法即可实现。
java 代码
   
 1 public void removeFile(String path) {
 2              this.removeFile(new File(path));
 3     }
 4 
 5     public void removeFile(File path){
 6         System.out.println("removing file " + path.getPath());
 7         if (path.isDirectory()){
 8             File[] child = path.listFiles();
 9             if (child != null && child.length != 0){
10                 for (int i = 0; i < child.length; i++){
11                     removeFile(child[i]);
12                     child[i].delete();
13                 }
14             }
15         }
16         path.delete();
17     }


只有注册用户登录后才能发表评论。


网站导航: