KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > petals > util > FileUtil


1 /**
2  * PETALS - PETALS Services Platform.
3  * Copyright (c) 2005-2006 Fossil E-Commerce, http://www.fossilec.com/
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * -------------------------------------------------------------------------
19  * $Id: FileUtil.java 508 2006-06-02 08:05:25Z ddesjardins $
20  * -------------------------------------------------------------------------
21  */

22 package org.objectweb.petals.util;
23
24 import java.io.File JavaDoc;
25 import java.io.IOException JavaDoc;
26 import java.io.InputStream JavaDoc;
27 import java.io.OutputStream JavaDoc;
28
29 /**
30  * This util class is used to manage file
31  *
32  * @version $Rev: 508 $ $Date: 2006-06-02 10:05:25 +0200 (ven., 02 juin 2006) $
33  * @since Petals 1.0
34  * @author <a HREF="mailto:rmarins@fossilec.com">Rafael Marins</a>
35  * @author ddesjardins - eBMWebsourcing
36  */

37 public final class FileUtil {
38
39     /**
40      * Buffer size used when copying the content of an input stream to an output
41      * stream.
42      */

43     private static final int DEFAULT_BUFFER_SIZE = 4096;
44
45     /**
46      * Do not construct.
47      */

48     private FileUtil() {
49         super();
50     }
51
52     /**
53      * Copy an in stream to an out stream
54      *
55      * @param in
56      * @param out
57      * @throws IOException
58      */

59     public static void copyInputStream(InputStream JavaDoc in, OutputStream JavaDoc out)
60         throws IOException JavaDoc {
61         byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
62         int len;
63         while ((len = in.read(buffer)) >= 0) {
64             out.write(buffer, 0, len);
65         }
66         in.close();
67         out.close();
68     }
69
70     public static boolean removeDirectory(File JavaDoc dir) {
71
72         boolean completeRemoval = true;
73
74         if (!dir.isDirectory() && !dir.exists()) {
75             return false;
76         }
77
78         // first remove directory contents
79
for (File JavaDoc aFile : dir.listFiles()) {
80
81             if (aFile.isDirectory()) {
82                 completeRemoval = removeDirectory(aFile);
83             } else {
84                 completeRemoval = aFile.delete();
85             }
86         }
87
88         if (completeRemoval) {
89             // delete main directory (now empty)
90
completeRemoval = dir.delete();
91         } else {
92             // do nothing simply log
93
System.out.println("Directory not deleted because not empty :"
94                     + dir.getAbsolutePath());
95         }
96
97         return completeRemoval;
98     }
99
100 }
101
Popular Tags