Posted on 2009-04-11 01:21 
笑看风云 阅读(332) 
评论(0)  编辑  收藏  所属分类: 
Java 
			
			
		 
		 
  1
package test;
  2
  3
import java.io.BufferedReader;
  4
import java.io.File;
  5
import java.io.FileInputStream;
  6
import java.io.FileOutputStream;
  7
import java.io.FileReader;
  8
  9
import junit.framework.TestCase;
 10
 11
public class Filetest extends TestCase 
{
 12
    
 13
    // 创建文件夹
 14
    public String  creatdir(String dir) 
{
 15
        File dirFile;
 16
        boolean bFile;
 17
 18
        bFile = false;
 19
        //指定创建文件夹的目录和名称
 20
        dirFile = new File(dir);
 21
        bFile = dirFile.exists();
 22
 23
        if (bFile == true) 
{
 24
            System.out.println("文件夹已经存在.");
 25
        } else 
{
 26
            System.out.println("文件夹不存在,开始创建");
 27
            bFile = dirFile.mkdirs();
 28
            if (bFile == true) 
{
 29
                System.out.println("创建文件夹成功!");
 30
            } else 
{
 31
                System.out.println("创建文件夹失败.");
 32
                System.exit(1);
 33
            }
 34
        }
 35
        
 36
        return dir;
 37
    }
 38
 39
    //拷贝文件夹
 40
    public void copyDir(File from, File to) 
{
 41
        // 判断需要拷贝到的路径下的文件夹是否存在
 42
        if (!to.exists()) 
{
 43
            // 创建文件夹
 44
            to.mkdirs();
 45
        }
 46
        File[] files = from.listFiles();
 47
        for (int i = 0; i < files.length; i++) 
{//遍历文件夹
 48
            File file1 = files[i];
 49
            File file2 = new File(to.getPath() + File.separator
 50
                    + files[i].getName());
 51
            if (!file1.isDirectory()) 
{ //拷贝文件
 52
                copyFile(file1, file2);
 53
            } else 
{
 54
                copyDir(file1, file2);
 55
            }
 56
        }
 57
 58
    }
 59
    
 60
    //拷贝文件
 61
    public void copyFile(File src, File dest) 
{
 62
        try 
{
 63
            System.out.println(src.getAbsoluteFile() + " -> "
 64
                    + dest.getAbsoluteFile());
 65
            FileInputStream in = new FileInputStream(src);
 66
            FileOutputStream out = new FileOutputStream(dest);
 67
            byte[] buffer = new byte[1024];
 68
            while (in.read(buffer) != -1) 
{
 69
                out.write(buffer);
 70
            }
 71
            out.close();
 72
            in.close();
 73
            System.out.println("文件拷贝成功");
 74
        } catch (Exception e) 
{
 75
            System.out.println("文件拷贝失败");
 76
        }
 77
 78
    }
 79
 80
    // 删除文件夹
 81
    // param folderPath 文件夹完整绝对路径
 82
    public void delFolder(String folderPath) 
{
 83
        try 
{
 84
            this.delAllFile(folderPath); // 删除完里面所有内容(第一步)
 85
            String filePath = folderPath;
 86
            filePath = filePath.toString();
 87
            java.io.File myFilePath = new java.io.File(filePath);
 88
            myFilePath.delete(); // 删除空文件夹(第二步)
 89
        } catch (Exception e) 
{
 90
            e.printStackTrace();
 91
        }
 92
    }
 93
 94
    // 删除指定文件夹下所有文件(第一步)
 95
    // param path 文件夹完整绝对路径
 96
    public boolean delAllFile(String path) 
{
 97
        boolean flag = false;
 98
        File file = new File(path);
 99
        // 文件夹是否存在
100
        if (!file.exists()) 
{
101
            return flag;
102
        }
103
        // 如果不是目录
104
        if (!file.isDirectory()) 
{
105
            return flag;
106
        }
107
        String[] tempList = file.list();
108
        File temp = null;
109
        for (int i = 0; i < tempList.length; i++) 
{//遍历所有文件夹以及里面的文件
110
            if (path.endsWith(File.separator)) 
{  //文件分隔符,各个操作系统不一样,如WIndows的是"\",而Linux的是"/"
111
                temp = new File(path + tempList[i]);
112
            } else 
{
113
                temp = new File(path + File.separator + tempList[i]);
114
            }
115
            if (temp.isFile()) 
{//删除文件
116
                temp.delete();
117
            }
118
            if (temp.isDirectory()) 
{
119
                delAllFile(path + "/" + tempList[i]);// 先删除文件夹里面的文件
120
                delFolder(path + "/" + tempList[i]);// 再删除空文件夹
121
                flag = true;
122
            }
123
        }
124
        return flag;
125
    }
126
127
    //读文件内容
128
    public File readfile(String path) 
{
129
        BufferedReader bufread;
130
        String read = "";
131
        String readStr = "";
132
        File file = new File(path);
133
        try 
{
134
            FileReader fileread = new FileReader(file);
135
            bufread = new BufferedReader(fileread);
136
            while ((read = bufread.readLine()) != null) 
{
137
                readStr = readStr + read;
138
            }
139
            bufread.close();
140
        } catch (Exception d) 
{
141
            System.out.println(d.getMessage());
142
        }
143
        System.out.println("###readStr:" + readStr);
144
        return file;
145
    }
146
147
    public void testcopyfile() 
{
148
149
        Filetest t = new Filetest();
150
        String a = "D://test1";
151
        String b = "D://test2";
152
      // t.copyDir(t.readfile(a), t.readfile(b));
153
      // t.copyDir(new File(a), new File(b));
154
        t.delFolder(a);
155
156
    }
157
}
158