KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > util > io > FileUtils


1 /**
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright
3  * notice. All rights reserved.
4  */

5 package com.tc.util.io;
6
7 import java.io.File JavaDoc;
8 import java.io.FileInputStream JavaDoc;
9 import java.io.FileOutputStream JavaDoc;
10 import java.io.IOException JavaDoc;
11 import java.io.InputStream JavaDoc;
12 import java.io.OutputStream JavaDoc;
13 import java.util.Iterator JavaDoc;
14 import java.util.LinkedList JavaDoc;
15 import java.util.List JavaDoc;
16
17 public class FileUtils {
18
19   /*
20    * deletes all files with matching extension. Does not recurse into sub directories.
21    */

22   public static void forceDelete(File JavaDoc directory, String JavaDoc extension) throws IOException JavaDoc {
23     Iterator JavaDoc files = org.apache.commons.io.FileUtils.iterateFiles(directory, new String JavaDoc[] { extension }, false);
24     while (files.hasNext()) {
25       File JavaDoc f = (File JavaDoc) files.next();
26       org.apache.commons.io.FileUtils.forceDelete(f);
27     }
28   }
29
30   /**
31    * copy one file to another. Can also copy directories
32    */

33   public static void copyFile(File JavaDoc src, File JavaDoc dest) throws IOException JavaDoc {
34     List JavaDoc queue = new LinkedList JavaDoc();
35     queue.add(new CopyTask(src.getCanonicalFile(), dest.getCanonicalFile()));
36
37     while (queue.size() > 0) {
38       CopyTask item = (CopyTask) queue.remove(0);
39       if (item.getSrc().isDirectory()) {
40         File JavaDoc destDir = item.getDest();
41         destDir.mkdirs();
42
43         if (!destDir.isDirectory()) { throw new IOException JavaDoc("Destination directory does not exist: " + destDir); }
44
45         String JavaDoc[] list = item.getSrc().list();
46         for (int i = 0; i < list.length; i++) {
47           File JavaDoc _src = new File JavaDoc(item.getSrc(), list[i]);
48           File JavaDoc _dest = new File JavaDoc(item.getDest(), list[i]);
49           queue.add(new CopyTask(_src, _dest));
50         }
51       } else if (item.getSrc().isFile()) {
52         doCopy(item.getSrc(), item.getDest());
53       } else {
54         throw new IOException JavaDoc(item.getSrc() + " is neither a file or a directory");
55       }
56     }
57
58   }
59
60   private static void doCopy(File JavaDoc src, File JavaDoc dest) throws IOException JavaDoc {
61     FileInputStream JavaDoc in = null;
62     FileOutputStream JavaDoc out = null;
63     byte[] buffer = new byte[1024 * 8];
64     int count;
65     try {
66       in = new FileInputStream JavaDoc(src);
67       out = new FileOutputStream JavaDoc(dest);
68       while ((count = in.read(buffer)) >= 0) {
69         out.write(buffer, 0, count);
70       }
71     } finally {
72       closeQuietly(in);
73       closeQuietly(out);
74     }
75   }
76
77   private static class CopyTask {
78     private final File JavaDoc src;
79     private final File JavaDoc dest;
80
81     public CopyTask(File JavaDoc src, File JavaDoc dest) {
82       this.src = src;
83       this.dest = dest;
84     }
85
86     public File JavaDoc getSrc() {
87       return src;
88     }
89
90     public File JavaDoc getDest() {
91       return dest;
92     }
93   }
94
95   public static void closeQuietly(InputStream JavaDoc is) {
96     if (is != null) {
97       try {
98         is.close();
99       } catch (IOException JavaDoc ioe) {
100         //
101
}
102     }
103   }
104
105   public static void closeQuietly(OutputStream JavaDoc os) {
106     if (os != null) {
107       try {
108         os.close();
109       } catch (IOException JavaDoc ioe) {
110         //
111
}
112     }
113   }
114
115 }
116
Popular Tags