Source Code Cross Referenced for FileBean.java in  » Content-Management-System » contineo » org » contineo » core » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Content Management System » contineo » org.contineo.core 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        package org.contineo.core;
002:
003:        import java.io.BufferedInputStream;
004:        import java.io.BufferedOutputStream;
005:        import java.io.File;
006:        import java.io.FileInputStream;
007:        import java.io.FileOutputStream;
008:        import java.io.IOException;
009:        import java.io.InputStream;
010:        import java.io.OutputStream;
011:        import java.net.URI;
012:        import java.util.Date;
013:
014:        import org.apache.commons.logging.Log;
015:        import org.apache.commons.logging.LogFactory;
016:
017:        /**
018:         * This class manages files and directories.
019:         * 
020:         * @author Michael Scholz
021:         * @version 1.0
022:         */
023:        public class FileBean {
024:            static final int BUFF_SIZE = 100000;
025:
026:            static final byte[] buffer = new byte[BUFF_SIZE];
027:
028:            /**
029:             * This method deletes a file.
030:             * 
031:             * @param filename Name of the file to be deleted.
032:             * @return Success of delete.
033:             */
034:            public static boolean deleteFile(String filename) {
035:                File f = new File(filename);
036:                boolean result = false;
037:
038:                try {
039:                    result = f.delete();
040:                } catch (Exception ex) {
041:                    logError(ex.getMessage());
042:                }
043:
044:                return result;
045:            } // end method deleteFile
046:
047:            public static boolean createDir(String dirname) {
048:                File f = new File(dirname);
049:                boolean result = false;
050:
051:                try {
052:                    result = f.mkdirs();
053:                } catch (Exception ex) {
054:                    logError(ex.getMessage());
055:                }
056:
057:                return result;
058:            } // end method createDir
059:
060:            public static boolean renameFile(String filename, String newfilename) {
061:                File f = new File(filename);
062:                File nf = new File(newfilename);
063:                boolean result = false;
064:
065:                try {
066:                    result = f.renameTo(nf);
067:                } catch (Exception ex) {
068:                    logError(ex.getMessage());
069:                }
070:
071:                return result;
072:            } // end method renameFile
073:
074:            public static boolean copyFile(String filename, String newfilename) {
075:                boolean result = false;
076:                InputStream fis = null;
077:                OutputStream fos = null;
078:
079:                try {
080:                    File out = new File(newfilename);
081:                    fis = new FileInputStream(filename);
082:                    fos = new FileOutputStream(out);
083:
084:                    int buffer = 0;
085:
086:                    while ((buffer = fis.read()) != -1) {
087:                        fos.write(buffer);
088:                    }
089:
090:                    result = true;
091:                } catch (Exception ex) {
092:                    logError(ex.getMessage());
093:                } finally {
094:                    if (fis != null) {
095:                        try {
096:                            fis.close();
097:                        } catch (IOException ioe) {
098:                            ;
099:                        }
100:                    }
101:
102:                    if (fos != null) {
103:                        try {
104:                            fos.close();
105:                        } catch (IOException ioe) {
106:                            ;
107:                        }
108:                    }
109:                }
110:
111:                return result;
112:            } // end method copyFile
113:
114:            public static boolean copyDir(String dirname, String newdir) {
115:                boolean result = false;
116:
117:                try {
118:                    createDir(newdir);
119:
120:                    File f = new File(dirname);
121:
122:                    if (f.isDirectory()) {
123:                        String[] files = f.list();
124:
125:                        for (int i = 0; i < files.length; i++) {
126:                            File file = new File(new StringBuilder(dirname)
127:                                    .append(File.separator).append(files[i])
128:                                    .toString());
129:
130:                            if (file.isDirectory()) {
131:                                copyDir(new StringBuilder(f.getAbsolutePath())
132:                                        .append(File.separator)
133:                                        .append(files[i]).toString(),
134:                                        new StringBuilder(newdir).append(
135:                                                File.separator)
136:                                                .append(files[i]).toString());
137:                            } else {
138:                                copyFile(new StringBuilder(f.getAbsolutePath())
139:                                        .append(File.separator)
140:                                        .append(files[i]).toString(),
141:                                        new StringBuilder(newdir).append(
142:                                                File.separator)
143:                                                .append(files[i]).toString());
144:                            }
145:                        }
146:                    } else {
147:                        copyFile(dirname, newdir);
148:                    }
149:
150:                    result = true;
151:                } catch (Exception e) {
152:                    logError(e.getMessage());
153:                }
154:
155:                return result;
156:            } // end method copyDir
157:
158:            public static boolean deleteDir(String dirname) {
159:                try {
160:                    return deleteDir(new File(dirname));
161:                } catch (Exception ex) {
162:                    logError(ex.getMessage());
163:                }
164:
165:                return false;
166:            } // end method deleteDir
167:
168:            public static boolean deleteDir(File file) {
169:                boolean result = true;
170:
171:                try {
172:                    if (!file.exists()) {
173:                        return true;
174:                    }
175:
176:                    if (file.isDirectory()) {
177:                        File[] files = file.listFiles();
178:
179:                        for (int i = 0; i < files.length; i++) {
180:                            result = deleteDir(files[i]);
181:                        }
182:
183:                        result = file.delete();
184:                    } else {
185:                        result = file.delete();
186:                    }
187:                } catch (Exception ex) {
188:                    logError(ex.getMessage());
189:                    result = false;
190:                }
191:
192:                return result;
193:            } // end method deleteDir
194:
195:            public static boolean exists(String filename) {
196:                try {
197:                    File file = new File(filename);
198:                    return file.exists();
199:                } catch (Exception e) {
200:                    logError(e.getMessage());
201:                    return false;
202:                }
203:            } // end method exists
204:
205:            public static boolean exists(URI filename) {
206:                try {
207:                    File file = new File(filename);
208:                    return file.exists();
209:                } catch (Exception e) {
210:                    logError(e.getMessage());
211:                    return false;
212:                }
213:            } // end method exists
214:
215:            public static long getSize(String filename) {
216:                File f = new File(filename);
217:
218:                if (f.length() > 1024) {
219:                    return f.length();
220:                } else {
221:                    return 1024;
222:                }
223:            } // end method getSize
224:
225:            public static Date getLastModified(String filename) {
226:                File f = new File(filename);
227:
228:                return new Date(f.lastModified());
229:            } // end method getLastModified
230:
231:            public static StringBuffer readFile(String filename) {
232:                StringBuffer content = new StringBuffer();
233:                BufferedInputStream bis = null;
234:
235:                try {
236:                    File file = new File(filename);
237:                    bis = new BufferedInputStream(new FileInputStream(file));
238:
239:                    int ichar = 0;
240:
241:                    while ((ichar = bis.read()) > 0) {
242:                        content.append((char) ichar);
243:                    }
244:                } catch (Exception ex) {
245:                    logError(ex.getMessage());
246:                } finally {
247:                    if (bis != null) {
248:                        try {
249:                            bis.close();
250:                        } catch (IOException ioe) {
251:                            ;
252:                        }
253:                    }
254:                }
255:
256:                return content;
257:            } // end method readFile
258:
259:            public static void writeFile(InputStream in, String filepath)
260:                    throws Exception {
261:                OutputStream os = null;
262:                try {
263:                    os = new FileOutputStream(filepath);
264:
265:                    while (true) {
266:                        synchronized (buffer) {
267:                            int amountRead = in.read(buffer);
268:                            if (amountRead == -1)
269:                                break;
270:                            os.write(buffer, 0, amountRead);
271:                        }
272:                    }
273:                } catch (Exception e) {
274:                    logError(e.getMessage());
275:                } finally {
276:                    try {
277:                        if (in != null)
278:                            in.close();
279:                        if (os != null)
280:                            os.close();
281:                    } catch (IOException e) {
282:                        logError(e.getMessage());
283:                    }
284:                }
285:            } // end method writeFile
286:
287:            public static void writeFile(String text, String filepath) {
288:                BufferedOutputStream bos = null;
289:
290:                try {
291:                    bos = new BufferedOutputStream(new FileOutputStream(
292:                            filepath));
293:                    bos.write(text.getBytes());
294:                } catch (Exception e) {
295:                    logError(e.getLocalizedMessage());
296:                } finally {
297:                    if (bos != null) {
298:                        try {
299:                            bos.close();
300:                        } catch (IOException ioe) {
301:                            ;
302:                        }
303:                    }
304:                }
305:            } // end method writeFile
306:
307:            public static void appendFile(String text, String filepath) {
308:                OutputStream bos = null;
309:
310:                try {
311:                    bos = new FileOutputStream(filepath, true);
312:                    bos.write(text.getBytes());
313:                } catch (Exception e) {
314:                    logError(e.getLocalizedMessage());
315:                } finally {
316:                    if (bos != null) {
317:                        try {
318:                            bos.close();
319:                        } catch (IOException ioe) {
320:                            ;
321:                        }
322:                    }
323:                }
324:            }
325:
326:            private static void logError(String message) {
327:                Log logger = LogFactory.getLog(FileBean.class);
328:                logger.error(message);
329:            }
330:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.