source: ether_core/trunk/kit/interactifKit/zipContent/src/FileCopy2.java @ 784

Last change on this file since 784 was 6, checked in by cbipsl, 18 years ago

ajout rep ether

  • Property svn:executable set to *
File size: 4.6 KB
Line 
1/**
2 *      $Id: FileCopy2.java,v 1.1 2001/01/15 15:38:47 pn Exp $
3 *      Taken from Fred Lavigne (fred.lavigne.com), who evidently borrowed it
4 *      from Jamie Hall.
5 */
6
7package flafi.util;
8
9import java.io.*;
10
11/**
12 * This class can be used for copying files and/or directories in Java.
13 * I wrote it because, for some reason, there is no java.io.File.copy()
14 * method. It can be used to copy file->file, file->directory, or
15 * directory->directory. The copy method is static, and thus this class
16 * should not be instantiated.<p>
17 *
18 * You may study, use, modify, and distribute this code for any purpose.
19 * This code is provided <b>WITHOUT WARRANTY</b> either expressed or
20 * implied.<p>
21 *
22 * Download the <a href="FileCopy.java">source</a>, or the compiled
23 * <a href="FileCopy.class">class</a>.<p>
24 *
25 * <i>Jamie Hall</i>, copyright (c) 1996.<br>
26 * <a href="mailto:jamihall@mhv.net">jamihall@mhv.net</a><br>
27 * <a href="http://www.mhv.net/~jamihall">http://www.mhv.net/~jamihall</a>
28 *
29 * @version 1.1, January 1, 1996
30 * @author  Jamie Hall
31 */
32
33final public class FileCopy2 {
34
35
36 /**
37        * This class shouldn't be instantiated.
38        */
39        private FileCopy2() {
40        }
41
42
43 /**
44        * Copy files and/or directories.
45        *
46        * @param src source file or directory
47        * @param dest destination file or directory
48        * @exception IOException if operation fails
49        */
50        public static void copy(File src, File dest) throws IOException {
51
52                FileInputStream source = null;
53                FileOutputStream destination = null;
54                byte[] buffer;
55                int bytes_read;
56
57                // Make sure the specified source exists and is readable.
58                if (!src.exists())
59                        throw new IOException("source not found: " + src);
60                if (!src.canRead())
61                        throw new IOException("source is unreadable: " + src);
62
63                if (src.isFile()) {
64                        if (!dest.exists()) {
65        File parentdir = parent(dest);
66        if (!parentdir.exists())
67                                        parentdir.mkdirs();     // Make all directories, and not just the immediate parent
68                        }
69                        else if (dest.isDirectory()) {
70                                dest = new File(dest + File.separator + src);
71                        }
72                }
73                else if (src.isDirectory()) {
74                        if (dest.isFile())
75                                throw new IOException("cannot copy directory " + src + " to file " + dest);
76                        if (!dest.exists())
77                                dest.mkdirs();  // Make all directories, and not just the immediate parent
78                }
79               
80                // The following line requires that the file already
81                // exists!!  Thanks to Scott Downey (downey@telestream.com)
82                // for pointing this out.  Someday, maybe I'll find out
83                // why java.io.File.canWrite() behaves like this.  Is it
84                // intentional for some odd reason?
85                //if (!dest.canWrite())
86                        //throw new IOException("destination is unwriteable: " + dest);
87
88                // If we've gotten this far everything is OK and we can copy.
89                if (src.isFile()) {
90                        try {
91                    source = new FileInputStream(src);
92                        destination = new FileOutputStream(dest);
93                            buffer = new byte[1024];
94                                while(true) {
95                        bytes_read = source.read(buffer);
96                            if (bytes_read == -1) break;
97                                destination.write(buffer, 0, bytes_read);
98                                }
99                        }
100                finally {
101                        if (source != null) 
102                                try { source.close(); } catch (IOException e) { ; }
103                                if (destination != null) 
104                        try { destination.close(); } catch (IOException e) { ; }
105                    }
106                }
107                else if (src.isDirectory()) {
108                        String targetfile, target, targetdest;
109                        String[] files = src.list();
110
111                        for (int i = 0; i < files.length; i++) {
112                                targetfile = files[i];
113                                target = src + File.separator + targetfile;
114                                targetdest = dest + File.separator + targetfile;
115
116
117                                if ((new File(target)).isDirectory()) {
118                                        copy(new File(target), new File(targetdest));
119                                }
120                                else {
121
122                                        try {
123                                                source = new FileInputStream(target);
124                                                destination = new FileOutputStream(targetdest);
125                                                buffer = new byte[1024];
126                                         
127                                                while(true) {
128                                                        bytes_read = source.read(buffer);
129                                                        if (bytes_read == -1) break;
130                                                        destination.write(buffer, 0, bytes_read);
131                                                }
132                                        }
133                                        finally {
134                                                if (source != null) 
135                                                        try { source.close(); } catch (IOException e) { ; }
136                                                if (destination != null) 
137                                                        try { destination.close(); } catch (IOException e) { ; }
138                                        }
139                                }
140                        }
141                }
142        }
143
144
145   /**
146        * File.getParent() can return null when the file is specified without
147        * a directory or is in the root directory. This method handles those cases.
148        *
149        * @param f the target File to analyze
150        * @return the parent directory as a File
151        */
152        private static File parent(File f) {
153                String dirname = f.getParent();
154                if (dirname == null) {
155                        if (f.isAbsolute()) return new File(File.separator);
156                        else return new File(System.getProperty("user.dir"));
157                }
158                return new File(dirname);
159        }
160
161}
Note: See TracBrowser for help on using the repository browser.