KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > buchuki > util > FileUtils


1 /*
2  * Copyright 2005 Dusty Phillips
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package com.buchuki.util;
18
19 import java.io.*;
20
21 /**
22  * Class containing auxillary methods for file manipulation. Most of these
23  * methods ought to be included in the File class.
24  *
25  * @author Dusty Phillips [dusty@buchuki.com]
26  */

27 public class FileUtils {
28     
29     /**
30      * Static constructor; not meant to be instantiated
31      */

32     private FileUtils() {
33     }
34     
35     /**
36      * Remove a directory and recursively remove its contents.
37      *
38      * @param directory the directory to recursively remove.
39      */

40     public static void deleteDirectory(File directory) throws IOException {
41         if (directory.isDirectory()) {
42             File[] files = directory.listFiles();
43             for (File file : files) {
44                 if (file.isDirectory()) {
45                     deleteDirectory(file);
46                 } else {
47                     file.delete();
48                 }
49             }
50         }
51         directory.delete();
52     }
53     
54     /**
55      * Copy a directory and its contents to a new location
56      *
57      * @param fromDirectory a File indicating the old directory (must exist)
58      * @param toDirectory a File indicating the new directory (must not exist)
59      */

60     public static void copyDirectory(File fromDirectory, File toDirectory)
61         throws IOException {
62         if (toDirectory.exists()) {
63             throw new IllegalArgumentException JavaDoc(
64                     "toDirectory " + toDirectory + "already exists");
65         }
66         toDirectory.mkdirs();
67         File[] files = fromDirectory.listFiles();
68         for (File file : files) {
69             File newFile = new File(toDirectory, file.getName());
70             if (file.isDirectory()) {
71                 copyDirectory(file, newFile);
72             } else {
73                 copyFile(file, newFile);
74             }
75         }
76     }
77     
78     /**
79      * Copy a regular file to a new location
80      *
81      * @param from a File indicating the original file (must exist)
82      * @param to a File indicating the new file (must not exist)
83      */

84     public static void copyFile(File from, File to) throws IOException {
85         if (to.exists()) {
86             throw new IllegalArgumentException JavaDoc(
87                     "toFile " + to + " exists");
88         }
89         InputStream in = new BufferedInputStream(new FileInputStream(from));
90         OutputStream out = new BufferedOutputStream(new FileOutputStream(to));
91         
92         int b;
93         while ((b = in.read()) != -1) {
94             out.write(b);
95         }
96         in.close();
97         out.close();
98     }
99 }
100
Popular Tags