KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > contineo > core > FileBean


1 /*
2  * FileBean.java
3  *
4  * Created on 17. August 2003, 23:51
5  */

6
7 package org.contineo.core;
8
9 import java.io.BufferedInputStream JavaDoc;
10 import java.io.File JavaDoc;
11 import java.io.FileInputStream JavaDoc;
12 import java.io.FileOutputStream JavaDoc;
13 import java.io.IOException JavaDoc;
14 import java.io.InputStream JavaDoc;
15 import java.io.OutputStream JavaDoc;
16
17 import org.apache.log4j.Level;
18 import org.apache.log4j.Logger;
19 /**
20  * This class manages files and directories.
21  * @author Michael Scholz
22  * @version 1.0
23  */

24 public class FileBean {
25     static final int BUFF_SIZE = 100000;
26     static final byte[] buffer = new byte[BUFF_SIZE];
27
28     /**
29      * This method deletes a file.
30      * @param filename Name of the file to be deleted.
31      * @return Success of delete.
32      */

33     public static boolean deleteFile(String JavaDoc filename) {
34         File JavaDoc f = new File JavaDoc(filename);
35         boolean result = false;
36         try {
37             result = f.delete();
38         }
39         catch (Exception JavaDoc ex) {
40             logError(ex.getMessage());
41         }
42         return result;
43     }
44     
45     public static boolean createDir(String JavaDoc dirname) {
46         File JavaDoc f = new File JavaDoc(dirname);
47         boolean result = false;
48         try {
49             result = f.mkdirs();
50         }
51         catch (Exception JavaDoc ex) {
52             logError(ex.getMessage());
53         }
54         return result;
55     }
56     
57     public static boolean renameFile(String JavaDoc filename, String JavaDoc newfilename) {
58         File JavaDoc f = new File JavaDoc(filename);
59         File JavaDoc nf = new File JavaDoc(newfilename);
60         boolean result = false;
61         try {
62             result = f.renameTo(nf);
63         }
64         catch (Exception JavaDoc ex) {
65             logError(ex.getMessage());
66         }
67         return result;
68     }
69     
70     public static boolean copyFile(String JavaDoc filename, String JavaDoc newfilename) {
71         boolean result = false;
72         try {
73             File JavaDoc out = new File JavaDoc(newfilename);
74             InputStream JavaDoc fis = new FileInputStream JavaDoc(filename);
75             OutputStream JavaDoc fos = new FileOutputStream JavaDoc(out);
76             int buffer = 0;
77             while ((buffer = fis.read()) != -1) {
78                 fos.write(buffer);
79             }
80             fis.close();
81             fos.close();
82             result = true;
83         }
84         catch (Exception JavaDoc ex) {
85             logError(ex.getMessage());
86         }
87         return result;
88     }
89     
90     public static boolean copyDir(String JavaDoc dirname, String JavaDoc newdir) {
91         boolean result = false;
92         try {
93             createDir(newdir);
94             File JavaDoc f = new File JavaDoc(dirname);
95             if (f.isDirectory()) {
96                 String JavaDoc[] files = f.list();
97                 for (int i=0; i<files.length; i++) {
98                     File JavaDoc file = new File JavaDoc(dirname + "/" + files[i]);
99                     if (file.isDirectory())
100                         copyDir(f.getAbsolutePath() + "/" + files[i], newdir + "/" + files[i]);
101                     else
102                         copyFile(f.getAbsolutePath() + "/" + files[i], newdir + "/" + files[i]);
103                 }
104             } else {
105                 copyFile(dirname, newdir);
106             }
107             result = true;
108         } catch (Exception JavaDoc e) {
109             logError(e.getMessage());
110         }
111         return result;
112     }
113     
114     public static boolean deleteDir(String JavaDoc dirname) {
115         try {
116             return deleteDir(new File JavaDoc(dirname));
117         } catch (Exception JavaDoc ex) {
118             logError(ex.getMessage());
119         }
120         return false;
121     }
122     
123     public static boolean deleteDir(File JavaDoc file) {
124         boolean result = true;
125         try {
126             if (!file.exists())
127                 return true;
128             
129             if (file.isDirectory()) {
130                 File JavaDoc[] files = file.listFiles();
131                 for (int i=0;i<files.length;i++) {
132                     result = deleteDir(files[i]);
133                 }
134                 result = file.delete();
135             } else {
136                 result = file.delete();
137             }
138         } catch (Exception JavaDoc ex) {
139             logError(ex.getMessage());
140             result = false;
141         }
142         return result;
143     }
144     
145     public static boolean exists(String JavaDoc filename) {
146         try {
147             File JavaDoc file = new File JavaDoc(filename);
148             return file.exists();
149         } catch (Exception JavaDoc e) {
150             logError(e.getMessage());
151             return false;
152         }
153     }
154     
155     public static long getSize(String JavaDoc filename) {
156         File JavaDoc f = new File JavaDoc(filename);
157         if (f.length() > 1024)
158             return f.length();
159         else
160             return 1024;
161     }
162     
163     public static StringBuffer JavaDoc readFile(String JavaDoc filename) {
164         StringBuffer JavaDoc content = new StringBuffer JavaDoc();
165         try {
166             File JavaDoc file = new File JavaDoc(filename);
167             BufferedInputStream JavaDoc bis = new BufferedInputStream JavaDoc(new FileInputStream JavaDoc(file));
168             int ichar = 0;
169             while ((ichar = bis.read()) > 0) {
170                 content.append((char)ichar);
171             }
172             bis.close();
173         } catch (Exception JavaDoc ex) {
174             logError(ex.getMessage());
175         }
176         
177         return content;
178     }
179     
180     public static void writeFile(InputStream JavaDoc in, String JavaDoc filepath) {
181         OutputStream JavaDoc os = null;
182         try {
183             os = new FileOutputStream JavaDoc(filepath);
184             
185             while (true) {
186                 synchronized (buffer) {
187                     int amountRead = in.read(buffer);
188                     if (amountRead == -1)
189                         break;
190                     os.write(buffer, 0, amountRead);
191                 }
192             }
193         }
194         catch (Exception JavaDoc e) {
195             logError(e.getMessage());
196         }
197         finally {
198             try {
199                 if (in != null)
200                     in.close();
201                 if (os != null)
202                     os.close();
203             } catch (IOException JavaDoc e) {
204                 logError(e.getMessage());
205             }
206         }
207     }
208     
209     public static void writeFile(String JavaDoc text, String JavaDoc filepath) {
210         try {
211             OutputStream JavaDoc bos = new FileOutputStream JavaDoc(filepath);
212             bos.write(text.getBytes());
213             bos.close();
214         }
215         catch (Exception JavaDoc e) {
216             logError(e.getMessage());
217         }
218     }
219     
220     public static void appendFile(String JavaDoc text, String JavaDoc filepath) {
221         try {
222             OutputStream JavaDoc bos = new FileOutputStream JavaDoc(filepath, true);
223             bos.write(text.getBytes());
224             bos.close();
225         } catch (Exception JavaDoc e) {
226             logError(e.getMessage());
227         }
228     }
229     
230     private static void logError(String JavaDoc message) {
231         Logger logger = LoggingManager.getLogger(org.contineo.core.FileBean.class);
232         if (logger.isEnabledFor(Level.ERROR))
233             logger.error(message);
234     }
235 }
236
Popular Tags