KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > ecm > taskdefs > FileHelper


1 // ====================================================================
2
//
3
// ECM: The Extensible Container Model
4
// Copyright (C) 2004 THALES
5
// Contact: openccm-ecm@objectweb.org
6
//
7
// This library is free software; you can redistribute it and/or
8
// modify it under the terms of the GNU Lesser General Public
9
// License as published by the Free Software Foundation; either
10
// version 2.1 of the License, or any later version.
11
//
12
// This library is distributed in the hope that it will be useful,
13
// but WITHOUT ANY WARRANTY; without even the implied warranty of
14
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15
// Lesser General Public License for more details.
16
//
17
// You should have received a copy of the GNU Lesser General Public
18
// License along with this library; if not, write to the Free Software
19
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20
// USA
21
//
22
// Initial developer(s): Mathieu Vadet.
23
// Initial Funding: IST COACH European project (IST-2001-34445)
24
// http://www.ist-coach.org
25
//
26
// ====================================================================
27

28
29
30 package org.objectweb.ecm.taskdefs;
31
32 /**
33  ** <p>Set of static operations to ease file manipulation for the ant tasks.</p>
34  **/

35 public class FileHelper
36 {
37     static public java.io.File JavaDoc[]
38     listFiles(java.io.File JavaDoc dir)
39     {
40         java.util.ArrayList JavaDoc files = new java.util.ArrayList JavaDoc();
41         java.io.File JavaDoc[] childs = dir.listFiles();
42         for (int i=0;i<childs.length;i++) {
43             if (childs[i].isDirectory()) {
44                 files.addAll(java.util.Arrays.asList(listFiles(childs[i])));
45             }
46             else {
47                 files.add(childs[i]);
48             }
49         }
50
51         return (java.io.File JavaDoc[])files.toArray(new java.io.File JavaDoc[0]);
52     }
53
54     static public void
55     deleteDir(java.io.File JavaDoc dir)
56     {
57         //
58
if (dir==null) {
59             return ;
60         }
61
62         java.io.File JavaDoc[] files = dir.listFiles();
63         if (files==null) {
64             files = new java.io.File JavaDoc[0];
65         }
66
67         for (int i=0;i<files.length;i++) {
68             if (files[i].isDirectory()) {
69                 deleteDir(files[i]);
70             }
71             else {
72                 files[i].delete();
73             }
74         }
75
76         dir.delete();
77     }
78
79     static public void
80     moveFiles(java.io.File JavaDoc[] files, java.io.File JavaDoc destdir)
81     throws org.apache.tools.ant.BuildException
82     {
83         //
84
if (destdir==null) {
85             return ;
86         }
87
88         java.io.File JavaDoc destfile = null;
89         // create dest dir if non-existent
90
destdir.mkdirs();
91
92         for (int i=0;i<files.length;i++) {
93             // recurse on directories
94
if (files[i].isDirectory()) {
95                 java.io.File JavaDoc ndestdir = new java.io.File JavaDoc(destdir, files[i].getName());
96                 moveFiles(files[i].listFiles(), ndestdir);
97                 continue;
98             }
99
100             destfile = new java.io.File JavaDoc(destdir, files[i].getName());
101             // only move if dest file is older
102
// NOTE: should not happen in our case
103
if ((destfile.exists()) && (destfile.lastModified()>files[i].lastModified())) {
104                 continue;
105             }
106
107             // copy
108
java.io.FileInputStream JavaDoc in = null;
109             java.io.FileOutputStream JavaDoc out = null;
110             try {
111                 // create dest file if non-existent first
112
if (!destfile.exists()) {
113                     destfile.createNewFile();
114                 }
115
116                 in = new java.io.FileInputStream JavaDoc(files[i]);
117                 out = new java.io.FileOutputStream JavaDoc(destfile);
118
119                 byte[] buffer = new byte[8*1024];
120                 int count = 0;
121                 do {
122                     out.write(buffer, 0, count);
123                     count = in.read(buffer, 0, buffer.length);
124                 } while (count!=-1);
125
126             } catch (Exception JavaDoc ex) {
127                 String JavaDoc msg = "exception caught: "+ex.getMessage();
128                 ex.printStackTrace();
129                 throw new org.apache.tools.ant.BuildException(msg);
130             } finally {
131                 if (in!=null) { try {in.close();} catch(java.io.IOException JavaDoc ex) {} }
132                 if (out!=null) { try {out.close();} catch(java.io.IOException JavaDoc ex) {} }
133             }
134         }
135     }
136
137     static public java.io.File JavaDoc
138     createTempDir()
139     throws org.apache.tools.ant.BuildException
140     {
141         // create temp file to obtain a path
142
java.io.File JavaDoc tmpfile = null;
143         try {
144             tmpfile = java.io.File.createTempFile("temp", null);
145         } catch (Exception JavaDoc ex) {
146             throw new org.apache.tools.ant.BuildException(ex.getMessage());
147         }
148
149         String JavaDoc path = tmpfile.getPath();
150         tmpfile.delete();
151
152         // create temp directory
153
java.io.File JavaDoc tmpdir = null;
154         try {
155             tmpdir = new java.io.File JavaDoc(path);
156             tmpdir.mkdirs();
157         } catch (Exception JavaDoc ex) {
158             throw new org.apache.tools.ant.BuildException(ex.getMessage());
159         }
160
161         return tmpdir;
162     }
163 }
164
Popular Tags