KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > SOFA > Connector > EEG > fileutil > Copy


1 /*
2  * Copy.java
3  *
4  * Created on 10. duben 2002, 12:41
5  */

6
7 package SOFA.Connector.EEG.fileutil;
8
9 /**
10  *
11  * @author ghort
12  * @version
13  */

14 public class Copy {
15
16     /** Creates new Copy */
17     private Copy() {
18     }
19
20     public static void copy(java.io.File JavaDoc destination, java.io.File JavaDoc source) throws CopyException {
21         if (source.isDirectory()) {
22             if (!destination.isDirectory()) {
23                 throw new CopyException("Destination '"+destination.getName()+"' is not directory.");
24             }
25             copyDirectory(destination,source);
26         } else {
27             if (destination.isDirectory()) {
28                 destination=new java.io.File JavaDoc(destination,source.getName());
29             }
30             copyFile(destination,source);
31         }
32     }
33     
34     protected static void copyDirectory(java.io.File JavaDoc destination, java.io.File JavaDoc source) throws CopyException {
35         java.io.File JavaDoc[] list=source.listFiles();
36         for (int i=0;i<list.length;i++) {
37             java.io.File JavaDoc dest=new java.io.File JavaDoc(destination,list[i].getName());
38             if (list[i].isDirectory()) {
39                 dest.mkdir();
40                 copyDirectory(dest,list[i]);
41             } else {
42                 copyFile(dest,list[i]);
43             }
44         }
45     }
46     
47     protected static void copyFile(java.io.File JavaDoc destination, java.io.File JavaDoc source) throws CopyException {
48         try {
49             java.io.FileInputStream JavaDoc inStream=new java.io.FileInputStream JavaDoc(source);
50             java.io.FileOutputStream JavaDoc outStream=new java.io.FileOutputStream JavaDoc(destination);
51
52             int len;
53             byte[] buf=new byte[2048];
54              
55             while ((len=inStream.read(buf))!=-1) {
56                 outStream.write(buf,0,len);
57             }
58         } catch (Exception JavaDoc e) {
59             throw new CopyException("Can't copy file "+source+" -> "+destination+".",e);
60         }
61     }
62 }
63
Popular Tags