KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > sync4j > syncclient > common > FileSystemTools


1 /**
2  * Copyright (C) 2003-2005 Funambol
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program 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
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */

18
19 package sync4j.syncclient.common;
20
21 import java.io.*;
22 import java.util.Vector JavaDoc;
23
24 import sync4j.syncclient.common.VectorTools;
25 /**
26  * This class supplies some methods of usefullness for the
27  * management of files and directories.
28  * <p>In particular it supplies methods for the creation and the reading of
29  * file and for the removal of a directory and of its everything content
30  *
31  * @version $Id: FileSystemTools.java,v 1.4 2005/01/19 11:18:36 fabius Exp $
32  * @author Stefano Nichele
33  */

34
35 public class FileSystemTools {
36
37     // -----------------------------------------------------------Public methods
38

39     /**
40      * Delete a directory, his subdirectory and all files.
41      * @param directoryName the name of the directory to remove.
42      * @throws Exception if an error occurs.
43      */

44     public static void removeDirectoryTree(String JavaDoc directoryName)
45             throws Exception JavaDoc {
46
47         File directory = new File(directoryName);
48
49         if (!directory.exists()) {
50             return;
51         }
52
53         if (!directory.isDirectory()) {
54             throw new Exception JavaDoc("'" + directory + "' is not a directory");
55         }
56
57         String JavaDoc[] fileList = directory.list();
58         int numFile = fileList.length;
59         boolean fileDeleted = false;
60         File f = null;
61         for (int i=0; i<numFile; i++) {
62             f = new File(directoryName + File.separator + fileList[i]);
63             if (f.isDirectory()) {
64                 removeDirectoryTree(f.getPath());
65             } else {
66                 fileDeleted = f.delete();
67             }
68         }
69         fileDeleted = directory.delete();
70     }
71
72     /**
73      * Create a new file in the specified directory using
74      * the given file name and content.
75      * <p>If the directory it does not exist it will be created.</p>
76      * <p>If already exists a file with the same name, this has deleted before
77      * create the new file.</p>
78      * @param directoryName the directory in which the file must be created.
79      * @param fileName the name of the file
80      * @param content the content of the file.
81      * @throws Exception if an error occurs during creation.
82      */

83     public static void createFile(String JavaDoc directoryName,
84                                   String JavaDoc fileName, byte[] content)
85             throws Exception JavaDoc {
86
87         int size = content.length;
88
89         File fileOut = new File(directoryName +
90                 File.separator + fileName);
91
92         fileOut.mkdirs();
93
94         if (fileOut.exists()) {
95             fileOut.delete();
96         }
97
98         FileOutputStream fout = new FileOutputStream(fileOut);
99
100         fout.write(content);
101         fout.close();
102
103     }
104
105
106     /**
107      * Create a new file in the specified directory using
108      * the given file name and byteStream.
109      * <p>If the directory it does not exist it will be created.</p>
110      * <p>If already exists a file with the same name, this has deleted before
111      * create the new file.</p>
112      * @param directoryName the directory in which the file must be created.
113      * @param fileName the name of the file
114      * @param byteStream the content of the file.
115      * @throws Exception if an error occurs during creation.
116      */

117     public static void createFile(String JavaDoc directoryName,
118                                   String JavaDoc fileName, ByteArrayOutputStream byteStream)
119             throws Exception JavaDoc {
120
121         File fileOut = new File(directoryName +
122                 File.separator + fileName);
123
124         fileOut.mkdirs();
125
126         if (fileOut.exists()) {
127             fileOut.delete();
128         }
129
130         FileOutputStream fout = new FileOutputStream(fileOut);
131         byteStream.writeTo(fout);
132         fout.close();
133
134     }
135
136     /**
137      * Write the given text in the given file appending to or rewriting it
138      * accordingly to <i>append</i>.
139      *
140      * @param file the file to write to - NOT NULL
141      * @param text the text to write - NULL, equivalent to blank
142      * @param append if true the text is appended to the file, if false
143      * the file is overwritten
144      *
145      * @throws IOException if an error occurs
146      */

147     public static void writeTextFile(File file, String JavaDoc text, boolean append)
148     throws IOException {
149         FileOutputStream fos = null;
150
151         try {
152             fos = new FileOutputStream(file.getAbsolutePath(), append);
153
154             if (text != null) {
155                 fos.write(text.getBytes());
156             }
157
158         } finally {
159             if (fos != null) {
160                 fos.close();
161             }
162         }
163     }
164
165
166     /**
167      * Returns the content of the file specified.
168      * @param fileName the name of the file.
169      * @return the content of the file.
170      * @throws IOException if the file not exits or if an I/O error occurs.
171      */

172     public static byte[] getFile(String JavaDoc fileName) throws IOException {
173         File file = new File(fileName);
174         int dim = (int)file.length();
175         byte[] content = new byte[dim];
176
177         BufferedInputStream stream =
178                 new BufferedInputStream(new FileInputStream(fileName));
179         stream.read(content);
180         stream.close();
181         return content;
182     }
183
184     /**
185      * Returns all files in the given directory filtering on the given extension.
186      * Pathnames are returned relative to <i>directory</i>
187      *
188      * @param directory the directory to list
189      * @param extension select only the files with this extension
190      *
191      * @return String[] with the selected filenames. An zero-length array if
192      * no files are selected
193      */

194     public static String JavaDoc[] getAllFiles(String JavaDoc directory,
195                                        String JavaDoc extension) {
196
197         ExtensionFilter filter = new ExtensionFilter(extension);
198
199         Vector JavaDoc fileList = new Vector JavaDoc();
200
201         String JavaDoc[] files = new File(directory).list(filter);
202
203         if ((files == null) || (files.length == 0)) {
204             return new String JavaDoc[0];
205         }
206
207         File f;
208         String JavaDoc[] children;
209         for (int i=0; i<files.length; ++i) {
210             f = new File(directory, files[i]);
211             if (f.isDirectory()) {
212                 children = getAllFiles(f.getPath(), extension);
213                 VectorTools.add(fileList, children);
214             }
215             //
216
// we have to exclude directories
217
//
218
if (f.isFile()){
219                 fileList.addElement(f.getPath());
220             }
221         }
222
223         return VectorTools.toStringArray(fileList);
224     }
225 }
Popular Tags