KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > uk > ac > roe > antigen > utils > CopyableFile


1 /*
2  * Created on 27-Dec-2004
3  */

4 package uk.ac.roe.antigen.utils;
5
6 import java.io.File JavaDoc;
7 import java.io.FileInputStream JavaDoc;
8 import java.io.FileOutputStream JavaDoc;
9 import java.io.IOException JavaDoc;
10 import java.net.URI JavaDoc;
11 import java.nio.channels.FileChannel JavaDoc;
12 import java.util.logging.Logger JavaDoc;
13
14 /**
15  * A file that is able to be copied - you'd think this would be straightforward
16  * wouldn't you?
17  *
18  * @author jdt
19  */

20 public class CopyableFile extends File JavaDoc {
21     /**
22      * Logger for this class
23      */

24     private static final Logger JavaDoc logger = Logger.getLogger(CopyableFile.class.getName());
25
26     public static void main(String JavaDoc[] args) throws IOException JavaDoc {
27         //quick test
28
CopyableFile file = new CopyableFile("C:/cygwin");
29         file.copyTo(new File JavaDoc("C:/cygwin2"));
30         //file.copyTo(new File("default2.properties"));
31
System.out.println("is abs "+file.isAbsolute());
32         System.out.println("absolute file " + file.getAbsoluteFile());
33         System.out.println("absolute path "+file.getAbsolutePath());
34         System.out.println("canonical file "+file.getCanonicalFile());
35         System.out.println("canonical path "+ file.getCanonicalPath());
36         System.out.println("name "+file.getName());
37         System.out.println("parent "+file.getParent());
38         System.out.println("parent file "+file.getParentFile());
39         System.out.println("path "+file.getPath());
40         
41         
42     }
43     
44
45
46     /**
47      * Copy this file or directory to the given new location
48      * @param newLocation
49      * @throws IOException
50      */

51     public void copyTo(File JavaDoc newLocation) throws IOException JavaDoc {
52         logger.fine("Copying "+getAbsolutePath()+" to "+newLocation);
53         if (isFile()) {
54             logger.fine("(file)");
55             copyFileTo(newLocation);
56         } else {
57             logger.fine("(folder)");
58             File JavaDoc[] files = listFiles();
59             logger.fine("Contains "+files.length+" files");
60             newLocation.mkdirs();
61             for (int i = 0;i<files.length;++i) {
62              logger.fine("Processing " +files[i]);
63              CopyableFile file = new CopyableFile(files[i]);
64              File JavaDoc newFile = new File JavaDoc(newLocation,file.getName());
65              file.copyTo(newFile);
66             }
67         }
68         
69     }
70     
71     /**
72      * You'd think this would be obvious....
73      * Remove a directory, even if not empty
74      */

75     public boolean recursivelyDelete() {
76         if (isFile()) {
77             logger.fine("deleting "+getName());
78             return delete();
79         } else {
80             File JavaDoc[] files = listFiles();
81             for (int i = 0;i<files.length;++i) {
82                 CopyableFile file = new CopyableFile(files[i]);
83                 file.recursivelyDelete();
84             }
85             return delete();
86         }
87         
88     }
89     
90     private void copyFileTo(File JavaDoc destination) throws IOException JavaDoc {
91         logger.fine("Copying from "+destination+"...");
92         FileChannel JavaDoc srcChannel = new FileInputStream JavaDoc(getAbsolutePath()).getChannel();
93         logger.fine("...got source channel "+srcChannel+"...");
94         FileChannel JavaDoc destChannel = new FileOutputStream JavaDoc(new File JavaDoc(destination.getAbsolutePath())).getChannel();
95         logger.fine("...got destination channel "+destChannel+"...");
96         logger.fine("...Got channels...");
97         destChannel.transferFrom(srcChannel, 0, srcChannel.size());
98         logger.fine("...transferred.");
99         srcChannel.close();
100         destChannel.close();
101     }
102     
103     /**
104      * @param arg0
105      */

106     public CopyableFile(String JavaDoc arg0) {
107         super(arg0);
108     }
109
110     /**
111      * @param arg0
112      * @param arg1
113      */

114     public CopyableFile(File JavaDoc arg0, String JavaDoc arg1) {
115         super(arg0, arg1);
116     }
117     
118     public CopyableFile(File JavaDoc arg0) {
119      super(arg0.getAbsolutePath());
120     }
121
122     
123     /**
124      * @param arg0
125      * @param arg1
126      */

127     public CopyableFile(String JavaDoc arg0, String JavaDoc arg1) {
128         super(arg0, arg1);
129     }
130     /**
131      * @param arg0
132      */

133     public CopyableFile(URI JavaDoc arg0) {
134         super(arg0);
135     }
136 }
137
Popular Tags