Source Code Cross Referenced for FileUtil.java in  » Development » jodd » jodd » io » 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 » Development » jodd » jodd.io 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


0001:        // Copyright (c) 2003-2007, Jodd Team (jodd.sf.net). All Rights Reserved.
0002:
0003:        package jodd.io;
0004:
0005:        import java.io.File;
0006:        import java.io.FileInputStream;
0007:        import java.io.FileNotFoundException;
0008:        import java.io.FileOutputStream;
0009:        import java.io.IOException;
0010:        import java.io.InputStream;
0011:        import java.io.StringWriter;
0012:        import java.net.URL;
0013:
0014:        /**
0015:         * File utilities.
0016:         */
0017:        public class FileUtil {
0018:
0019:            // ---------------------------------------------------------------- misc shortcuts
0020:
0021:            /**
0022:             * Checks if two files points to the same file.
0023:             */
0024:            public static boolean equals(String file1, String file2) {
0025:                return equals(new File(file1), new File(file2));
0026:            }
0027:
0028:            /**
0029:             * Checks if two files points to the same file.
0030:             */
0031:            public static boolean equals(File file1, File file2) {
0032:                try {
0033:                    file1 = file1.getCanonicalFile();
0034:                    file2 = file2.getCanonicalFile();
0035:                } catch (IOException ioex) {
0036:                    return false;
0037:                }
0038:                return file1.equals(file2);
0039:            }
0040:
0041:            /**
0042:             * Converts file URLs to file. Ignores other schemes and returns <code>null</code>.
0043:             */
0044:            public static File toFile(URL url) {
0045:                return new File(toFileName(url));
0046:            }
0047:
0048:            /**
0049:             * Converts file URLs to file name. Ignores other schemes and returns <code>null</code>.
0050:             */
0051:            public static String toFileName(URL url) {
0052:                if ((url == null)
0053:                        || (url.getProtocol().equals("file") == false)) {
0054:                    return null;
0055:                }
0056:                String filename = url.getFile()
0057:                        .replace('/', File.separatorChar);
0058:                int pos = 0;
0059:                while ((pos = filename.indexOf('%', pos)) >= 0) {
0060:                    if (pos + 2 < filename.length()) {
0061:                        String hexStr = filename.substring(pos + 1, pos + 3);
0062:                        char ch = (char) Integer.parseInt(hexStr, 16);
0063:                        filename = filename.substring(0, pos) + ch
0064:                                + filename.substring(pos + 3);
0065:                    }
0066:                }
0067:                return filename;
0068:            }
0069:
0070:            /**
0071:             * Converts array of URLS to file names string. Other schemes are ignored.
0072:             */
0073:            public static String toFileNames(URL[] urls) {
0074:                StringBuilder path = new StringBuilder();
0075:                for (URL url : urls) {
0076:                    String fileName = toFileName(url);
0077:                    if (fileName == null) {
0078:                        continue;
0079:                    }
0080:                    path.append(fileName).append(File.pathSeparatorChar);
0081:                }
0082:                return path.toString();
0083:            }
0084:
0085:            // ---------------------------------------------------------------- mkdirs
0086:
0087:            /**
0088:             * Creates all folders at once.
0089:             */
0090:            public static void mkdirs(String dirs) throws IOException {
0091:                mkdirs(new File(dirs));
0092:            }
0093:
0094:            /**
0095:             * Creates all folders at once.
0096:             */
0097:            public static void mkdirs(File dirs) throws IOException {
0098:                if (dirs.exists()) {
0099:                    if (dirs.isDirectory() == false) {
0100:                        throw new IOException("Directory '"
0101:                                + "' is not a directory.");
0102:                    }
0103:                    return;
0104:                }
0105:                if (dirs.mkdirs() == false) {
0106:                    throw new IOException("Unable to create directory '" + dirs
0107:                            + "'.");
0108:                }
0109:            }
0110:
0111:            /**
0112:             * Creates single folder.
0113:             */
0114:            public static void mkdir(String dir) throws IOException {
0115:                mkdir(new File(dir));
0116:            }
0117:
0118:            /**
0119:             * Creates single folders.
0120:             */
0121:            public static void mkdir(File dir) throws IOException {
0122:                if (dir.exists()) {
0123:                    if (dir.isDirectory() == false) {
0124:                        throw new IOException("Destination '"
0125:                                + "' is not a directory.");
0126:                    }
0127:                    return;
0128:                }
0129:                if (dir.mkdir() == false) {
0130:                    throw new IOException("Unable to create directory '" + dir
0131:                            + "'.");
0132:                }
0133:            }
0134:
0135:            // ---------------------------------------------------------------- touch
0136:
0137:            public static void touch(String file) throws IOException {
0138:                touch(new File(file));
0139:            }
0140:
0141:            /**
0142:             * Implements the Unix "touch" utility. It creates a new file
0143:             * with size 0 or, if the file exists already, it is opened and
0144:             * closed without modifying it, but updating the file date and time.
0145:             */
0146:            public static void touch(File file) throws IOException {
0147:                if (file == null) {
0148:                    throw new IOException("Destination is null.");
0149:                }
0150:                if (file.exists() == false) {
0151:                    StreamUtil.close(new FileOutputStream(file));
0152:                }
0153:                file.setLastModified(System.currentTimeMillis());
0154:            }
0155:
0156:            // ---------------------------------------------------------------- settings
0157:
0158:            /**
0159:             * Inner settings describe behaviour of the FileUtil class.
0160:             */
0161:            public static class Settings implements  Cloneable {
0162:                private Settings() {
0163:                }
0164:
0165:                // should destination file have the same timestamp as source
0166:                protected boolean preserveDate = true;
0167:                // overwrite existing destination
0168:                protected boolean overwriteExisting = true;
0169:                // create missing subdirectories of destination
0170:                protected boolean createDirs = true;
0171:                // use recursive directory copying and deleting
0172:                protected boolean recursive = true;
0173:                // don't stop on error and continue job as much as possible
0174:                protected boolean continueOnError = true;
0175:                // default encoding for reading/writing strings
0176:                protected String encoding = "ISO-8859-1";
0177:
0178:                public boolean isPreserveDate() {
0179:                    return preserveDate;
0180:                }
0181:
0182:                public Settings setPreserveDate(boolean preserveDate) {
0183:                    this .preserveDate = preserveDate;
0184:                    return this ;
0185:                }
0186:
0187:                public Settings dontPerserveDate() {
0188:                    this .preserveDate = false;
0189:                    return this ;
0190:                }
0191:
0192:                public boolean isOverwriteExisting() {
0193:                    return overwriteExisting;
0194:                }
0195:
0196:                public Settings setOverwriteExisting(boolean overwriteExisting) {
0197:                    this .overwriteExisting = overwriteExisting;
0198:                    return this ;
0199:                }
0200:
0201:                public Settings dontOverwrite() {
0202:                    this .overwriteExisting = false;
0203:                    return this ;
0204:                }
0205:
0206:                public boolean isCreateDirs() {
0207:                    return createDirs;
0208:                }
0209:
0210:                public Settings setCreateDirs(boolean createDirs) {
0211:                    this .createDirs = createDirs;
0212:                    return this ;
0213:                }
0214:
0215:                public Settings dontCreateDirs() {
0216:                    this .createDirs = false;
0217:                    return this ;
0218:                }
0219:
0220:                public boolean isRecursive() {
0221:                    return recursive;
0222:                }
0223:
0224:                public Settings setRecursive(boolean recursive) {
0225:                    this .recursive = recursive;
0226:                    return this ;
0227:                }
0228:
0229:                public Settings dontRecurse() {
0230:                    this .recursive = false;
0231:                    return this ;
0232:                }
0233:
0234:                public boolean isContinueOnError() {
0235:                    return continueOnError;
0236:                }
0237:
0238:                public Settings setContinueOnError(boolean continueOnError) {
0239:                    this .continueOnError = continueOnError;
0240:                    return this ;
0241:                }
0242:
0243:                public Settings stopOnError() {
0244:                    this .continueOnError = false;
0245:                    return this ;
0246:                }
0247:
0248:                public String getEncoding() {
0249:                    return encoding;
0250:                }
0251:
0252:                public Settings setEncoding(String encoding) {
0253:                    this .encoding = encoding;
0254:                    return this ;
0255:                }
0256:
0257:                // ------------------------------------------------------------ clone
0258:
0259:                @Override
0260:                public Object clone() throws CloneNotSupportedException {
0261:                    Object clone = super .clone();
0262:                    //noinspection RedundantStringConstructorCall
0263:                    ((Settings) clone).encoding = new String(this .encoding);
0264:                    return clone;
0265:                }
0266:            }
0267:
0268:            // default global settings
0269:            public static Settings settings = new Settings();
0270:
0271:            /**
0272:             * Creates new {@link Settings} instance by clonning current default settings.
0273:             */
0274:            public static Settings cloneSettings() {
0275:                try {
0276:                    return (Settings) settings.clone();
0277:                } catch (CloneNotSupportedException cnsex) {
0278:                    return null;
0279:                }
0280:            }
0281:
0282:            /**
0283:             * Creates new {@link Settings} instance with default values.
0284:             */
0285:            public static Settings newSettings() {
0286:                return new Settings();
0287:            }
0288:
0289:            // ---------------------------------------------------------------- copy file to file
0290:
0291:            public static void copyFile(String src, String dest)
0292:                    throws IOException {
0293:                copyFile(new File(src), new File(dest), settings);
0294:            }
0295:
0296:            public static void copyFile(String src, String dest,
0297:                    Settings settings) throws IOException {
0298:                copyFile(new File(src), new File(dest), settings);
0299:            }
0300:
0301:            public static void copyFile(File src, File dest) throws IOException {
0302:                copyFile(src, dest, settings);
0303:            }
0304:
0305:            /**
0306:             * Copies a file to another file with specified copy settings.
0307:             */
0308:            public static void copyFile(File src, File dest, Settings settings)
0309:                    throws IOException {
0310:                checkFileCopy(src, dest, settings);
0311:                doCopyFile(src, dest, settings);
0312:            }
0313:
0314:            private static void checkFileCopy(File src, File dest,
0315:                    Settings settings) throws IOException {
0316:                if (src == null) {
0317:                    throw new IOException("Source is null.");
0318:                }
0319:                if (dest == null) {
0320:                    throw new IOException("Destination is null.");
0321:                }
0322:                if (src.exists() == false) {
0323:                    throw new FileNotFoundException("Source '" + src
0324:                            + "' does not exist.");
0325:                }
0326:                if (src.isFile() == false) {
0327:                    throw new IOException("Source '" + src + "' is not a file.");
0328:                }
0329:                if (equals(src, dest) == true) {
0330:                    throw new IOException("Source '" + src
0331:                            + "' and destination '" + dest + "' are the same.");
0332:                }
0333:
0334:                File destParent = dest.getParentFile();
0335:                if (destParent != null && destParent.exists() == false) {
0336:                    if (settings.createDirs == false) {
0337:                        throw new IOException("Destination directory '"
0338:                                + destParent + "' doesn't exist.");
0339:                    }
0340:                    if (destParent.mkdirs() == false) {
0341:                        throw new IOException("Destination directory '"
0342:                                + destParent + "' cannot be created.");
0343:                    }
0344:                }
0345:            }
0346:
0347:            /**
0348:             * Internal file copy when most of the pre-checking has passed.
0349:             */
0350:            private static void doCopyFile(File src, File dest,
0351:                    Settings settings) throws IOException {
0352:                if (dest.exists()) {
0353:                    if (dest.isDirectory()) {
0354:                        throw new IOException("Destination '" + dest
0355:                                + "' is a directory.");
0356:                    }
0357:                    if (settings.overwriteExisting == false) {
0358:                        throw new IOException("Destination '" + dest
0359:                                + "' already exists.");
0360:                    }
0361:                }
0362:
0363:                doCopy(src, dest);
0364:
0365:                if (src.length() != dest.length()) {
0366:                    throw new IOException("Copying of '" + src + "' to '"
0367:                            + dest + "' failed due to different sizes.");
0368:                }
0369:                if (settings.preserveDate) {
0370:                    dest.setLastModified(src.lastModified());
0371:                }
0372:            }
0373:
0374:            // ---------------------------------------------------------------- simple copy file
0375:
0376:            /**
0377:             * Copies one file to another without any checking.
0378:             * @see #doCopy(java.io.File, java.io.File)
0379:             */
0380:            protected static void doCopy(String src, String dest)
0381:                    throws IOException {
0382:                doCopy(new File(src), new File(dest));
0383:            }
0384:
0385:            /**
0386:             * Copies one file to another without any checking. It is assumed that
0387:             * both parameters represents valid files.
0388:             */
0389:            protected static void doCopy(File src, File dest)
0390:                    throws IOException {
0391:                FileInputStream input = new FileInputStream(src);
0392:                try {
0393:                    FileOutputStream output = new FileOutputStream(dest);
0394:                    try {
0395:                        StreamUtil.copy(input, output);
0396:                    } finally {
0397:                        StreamUtil.close(output);
0398:                    }
0399:                } finally {
0400:                    StreamUtil.close(input);
0401:                }
0402:            }
0403:
0404:            // ---------------------------------------------------------------- copy file to directory
0405:
0406:            public static void copyFileToDir(String src, String destDir)
0407:                    throws IOException {
0408:                copyFileToDir(new File(src), new File(destDir), settings);
0409:            }
0410:
0411:            public static void copyFileToDir(String src, String destDir,
0412:                    Settings settings) throws IOException {
0413:                copyFileToDir(new File(src), new File(destDir), settings);
0414:            }
0415:
0416:            public static void copyFileToDir(File src, File destDir)
0417:                    throws IOException {
0418:                copyFileToDir(src, destDir, settings);
0419:            }
0420:
0421:            /**
0422:             * Copies a file to folder with specified copy settings.
0423:             */
0424:            public static void copyFileToDir(File src, File destDir,
0425:                    Settings settings) throws IOException {
0426:                if (destDir == null) {
0427:                    throw new IOException("Destination is null.");
0428:                }
0429:                if (destDir.exists() && destDir.isDirectory() == false) {
0430:                    throw new IOException("Destination '" + destDir
0431:                            + "' is not a directory.");
0432:                }
0433:                copyFile(src, new File(destDir, src.getName()), settings);
0434:            }
0435:
0436:            // ---------------------------------------------------------------- copy dir
0437:
0438:            public static void copyDir(String srcDir, String destDir)
0439:                    throws IOException {
0440:                copyDir(new File(srcDir), new File(destDir), settings);
0441:            }
0442:
0443:            public static void copyDir(String srcDir, String destDir,
0444:                    Settings settings) throws IOException {
0445:                copyDir(new File(srcDir), new File(destDir), settings);
0446:            }
0447:
0448:            public static void copyDir(File srcDir, File destDir)
0449:                    throws IOException {
0450:                copyDir(srcDir, destDir, settings);
0451:            }
0452:
0453:            /**
0454:             * Copies directory with specified copy settings.
0455:             */
0456:            public static void copyDir(File srcDir, File destDir,
0457:                    Settings settings) throws IOException {
0458:                checkDirCopy(srcDir, destDir);
0459:                doCopyDirectory(srcDir, destDir, settings);
0460:            }
0461:
0462:            private static void checkDirCopy(File srcDir, File destDir)
0463:                    throws IOException {
0464:                if (srcDir == null) {
0465:                    throw new IOException("Source is null.");
0466:                }
0467:                if (destDir == null) {
0468:                    throw new IOException("Destination is null.");
0469:                }
0470:                if (srcDir.exists() == false) {
0471:                    throw new FileNotFoundException("Source '" + srcDir
0472:                            + "' does not exist.");
0473:                }
0474:                if (srcDir.isDirectory() == false) {
0475:                    throw new IOException("Source '" + srcDir
0476:                            + "' is not a directory.");
0477:                }
0478:                if (equals(srcDir, destDir) == true) {
0479:                    throw new IOException("Source '" + srcDir
0480:                            + "' and destination '" + destDir
0481:                            + "' are the same.");
0482:                }
0483:            }
0484:
0485:            private static void doCopyDirectory(File srcDir, File destDir,
0486:                    Settings settings) throws IOException {
0487:                if (destDir.exists()) {
0488:                    if (destDir.isDirectory() == false) {
0489:                        throw new IOException("Destination '" + destDir
0490:                                + "' is not a directory.");
0491:                    }
0492:                } else {
0493:                    if (settings.createDirs == false) {
0494:                        throw new IOException("Destination '" + destDir
0495:                                + "' doesn't exists.");
0496:                    }
0497:                    if (destDir.mkdirs() == false) {
0498:                        throw new IOException("Destination '" + destDir
0499:                                + "' directory cannot be created.");
0500:                    }
0501:                    if (settings.preserveDate) {
0502:                        destDir.setLastModified(srcDir.lastModified());
0503:                    }
0504:                }
0505:
0506:                File[] files = srcDir.listFiles();
0507:                if (files == null) {
0508:                    throw new IOException("Failed to list contents of '"
0509:                            + srcDir + '\'');
0510:                }
0511:
0512:                IOException exception = null;
0513:                for (File file : files) {
0514:                    File destFile = new File(destDir, file.getName());
0515:                    try {
0516:                        if (file.isDirectory()) {
0517:                            if (settings.recursive == true) {
0518:                                doCopyDirectory(file, destFile, settings);
0519:                            }
0520:                        } else {
0521:                            doCopyFile(file, destFile, settings);
0522:                        }
0523:                    } catch (IOException ioex) {
0524:                        if (settings.continueOnError == true) {
0525:                            exception = ioex;
0526:                            continue;
0527:                        }
0528:                        throw ioex;
0529:                    }
0530:                }
0531:
0532:                if (exception != null) {
0533:                    throw exception;
0534:                }
0535:            }
0536:
0537:            // ---------------------------------------------------------------- move file
0538:
0539:            public static void moveFile(String src, String dest)
0540:                    throws IOException {
0541:                moveFile(new File(src), new File(dest), settings);
0542:            }
0543:
0544:            public static void moveFile(String src, String dest,
0545:                    Settings settings) throws IOException {
0546:                moveFile(new File(src), new File(dest), settings);
0547:            }
0548:
0549:            public static void moveFile(File src, File dest) throws IOException {
0550:                moveFile(src, dest, settings);
0551:            }
0552:
0553:            public static void moveFile(File src, File dest, Settings settings)
0554:                    throws IOException {
0555:                checkFileCopy(src, dest, settings);
0556:                doMoveFile(src, dest, settings);
0557:            }
0558:
0559:            private static void doMoveFile(File src, File dest,
0560:                    Settings settings) throws IOException {
0561:                if (dest.exists()) {
0562:                    if (dest.isFile() == false) {
0563:                        throw new IOException("Destination '" + dest
0564:                                + "' is not a file.");
0565:                    }
0566:                    if (settings.overwriteExisting == false) {
0567:                        throw new IOException("Destination '" + dest
0568:                                + "' already exists.");
0569:                    }
0570:                    dest.delete();
0571:                }
0572:
0573:                if (src.renameTo(dest) == false) {
0574:                    throw new IOException("Moving of '" + src + "' to '" + dest
0575:                            + "' failed.");
0576:                }
0577:            }
0578:
0579:            // ---------------------------------------------------------------- move file to dir
0580:
0581:            public static void moveFileToDir(String src, String destDir)
0582:                    throws IOException {
0583:                moveFileToDir(new File(src), new File(destDir), settings);
0584:            }
0585:
0586:            public static void moveFileToDir(String src, String destDir,
0587:                    Settings settings) throws IOException {
0588:                moveFileToDir(new File(src), new File(destDir), settings);
0589:            }
0590:
0591:            public static void moveFileToDir(File src, File destDir)
0592:                    throws IOException {
0593:                moveFileToDir(src, destDir, settings);
0594:            }
0595:
0596:            public static void moveFileToDir(File src, File destDir,
0597:                    Settings settings) throws IOException {
0598:                if (destDir == null) {
0599:                    throw new IOException("Destination is null.");
0600:                }
0601:                if (destDir.exists() && destDir.isDirectory() == false) {
0602:                    throw new IOException("Destination '" + destDir
0603:                            + "' is not a directory.");
0604:                }
0605:                moveFile(src, new File(destDir, src.getName()), settings);
0606:            }
0607:
0608:            // ---------------------------------------------------------------- move dir
0609:
0610:            public static void moveDir(String srcDir, String destDir)
0611:                    throws IOException {
0612:                moveDir(new File(srcDir), new File(destDir));
0613:            }
0614:
0615:            public static void moveDir(File srcDir, File destDir)
0616:                    throws IOException {
0617:                checkDirCopy(srcDir, destDir);
0618:                doMoveDirectory(srcDir, destDir);
0619:            }
0620:
0621:            private static void doMoveDirectory(File src, File dest)
0622:                    throws IOException {
0623:                if (dest.exists()) {
0624:                    if (dest.isDirectory() == false) {
0625:                        throw new IOException("Destination '" + dest
0626:                                + "' is not a directory.");
0627:                    }
0628:                    dest = new File(dest, dest.getName());
0629:                    dest.mkdir();
0630:                }
0631:
0632:                if (src.renameTo(dest) == false) {
0633:                    throw new IOException("Moving of '" + src + "' to '" + dest
0634:                            + "' failed.");
0635:                }
0636:            }
0637:
0638:            // ---------------------------------------------------------------- delete file
0639:
0640:            public static void deleteFile(String dest) throws IOException {
0641:                deleteFile(new File(dest));
0642:            }
0643:
0644:            public static void deleteFile(File dest) throws IOException {
0645:                if (dest == null) {
0646:                    throw new IOException("Destination is null.");
0647:                }
0648:                if (dest.exists() == false) {
0649:                    throw new FileNotFoundException("Destination '" + dest
0650:                            + "' doesn't exist");
0651:                }
0652:                if (dest.isFile() == false) {
0653:                    throw new IOException("Destination '" + dest
0654:                            + "' is not a file.");
0655:                }
0656:                if (dest.delete() == false) {
0657:                    throw new IOException("Unable to delete '" + dest + "'.");
0658:                }
0659:            }
0660:
0661:            // ---------------------------------------------------------------- delete dir
0662:
0663:            public static void deleteDir(String dest) throws IOException {
0664:                deleteDir(new File(dest), settings);
0665:            }
0666:
0667:            public static void deleteDir(String dest, Settings settings)
0668:                    throws IOException {
0669:                deleteDir(new File(dest), settings);
0670:            }
0671:
0672:            public static void deleteDir(File dest) throws IOException {
0673:                deleteDir(dest, settings);
0674:            }
0675:
0676:            /**
0677:             * Deletes a directory.
0678:             */
0679:            public static void deleteDir(File dest, Settings settings)
0680:                    throws IOException {
0681:                cleanDir(dest, settings);
0682:                if (dest.delete() == false) {
0683:                    throw new IOException("Unable to delete '" + dest + "'.");
0684:                }
0685:            }
0686:
0687:            public static void cleanDir(String dest) throws IOException {
0688:                cleanDir(new File(dest), settings);
0689:            }
0690:
0691:            public static void cleanDir(String dest, Settings settings)
0692:                    throws IOException {
0693:                cleanDir(new File(dest), settings);
0694:            }
0695:
0696:            public static void cleanDir(File dest) throws IOException {
0697:                cleanDir(dest, settings);
0698:            }
0699:
0700:            /**
0701:             * Cleans a directory without deleting it.
0702:             */
0703:            public static void cleanDir(File dest, Settings settings)
0704:                    throws IOException {
0705:                if (dest == null) {
0706:                    throw new IOException("Destination is null.");
0707:                }
0708:                if (dest.exists() == false) {
0709:                    throw new FileNotFoundException("Destination '" + dest
0710:                            + "' doesn't exists.");
0711:                }
0712:
0713:                if (dest.isDirectory() == false) {
0714:                    throw new IOException("Destination '" + dest
0715:                            + "' is not a directory.");
0716:                }
0717:
0718:                File[] files = dest.listFiles();
0719:                if (files == null) {
0720:                    throw new IOException("Failed to list contents of '" + dest
0721:                            + "'.");
0722:                }
0723:
0724:                IOException exception = null;
0725:                for (File file : files) {
0726:                    try {
0727:                        if (file.isDirectory()) {
0728:                            if (settings.recursive == true) {
0729:                                deleteDir(file, settings);
0730:                            }
0731:                        } else {
0732:                            file.delete();
0733:                        }
0734:                    } catch (IOException ioex) {
0735:                        if (settings.continueOnError == true) {
0736:                            exception = ioex;
0737:                            continue;
0738:                        }
0739:                        throw ioex;
0740:                    }
0741:                }
0742:
0743:                if (exception != null) {
0744:                    throw exception;
0745:                }
0746:            }
0747:
0748:            // ---------------------------------------------------------------- read/write string
0749:
0750:            public static String readString(String source) throws IOException {
0751:                return readString(new File(source), settings.encoding);
0752:            }
0753:
0754:            public static String readString(String source, String encoding)
0755:                    throws IOException {
0756:                return readString(new File(source), encoding);
0757:            }
0758:
0759:            public static String readString(File source) throws IOException {
0760:                return readString(source, settings.encoding);
0761:            }
0762:
0763:            public static String readString(File source, String encoding)
0764:                    throws IOException {
0765:                if (source == null) {
0766:                    throw new IOException("Source '" + source + "' is null.");
0767:                }
0768:                if (source.exists() == false) {
0769:                    throw new FileNotFoundException("Source '" + source
0770:                            + "' doesn't exist.");
0771:                }
0772:                if (source.isFile() == false) {
0773:                    throw new IOException("Source '" + source
0774:                            + "' is not a file.");
0775:                }
0776:                long len = source.length();
0777:                if (len >= Integer.MAX_VALUE) {
0778:                    len = Integer.MAX_VALUE;
0779:                }
0780:                FileInputStream in = null;
0781:                try {
0782:                    in = new FileInputStream(source);
0783:                    StringWriter sw = new StringWriter((int) len);
0784:                    StreamUtil.copy(in, sw, encoding);
0785:                    return sw.toString();
0786:                } finally {
0787:                    StreamUtil.close(in);
0788:                }
0789:            }
0790:
0791:            public static void writeString(String dest, String data)
0792:                    throws IOException {
0793:                writeString(new File(dest), data, settings.encoding);
0794:            }
0795:
0796:            public static void writeString(String dest, String data,
0797:                    String encoding) throws IOException {
0798:                writeString(new File(dest), data, encoding);
0799:            }
0800:
0801:            public static void writeString(File dest, String data)
0802:                    throws IOException {
0803:                writeString(dest, data, settings.encoding);
0804:            }
0805:
0806:            public static void writeString(File dest, String data,
0807:                    String encoding) throws IOException {
0808:                if (dest == null) {
0809:                    throw new IOException("Destination '" + dest + "' is null.");
0810:                }
0811:                if (dest.exists() == true) {
0812:                    if (dest.isFile() == false) {
0813:                        throw new IOException("Destination '" + dest
0814:                                + "' exist, but it is not a file.");
0815:                    }
0816:                }
0817:                FileOutputStream out = null;
0818:                try {
0819:                    out = new FileOutputStream(dest);
0820:                    out.write(data.getBytes(encoding));
0821:                } finally {
0822:                    StreamUtil.close(out);
0823:                }
0824:            }
0825:
0826:            // ---------------------------------------------------------------- read/write bytearray
0827:
0828:            public static byte[] readBytes(String file) throws IOException {
0829:                return readBytes(new File(file));
0830:            }
0831:
0832:            public static byte[] readBytes(File source) throws IOException {
0833:                if (source == null) {
0834:                    throw new IOException("Source '" + source + "' is null.");
0835:                }
0836:                if (source.exists() == false) {
0837:                    throw new FileNotFoundException("Source '" + source
0838:                            + "' doesn't exist.");
0839:                }
0840:                if (source.isFile() == false) {
0841:                    throw new IOException("Source '" + source
0842:                            + "' exists, but it is not a file.");
0843:                }
0844:                long len = source.length();
0845:                if (len >= Integer.MAX_VALUE) {
0846:                    throw new IOException(
0847:                            "Source size is greater then max array size.");
0848:                }
0849:                FileInputStream in = null;
0850:                try {
0851:                    in = new FileInputStream(source);
0852:                    return StreamUtil.readBytes(in);
0853:                } finally {
0854:                    StreamUtil.close(in);
0855:                }
0856:            }
0857:
0858:            public static void writeBytes(String dest, byte[] data)
0859:                    throws IOException {
0860:                writeBytes(new File(dest), data, 0, data.length);
0861:            }
0862:
0863:            public static void writeBytes(String dest, byte[] data, int off,
0864:                    int len) throws IOException {
0865:                writeBytes(new File(dest), data, off, len);
0866:            }
0867:
0868:            public static void writeBytes(File dest, byte[] data)
0869:                    throws IOException {
0870:                writeBytes(dest, data, 0, data.length);
0871:            }
0872:
0873:            public static void writeBytes(File dest, byte[] data, int off,
0874:                    int len) throws IOException {
0875:                if (dest == null) {
0876:                    throw new IOException("Destination '" + dest + "' is null.");
0877:                }
0878:                if (dest.exists() == true) {
0879:                    if (dest.isFile() == false) {
0880:                        throw new IOException("Destination '" + dest
0881:                                + "' exist but it is not a file.");
0882:                    }
0883:                }
0884:                FileOutputStream out = null;
0885:                try {
0886:                    out = new FileOutputStream(dest);
0887:                    out.write(data, off, len);
0888:                } finally {
0889:                    StreamUtil.close(out);
0890:                }
0891:            }
0892:
0893:            // ---------------------------------------------------------------- equals content
0894:
0895:            public static boolean compare(String file1, String file2)
0896:                    throws IOException {
0897:                return compare(new File(file1), new File(file2));
0898:            }
0899:
0900:            /**
0901:             * Compare the contents of two files to determine if they are equal or
0902:             * not.
0903:             * <p>
0904:             * This method checks to see if the two files are different lengths
0905:             * or if they point to the same file, before resorting to byte-by-byte
0906:             * comparison of the contents.
0907:             * <p>
0908:             * Code origin: Avalon
0909:             */
0910:            public static boolean compare(File file1, File file2)
0911:                    throws IOException {
0912:                if ((file1 == null) || (file2 == null)) {
0913:                    throw new IOException("One of the files is null.");
0914:                }
0915:                boolean file1Exists = file1.exists();
0916:                if (file1Exists != file2.exists()) {
0917:                    return false;
0918:                }
0919:
0920:                if (file1Exists == false) {
0921:                    return true;
0922:                }
0923:
0924:                if ((file1.isFile() == false) || (file2.isFile() == false)) {
0925:                    throw new IOException("Only files can be compared.");
0926:                }
0927:
0928:                if (file1.length() != file2.length()) {
0929:                    return false;
0930:                }
0931:
0932:                if (equals(file1, file1)) {
0933:                    return true;
0934:                }
0935:
0936:                InputStream input1 = null;
0937:                InputStream input2 = null;
0938:                try {
0939:                    input1 = new FileInputStream(file1);
0940:                    input2 = new FileInputStream(file2);
0941:                    return StreamUtil.compare(input1, input2);
0942:                } finally {
0943:                    StreamUtil.close(input1);
0944:                    StreamUtil.close(input2);
0945:                }
0946:            }
0947:
0948:            // ---------------------------------------------------------------- time
0949:
0950:            public static boolean isNewer(String file, String reference) {
0951:                return isNewer(new File(file), new File(reference));
0952:            }
0953:
0954:            /**
0955:             * Test if specified <code>File</code> is newer than the reference <code>File</code>.
0956:             *
0957:             * @param file		the <code>File</code> of which the modification date must be compared
0958:             * @param reference	the <code>File</code> of which the modification date is used
0959:             * @return <code>true</code> if the <code>File</code> exists and has been modified more
0960:             * 			recently than the reference <code>File</code>.
0961:             */
0962:            public static boolean isNewer(File file, File reference) {
0963:                if (reference == null) {
0964:                    throw new IllegalArgumentException(
0965:                            "Reference file is null.");
0966:                }
0967:                if (reference.exists() == false) {
0968:                    throw new IllegalArgumentException("The reference file '"
0969:                            + file + "' doesn't exist");
0970:                }
0971:                return isNewer(file, reference.lastModified());
0972:            }
0973:
0974:            public static boolean isOlder(String file, String reference) {
0975:                return isOlder(new File(file), new File(reference));
0976:            }
0977:
0978:            public static boolean isOlder(File file, File reference) {
0979:                if (reference == null) {
0980:                    throw new IllegalArgumentException(
0981:                            "Reference file is null.");
0982:                }
0983:                if (reference.exists() == false) {
0984:                    throw new IllegalArgumentException("The reference file '"
0985:                            + file + "' doesn't exist");
0986:                }
0987:                return isOlder(file, reference.lastModified());
0988:            }
0989:
0990:            /**
0991:             * Tests if the specified <code>File</code> is newer than the specified time reference.
0992:             *
0993:             * @param file			the <code>File</code> of which the modification date must be compared.
0994:             * @param timeMillis	the time reference measured in milliseconds since the
0995:             * 						epoch (00:00:00 GMT, January 1, 1970)
0996:             * @return <code>true</code> if the <code>File</code> exists and has been modified after
0997:             *         the given time reference.
0998:             */
0999:            public static boolean isNewer(File file, long timeMillis) {
1000:                if (file == null) {
1001:                    throw new IllegalArgumentException("File is null.");
1002:                }
1003:                if (!file.exists()) {
1004:                    return false;
1005:                }
1006:                return file.lastModified() > timeMillis;
1007:            }
1008:
1009:            public static boolean isNewer(String file, long timeMillis) {
1010:                return isNewer(new File(file), timeMillis);
1011:            }
1012:
1013:            public static boolean isOlder(File file, long timeMillis) {
1014:                if (file == null) {
1015:                    throw new IllegalArgumentException("File is null.");
1016:                }
1017:                if (!file.exists()) {
1018:                    return false;
1019:                }
1020:                return file.lastModified() < timeMillis;
1021:            }
1022:
1023:            public static boolean isOlder(String file, long timeMillis) {
1024:                return isOlder(new File(file), timeMillis);
1025:            }
1026:
1027:            // ---------------------------------------------------------------- smart copy
1028:
1029:            public static void copy(String src, String dest) throws IOException {
1030:                copy(new File(src), new File(dest), settings);
1031:            }
1032:
1033:            public static void copy(String src, String dest, Settings settings)
1034:                    throws IOException {
1035:                copy(new File(src), new File(dest), settings);
1036:            }
1037:
1038:            public static void copy(File src, File dest) throws IOException {
1039:                copy(src, dest, settings);
1040:            }
1041:
1042:            /**
1043:             * Smart copy. If source is a directory, copy it to destination.
1044:             * Otherwise, if destination is directory, copy source file to it.
1045:             * Otherwise, try to copy source file to destination file.
1046:             */
1047:            public static void copy(File src, File dest, Settings settings)
1048:                    throws IOException {
1049:                if (src == null) {
1050:                    throw new IOException("Source file is null.");
1051:                }
1052:                if (dest == null) {
1053:                    throw new IOException("Destination file is null.");
1054:                }
1055:                if (src.isDirectory() == true) {
1056:                    copyDir(src, dest, settings);
1057:                    return;
1058:                }
1059:                if (dest.isDirectory() == true) {
1060:                    copyFileToDir(src, dest, settings);
1061:                    return;
1062:                }
1063:                copyFile(src, dest, settings);
1064:            }
1065:
1066:            // ---------------------------------------------------------------- smart move
1067:
1068:            public static void move(String src, String dest) throws IOException {
1069:                move(new File(src), new File(dest), settings);
1070:            }
1071:
1072:            public static void move(String src, String dest, Settings settings)
1073:                    throws IOException {
1074:                move(new File(src), new File(dest), settings);
1075:            }
1076:
1077:            public static void move(File src, File dest) throws IOException {
1078:                move(src, dest, settings);
1079:            }
1080:
1081:            /**
1082:             * Smart move. If source is a directory, move it to destination.
1083:             * Otherwise, if destination is directory, move source file to it.
1084:             * Otherwise, try to move source file to destination file.
1085:             */
1086:            public static void move(File src, File dest, Settings settings)
1087:                    throws IOException {
1088:                if (src == null) {
1089:                    throw new IOException("Source file is null.");
1090:                }
1091:                if (dest == null) {
1092:                    throw new IOException("Destination file is null.");
1093:                }
1094:                if (src.isDirectory() == true) {
1095:                    moveDir(src, dest);
1096:                    return;
1097:                }
1098:                if (dest.isDirectory() == true) {
1099:                    moveFileToDir(src, dest, settings);
1100:                    return;
1101:                }
1102:                moveFile(src, dest, settings);
1103:            }
1104:
1105:            // ---------------------------------------------------------------- smart delete
1106:
1107:            public static void delete(String dest) throws IOException {
1108:                delete(new File(dest), settings);
1109:            }
1110:
1111:            public static void delete(String dest, Settings settings)
1112:                    throws IOException {
1113:                delete(new File(dest), settings);
1114:            }
1115:
1116:            public static void delete(File dest) throws IOException {
1117:                delete(dest, settings);
1118:            }
1119:
1120:            /**
1121:             * Smart delete of destination file or directory.
1122:             */
1123:            public static void delete(File dest, Settings settings)
1124:                    throws IOException {
1125:                if (dest == null) {
1126:                    throw new IOException("Destination is null.");
1127:                }
1128:                if (dest.isDirectory()) {
1129:                    deleteDir(dest, settings);
1130:                    return;
1131:                }
1132:                deleteFile(dest);
1133:            }
1134:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.