Source Code Cross Referenced for DirectoryUtil.java in  » IDE-Netbeans » iep.editor » org » netbeans » modules » iep » project » anttasks » 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 » IDE Netbeans » iep.editor » org.netbeans.modules.iep.project.anttasks 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * The contents of this file are subject to the terms of the Common Development
003:         * and Distribution License (the License). You may not use this file except in
004:         * compliance with the License.
005:         * 
006:         * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
007:         * or http://www.netbeans.org/cddl.txt.
008:         * 
009:         * When distributing Covered Code, include this CDDL Header Notice in each file
010:         * and include the License file at http://www.netbeans.org/cddl.txt.
011:         * If applicable, add the following below the CDDL Header, with the fields
012:         * enclosed by brackets [] replaced by your own identifying information:
013:         * "Portions Copyrighted [year] [name of copyright owner]"
014:         * 
015:         * The Original Software is NetBeans. The Initial Developer of the Original
016:         * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
017:         * Microsystems, Inc. All Rights Reserved.
018:         */
019:
020:        package org.netbeans.modules.iep.project.anttasks;
021:
022:        import java.io.File;
023:        import java.io.FileFilter;
024:        import java.io.FileInputStream;
025:        import java.io.FileOutputStream;
026:        import java.io.IOException;
027:        import java.io.InputStream;
028:        import java.io.OutputStream;
029:        import java.io.Reader;
030:        import java.io.Writer;
031:        import java.util.ArrayList;
032:        import java.util.List;
033:
034:        /**
035:         * @author Bing Lu
036:         * @todo Document this class
037:         */
038:        public class DirectoryUtil extends Object {
039:            /**
040:             * Creates new DirectoryUtil
041:             */
042:            public DirectoryUtil() {
043:            }
044:
045:            /**
046:             * This method will return an arraylist containing all the file/s name/s in
047:             * the provided directory name
048:             *
049:             * @param fromDir Current directory where files are located.
050:             * @return ArrayList - could be zero in length if directory is empty or no
051:             *      found.
052:             */
053:            public static List getFiles(String fromDir) {
054:                // create the object to be return as a result
055:                ArrayList filesList = new ArrayList();
056:                if (dirExists(fromDir)) {
057:                    // yes
058:                    File aDir = new File(fromDir);
059:                    // create the directory class
060:                    String files[] = aDir.list();
061:                    // list all the file in the directory
062:                    int numFiles = files.length;
063:                    // get the file count
064:                    for (int i = 0; i < numFiles; i++) {
065:                        // update the array list with the file names
066:                        String fileName = fromDir
067:                                + System.getProperty("file.separator")
068:                                + files[i];
069:                        if (dirExists(fileName) == false) {
070:                            // do not add sub directory to the list
071:                            filesList.add(files[i]);
072:                        }
073:                    }
074:                }
075:                return filesList;
076:            }
077:
078:            /**
079:             * @param sourceDir source dir to be moved
080:             * @param targetDir target : where it will be moved to
081:             * @param excludeExt exclude files with these extension for the move
082:             * @todo Document this method
083:             */
084:            public static void moveTree(File sourceDir, File targetDir,
085:                    String[] excludeExt) {
086:                FileFilter filter = new ExtensionFilter(excludeExt, false);
087:                moveFilesInDir(sourceDir, targetDir, filter, true);
088:            }
089:
090:            /**
091:             * this util method construct the absolute path from its parts. it will
092:             * remove any preamble to the file name prior to concatenate the path
093:             * elements.
094:             *
095:             * @param aDir - the directory part of the absolute path
096:             * @param aFileName - the file part of the absolute path
097:             * @return string absolute path of the parameters provided.
098:             */
099:            public static String buildFullPath(String aDir, String aFileName) {
100:                String currentDir = ".";
101:                String fullPath = "";
102:                if (aFileName.startsWith(currentDir)) {
103:                    fullPath = aDir + aFileName.substring(1);
104:                } else {
105:                    if (aFileName.startsWith(System
106:                            .getProperty("file.separator"))) {
107:                        fullPath = aDir + aFileName;
108:                    } else {
109:                        fullPath = aDir + System.getProperty("file.separator")
110:                                + aFileName;
111:                    }
112:                }
113:                return fullPath;
114:            }
115:
116:            /**
117:             * Copy one file's contents to another file.
118:             *
119:             * @param source The file to copy.
120:             * @param dest The file to copy to.
121:             * @return true if the copy succeeded, false otherwise.
122:             */
123:            public static boolean copyFile(File source, File dest) {
124:                FileInputStream in = null;
125:                FileOutputStream out = null;
126:
127:                try {
128:                    in = new FileInputStream(source);
129:                    out = new FileOutputStream(dest);
130:                    copyStream(in, out);
131:                } catch (Exception e) {
132:                    return false;
133:                } finally {
134:                    if (in != null) {
135:                        try {
136:                            in.close();
137:                        } catch (Exception e) {
138:                            // empty!
139:                        }
140:                    }
141:
142:                    if (out != null) {
143:                        try {
144:                            out.close();
145:                        } catch (Exception e) {
146:                            // empty!
147:                        }
148:                    }
149:                }
150:
151:                return true;
152:            }
153:
154:            /**
155:             * @param source source directory to copy
156:             * @param destDir target directory where it will be copied to
157:             * @return whether copy was successfule or not
158:             * @todo Document this method
159:             */
160:            public static boolean copyFile(String source, String destDir) {
161:                FileInputStream fisource = null;
162:                FileOutputStream fodest = null;
163:
164:                try {
165:                    File fsource = new File(source);
166:                    fisource = new FileInputStream(fsource);
167:                    File fdest = new File(destDir, fsource.getName());
168:                    fodest = new FileOutputStream(fdest);
169:                    copyStream(fisource, fodest);
170:                } catch (Throwable e) {
171:                    return false;
172:                } finally {
173:                    try {
174:                        fisource.close();
175:                    } catch (Throwable e) {
176:                        // empty!
177:                    }
178:                    try {
179:                        fodest.close();
180:                    } catch (Throwable e) {
181:                        // empty!
182:                    }
183:                }
184:                return true;
185:            }
186:
187:            /**
188:             * @param sourceDir source directory
189:             * @param targetDir target directory
190:             * @param filter file filter
191:             * @param recursive TODO: document me!
192:             * @todo Document this method
193:             */
194:            public static void copyFilesInDir(File sourceDir, File targetDir,
195:                    FileFilter filter, boolean recursive) {
196:                // Create target directory
197:                if (!targetDir.exists()) {
198:                    targetDir.mkdirs();
199:                }
200:
201:                File[] children = sourceDir.listFiles(filter);
202:                for (int i = 0; i < children.length; ++i) {
203:                    if (children[i].isDirectory() && recursive) {
204:                        copyFilesInDir(children[i], new File(targetDir,
205:                                children[i].getName()), filter, recursive);
206:                    } else {
207:                        copyFile(children[i], new File(targetDir, children[i]
208:                                .getName()));
209:                    }
210:                }
211:            }
212:
213:            /**
214:             * @param is input stream
215:             * @param os output stream
216:             * @exception IOException rethrow exception from streams
217:             * @todo Document this method
218:             */
219:            public static void copyStream(InputStream is, OutputStream os)
220:                    throws IOException {
221:                int bytesRead = 0;
222:                byte[] memBuf = new byte[32768];
223:
224:                while (bytesRead != -1) {
225:                    bytesRead = is.read(memBuf);
226:                    if (bytesRead != -1) {
227:                        os.write(memBuf, 0, bytesRead);
228:                    }
229:                }
230:                os.flush();
231:            }
232:
233:            /**
234:             * @param r reader object
235:             * @param w writer object
236:             * @exception IOException rethrow exception from streams
237:             * @todo Document this method
238:             */
239:            public static void copyStream(Reader r, Writer w)
240:                    throws IOException {
241:                int nRead;
242:                char[] memBuf = new char[32768];
243:                while ((nRead = r.read(memBuf)) != -1) {
244:                    w.write(memBuf, 0, nRead);
245:                }
246:                w.flush();
247:            }
248:
249:            /**
250:             * This method copies recursively copies all the files in a directory to
251:             * another directory, maintaining the source directory's child directory
252:             * structure. If any particular file type should be excluded, it's extension
253:             * may be specified using the excludeExt argument.
254:             *
255:             * @param sourceDir The directory to copy files from.
256:             * @param targetDir The directory to copy files to.
257:             * @param excludeExt Array of string extensions of form .ext to exclude from
258:             *      the copy.
259:             */
260:            public static void copyTree(File sourceDir, File targetDir,
261:                    String[] excludeExt) {
262:                FileFilter filter = new ExtensionFilter(excludeExt, false);
263:                copyFilesInDir(sourceDir, targetDir, filter, true);
264:            }
265:
266:            /**
267:             * This method will create a directory and its parent directories. The
268:             * specified directory cannot be created if a file exists with the same
269:             * name.
270:             *
271:             * @param dirName Full pathname of directory to be created.
272:             * @return boolean Returns true if directory already exists or was created
273:             *      sucessfully, false otherwise.
274:             */
275:            public static boolean createDir(String dirName) {
276:                File dir = new File(dirName);
277:                if (!dir.exists() || !dir.isDirectory()) {
278:                    // create the directories if they don't exist
279:                    return dir.mkdirs();
280:                } else {
281:                    // directory already exists
282:                    return true;
283:                }
284:            }
285:
286:            /**
287:             * This method will recursively delete a directory and all its files and
288:             * subdirectories.
289:             *
290:             * @param dirName The full path of the directory to delete
291:             * @return Returns true if directory was deleted, false otherwise.
292:             */
293:            public static boolean deleteDir(String dirName) {
294:                return deleteDir(new File(dirName));
295:            }
296:
297:            /**
298:             * This method will recursively delete a directory and all its files and
299:             * subdirectories.
300:             *
301:             * @param dir The directory to delete
302:             * @return Returns true if directory was deleted, false otherwise.
303:             */
304:            public static boolean deleteDir(File dir) {
305:                return deleteDir(dir, true);
306:            }
307:
308:            /**
309:             * This method will recursively delete a directory and all its files and
310:             * subdirectories.
311:             *
312:             * @param dir The directory to delete
313:             * @param deleteOriginalDir Whether or not to delete the original directory.
314:             * @return Returns true if directory was deleted, false otherwise.
315:             */
316:            public static boolean deleteDir(File dir, boolean deleteOriginalDir) {
317:                if (!dir.isDirectory() || !dir.exists()) {
318:                    return false;
319:                }
320:
321:                // go through files in directory and delete one at a time
322:                File file[] = dir.listFiles();
323:                for (int i = 0; i < file.length; i++) {
324:                    if (file[i].isDirectory()) {
325:                        deleteDir(file[i]);
326:                    }
327:                    file[i].delete();
328:                }
329:                // try to delete directory itself
330:                if (deleteOriginalDir) {
331:                    return dir.delete();
332:                }
333:
334:                return true;
335:            }
336:
337:            /**
338:             * This method will delete a single file.
339:             *
340:             * @param filename Full pathname of the file to delete.
341:             * @return boolean Returns true if file was deleted, false otherwise.
342:             */
343:            public static boolean deleteFile(String filename) {
344:                File file = new File(filename);
345:                if (!file.exists()) {
346:                    return false;
347:                }
348:
349:                if (file.isFile() && file.canRead()) {
350:                    return file.delete();
351:                }
352:                return false;
353:            }
354:
355:            /**
356:             * This method will delete a single file.
357:             *
358:             * @param filename Name of the file.
359:             * @param dirName Directory where the file can be found.
360:             * @return boolean Returns true if file was deleted, false otherwise.
361:             */
362:            public static boolean deleteFile(String filename, String dirName) {
363:                return deleteFile(dirName + File.separator + filename);
364:            }
365:
366:            /**
367:             * This method will check if a file exists.
368:             *
369:             * @param dirName Full pathname of the directory.
370:             * @return boolean Returns true if the directory exists, false otherwise
371:             */
372:            public static boolean dirExists(String dirName) {
373:                File dir = new File(dirName);
374:                return (dir.exists() && dir.isDirectory());
375:            }
376:
377:            /**
378:             * This method will check if a file exists.
379:             *
380:             * @param filename Full pathname of the file.
381:             * @return boolean Returns true if the file exists and is not a directory,
382:             *      false otherwise
383:             */
384:            public static boolean fileExists(String filename) {
385:                File file = new File(filename);
386:                return (file.exists() && !file.isDirectory());
387:            }
388:
389:            /**
390:             * This method will move a single file from one directory to another.
391:             *
392:             * @param filename Full path name of the file.
393:             * @param toDir Directory where to move file to.
394:             * @return boolean Returns true if file was moved successfully, false
395:             *      otherwise.
396:             */
397:            public static boolean moveFile(String filename, String toDir) {
398:                char fs = System.getProperty("file.separator").charAt(0);
399:                File file = new File(filename);
400:                // check if the source file exist
401:                if (!file.exists()) {
402:                    return false;
403:                }
404:                // check to see if the destination file exist
405:                String destinationFile = toDir + fs + file.getName();
406:                File desFile = new File(destinationFile);
407:                if (desFile.exists()) {
408:                    // file exists, remove it to allow the move to complete
409:                    deleteFile(file.getName(), toDir);
410:                }
411:                // attempt to create the destination directory if it doesn't exist
412:                createDir(toDir);
413:                // attempt to move the file
414:                if (file.isFile() && file.canRead()) {
415:                    return file.renameTo(new File(toDir, file.getName()));
416:                }
417:                return false;
418:            }
419:
420:            /**
421:             * This method will move a single file from one directory to another.
422:             *
423:             * @param toDir Directory where to move file to.
424:             * @param file file that will be moved
425:             * @return boolean Returns true if file was moved successfully, false
426:             *      otherwise.
427:             */
428:            public static boolean moveFile(File file, String toDir) {
429:                char fs = System.getProperty("file.separator").charAt(0);
430:                //File file = new File(filename);
431:                // check if the source file exist
432:                if (!file.exists()) {
433:                    return false;
434:                }
435:
436:                // check to see if the destination file exist
437:
438:                String destinationFile = toDir + fs + file.getName();
439:                File desFile = new File(destinationFile);
440:
441:                if (desFile.exists()) {
442:                    // file exists, remove it to allow the move to complete
443:                    deleteFile(file.getName(), toDir);
444:                }
445:                // attempt to create the destination directory if it doesn't exist
446:                createDir(toDir);
447:                // attempt to move the file
448:                if (file.isFile() && file.canRead()) {
449:                    return file.renameTo(new File(toDir, file.getName()));
450:                }
451:                return false;
452:            }
453:
454:            /**
455:             * This method will more a single file from one directory to another.
456:             *
457:             * @param filename Name of the file.
458:             * @param fromDir Current directory where file is located.
459:             * @param toDir Directory where to move file to.
460:             * @return boolean Returns true if file was moved successfully, false
461:             *      otherwise.
462:             */
463:            public static boolean moveFile(String filename, String fromDir,
464:                    String toDir) {
465:                return moveFile(fromDir + File.separator + filename, toDir);
466:            }
467:
468:            /**
469:             * @param sourceDir source directory
470:             * @param targetDir target directory
471:             * @param filter file filter
472:             * @param recursive TODO: document me!
473:             * @todo Document this method
474:             */
475:            public static void moveFilesInDir(File sourceDir, File targetDir,
476:                    FileFilter filter, boolean recursive) {
477:
478:                char fs = System.getProperty("file.separator").charAt(0);
479:                if (!targetDir.exists()) {
480:                    targetDir.mkdirs();
481:                }
482:
483:                File[] children = sourceDir.listFiles(filter);
484:                // Create target directory
485:
486:                for (int i = 0; i < children.length; ++i) {
487:                    if (children[i].isDirectory() && recursive) {
488:                        moveFilesInDir(children[i], new File(targetDir,
489:                                children[i].getName()), filter, recursive);
490:                    } else {
491:                        moveFile(children[i], targetDir.getAbsolutePath());
492:                    }
493:                }
494:
495:            }
496:
497:            public static List getFilesRecursively(File dir, FileFilter filter) {
498:                List ret = new ArrayList();
499:                if (!dir.isDirectory()) {
500:                    return ret;
501:                }
502:                File[] fileNdirs = dir.listFiles(filter);
503:                for (int i = 0, I = fileNdirs.length; i < I; i++) {
504:                    if (fileNdirs[i].isDirectory()) {
505:                        ret.addAll(getFilesRecursively(fileNdirs[i], filter));
506:                    } else {
507:                        ret.add(fileNdirs[i]);
508:                    }
509:                }
510:                return ret;
511:            }
512:
513:            public static List getFilesRecursively(File dir, String[] extensions) {
514:                ExtensionFilter filter = new ExtensionFilter(extensions);
515:                return getFilesRecursively(dir, filter);
516:            }
517:
518:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.