KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jodd > io > FileUtil


1 // Copyright (c) 2003-2007, Jodd Team (jodd.sf.net). All Rights Reserved.
2

3 package jodd.io;
4
5 import java.io.*;
6 import java.net.URL JavaDoc;
7
8 /**
9  * File utilities.
10  */

11 public class FileUtil {
12
13     // ---------------------------------------------------------------- misc shortcuts
14

15     /**
16      * Checks if two files points to the same file.
17      */

18     public static boolean equals(String JavaDoc file1, String JavaDoc file2) {
19         return equals(new File(file1), new File(file2));
20     }
21
22     /**
23      * Checks if two files points to the same file.
24      */

25     public static boolean equals(File file1, File file2) {
26         try {
27             file1 = file1.getCanonicalFile();
28             file2 = file2.getCanonicalFile();
29         } catch (IOException ioex) {
30             return false;
31         }
32         return file1.equals(file2);
33     }
34
35     /**
36      * Converts file URLs to file. Ignores other schemes and returns <code>null</code>.
37      */

38     public static File toFile(URL JavaDoc url) {
39         if ((url == null) || (url.getProtocol().equals("file") == false)) {
40             return null;
41         }
42         String JavaDoc filename = url.getFile().replace('/', File.separatorChar);
43         int pos = 0;
44         while ((pos = filename.indexOf('%', pos)) >= 0) {
45             if (pos + 2 < filename.length()) {
46                 String JavaDoc hexStr = filename.substring(pos + 1, pos + 3);
47                 char ch = (char) Integer.parseInt(hexStr, 16);
48                 filename = filename.substring(0, pos) + ch + filename.substring(pos + 3);
49             }
50         }
51         return new File(filename);
52     }
53
54
55     // ---------------------------------------------------------------- mkdirs
56

57     /**
58      * Creates all folders at once.
59      */

60     public static void mkdirs(String JavaDoc dirs) throws IOException {
61         mkdirs(new File(dirs));
62     }
63     /**
64      * Creates all folders at once.
65      */

66     public static void mkdirs(File dirs) throws IOException {
67         if (dirs.exists()) {
68             if (dirs.isDirectory() == false) {
69                 throw new IOException("Directory '" + "' is not a directory.");
70             }
71             return;
72         }
73         if (dirs.mkdirs() == false) {
74             throw new IOException("Unable to create directory '" + dirs + "'.");
75         }
76     }
77
78     /**
79      * Creates single folder.
80      */

81     public static void mkdir(String JavaDoc dir) throws IOException {
82         mkdir(new File(dir));
83     }
84     /**
85      * Creates single folders.
86      */

87     public static void mkdir(File dir) throws IOException {
88         if (dir.exists()) {
89             if (dir.isDirectory() == false) {
90                 throw new IOException("Destination '" + "' is not a directory.");
91             }
92             return;
93         }
94         if (dir.mkdir() == false) {
95             throw new IOException("Unable to create directory '" + dir + "'.");
96         }
97     }
98
99     // ---------------------------------------------------------------- touch
100

101     public static void touch(String JavaDoc file) throws IOException {
102         touch(new File(file));
103     }
104
105     /**
106      * Implements the Unix "touch" utility. It creates a new file
107      * with size 0 or, if the file exists already, it is opened and
108      * closed without modifying it, but updating the file date and time.
109      */

110     public static void touch(File file) throws IOException {
111         if (file == null) {
112             throw new IOException("Destination is null.");
113         }
114         if (file.exists() == false) {
115             StreamUtil.close(new FileOutputStream(file));
116         }
117         file.setLastModified(System.currentTimeMillis());
118     }
119
120
121
122     // ---------------------------------------------------------------- settings
123

124     /**
125      * Inner settings describe behaviour of the FileUtil class.
126      */

127     public static class Settings implements Cloneable JavaDoc {
128         private Settings() {}
129
130         // should destination file have the same timestamp as source
131
protected boolean preserveDate = true;
132         // overwrite existing destination
133
protected boolean overwriteExisting = true;
134         // create missing subdirectories of destination
135
protected boolean createDirs = true;
136         // use recursive directory copying and deleting
137
protected boolean recursive = true;
138         // don't stop on error and continue job as much as possible
139
protected boolean continueOnError = true;
140         // default encoding for reading/writing strings
141
protected String JavaDoc encoding = "ISO-8859-1";
142
143
144         public boolean isPreserveDate() {
145             return preserveDate;
146         }
147         public Settings setPreserveDate(boolean preserveDate) {
148             this.preserveDate = preserveDate;
149             return this;
150         }
151         public Settings dontPerserveDate() {
152             this.preserveDate = false;
153             return this;
154         }
155
156         public boolean isOverwriteExisting() {
157             return overwriteExisting;
158         }
159         public Settings setOverwriteExisting(boolean overwriteExisting) {
160             this.overwriteExisting = overwriteExisting;
161             return this;
162         }
163         public Settings dontOverwrite() {
164             this.overwriteExisting = false;
165             return this;
166         }
167
168         public boolean isCreateDirs() {
169             return createDirs;
170         }
171         public Settings setCreateDirs(boolean createDirs) {
172             this.createDirs = createDirs;
173             return this;
174         }
175         public Settings dontCreateDirs() {
176             this.createDirs = false;
177             return this;
178         }
179
180         public boolean isRecursive() {
181             return recursive;
182         }
183         public Settings setRecursive(boolean recursive) {
184             this.recursive = recursive;
185             return this;
186         }
187         public Settings dontRecurse() {
188             this.recursive = false;
189             return this;
190         }
191
192         public boolean isContinueOnError() {
193             return continueOnError;
194         }
195         public Settings setContinueOnError(boolean continueOnError) {
196             this.continueOnError = continueOnError;
197             return this;
198         }
199         public Settings stopOnError() {
200             this.continueOnError = false;
201             return this;
202         }
203
204
205         public String JavaDoc getEncoding() {
206             return encoding;
207         }
208         public Settings setEncoding(String JavaDoc encoding) {
209             this.encoding = encoding;
210             return this;
211         }
212
213         // ------------------------------------------------------------ clone
214

215         public Object JavaDoc clone() throws CloneNotSupportedException JavaDoc {
216             Object JavaDoc clone = super.clone();
217             //noinspection RedundantStringConstructorCall
218
((Settings) clone).encoding = new String JavaDoc(this.encoding);
219             return clone;
220         }
221     }
222
223     // default global settings
224
public static Settings settings = new Settings();
225
226     /**
227      * Creates new {@link Settings} instance by clonning current default settings.
228      */

229     public static Settings cloneSettings() {
230         try {
231             return (Settings) settings.clone();
232         } catch (CloneNotSupportedException JavaDoc cnsex) {
233             return null;
234         }
235     }
236
237     /**
238      * Creates new {@link Settings} instance with default values.
239      */

240     public static Settings newSettings() {
241         return new Settings();
242     }
243
244     // ---------------------------------------------------------------- copy file to file
245

246     public static void copyFile(String JavaDoc src, String JavaDoc dest) throws IOException {
247         copyFile(new File(src), new File(dest), settings);
248     }
249
250     public static void copyFile(String JavaDoc src, String JavaDoc dest, Settings settings) throws IOException {
251         copyFile(new File(src), new File(dest), settings);
252     }
253
254     public static void copyFile(File src, File dest) throws IOException {
255         copyFile(src, dest, settings);
256     }
257
258     /**
259      * Copies a file to another file with specified copy settings.
260      */

261     public static void copyFile(File src, File dest, Settings settings) throws IOException {
262         checkFileCopy(src, dest, settings);
263         doCopyFile(src, dest, settings);
264     }
265
266     private static void checkFileCopy(File src, File dest, Settings settings) throws IOException {
267         if (src == null) {
268             throw new IOException("Source is null.");
269         }
270         if (dest == null) {
271             throw new IOException("Destination is null.");
272         }
273         if (src.exists() == false) {
274             throw new FileNotFoundException("Source '" + src + "' does not exist.");
275         }
276         if (src.isFile() == false) {
277             throw new IOException("Source '" + src + "' is not a file.");
278         }
279         if (equals(src, dest) == true) {
280             throw new IOException("Source '" + src + "' and destination '" + dest + "' are the same.");
281         }
282
283         File destParent = dest.getParentFile();
284         if (destParent != null && destParent.exists() == false) {
285             if (settings.createDirs == false) {
286                 throw new IOException("Destination directory '" + destParent + "' doesn't exist.");
287             }
288             if (destParent.mkdirs() == false) {
289                 throw new IOException("Destination directory '" + destParent + "' cannot be created.");
290             }
291         }
292     }
293
294     /**
295      * Internal file copy when most of the pre-checking has passed.
296      */

297     private static void doCopyFile(File src, File dest, Settings settings) throws IOException {
298         if (dest.exists()) {
299             if (dest.isDirectory()) {
300                 throw new IOException("Destination '" + dest + "' is a directory.");
301             }
302             if (settings.overwriteExisting == false) {
303                 throw new IOException("Destination '" + dest + "' already exists.");
304             }
305         }
306
307         doCopy(src, dest);
308
309         if (src.length() != dest.length()) {
310             throw new IOException("Copying of '" + src + "' to '" + dest + "' failed due to different sizes.");
311         }
312         if (settings.preserveDate) {
313             dest.setLastModified(src.lastModified());
314         }
315     }
316
317     // ---------------------------------------------------------------- simple copy file
318

319     /**
320      * Copies one file to another without any checking.
321      * @see #doCopy(java.io.File, java.io.File)
322      */

323     protected static void doCopy(String JavaDoc src, String JavaDoc dest) throws IOException {
324         doCopy(new File(src), new File(dest));
325     }
326
327     /**
328      * Copies one file to another without any checking. It is assumed that
329      * both parameters represents valid files.
330      */

331     protected static void doCopy(File src, File dest) throws IOException {
332         FileInputStream input = new FileInputStream(src);
333         try {
334             FileOutputStream output = new FileOutputStream(dest);
335             try {
336                 StreamUtil.copy(input, output);
337             } finally {
338                 StreamUtil.close(output);
339             }
340         } finally {
341             StreamUtil.close(input);
342         }
343     }
344
345
346     // ---------------------------------------------------------------- copy file to directory
347

348     public static void copyFileToDir(String JavaDoc src, String JavaDoc destDir) throws IOException {
349         copyFileToDir(new File(src), new File(destDir), settings);
350     }
351     public static void copyFileToDir(String JavaDoc src, String JavaDoc destDir, Settings settings) throws IOException {
352         copyFileToDir(new File(src), new File(destDir), settings);
353     }
354
355     public static void copyFileToDir(File src, File destDir) throws IOException {
356         copyFileToDir(src, destDir, settings);
357     }
358     /**
359      * Copies a file to folder with specified copy settings.
360      */

361     public static void copyFileToDir(File src, File destDir, Settings settings) throws IOException {
362         if (destDir == null) {
363             throw new IOException("Destination is null.");
364         }
365         if (destDir.exists() && destDir.isDirectory() == false) {
366             throw new IOException("Destination '" + destDir + "' is not a directory.");
367         }
368         copyFile(src, new File(destDir, src.getName()), settings);
369     }
370
371
372     // ---------------------------------------------------------------- copy dir
373

374
375     public static void copyDir(String JavaDoc srcDir, String JavaDoc destDir) throws IOException {
376         copyDir(new File(srcDir), new File(destDir), settings);
377     }
378
379     public static void copyDir(String JavaDoc srcDir, String JavaDoc destDir, Settings settings) throws IOException {
380         copyDir(new File(srcDir), new File(destDir), settings);
381     }
382
383     public static void copyDir(File srcDir, File destDir) throws IOException {
384         copyDir(srcDir, destDir, settings);
385     }
386
387     /**
388      * Copies directory with specified copy settings.
389      */

390     public static void copyDir(File srcDir, File destDir, Settings settings) throws IOException {
391         checkDirCopy(srcDir, destDir);
392         doCopyDirectory(srcDir, destDir, settings);
393     }
394
395     private static void checkDirCopy(File srcDir, File destDir) throws IOException {
396         if (srcDir == null) {
397             throw new IOException("Source is null.");
398         }
399         if (destDir == null) {
400             throw new IOException("Destination is null.");
401         }
402         if (srcDir.exists() == false) {
403             throw new FileNotFoundException("Source '" + srcDir + "' does not exist.");
404         }
405         if (srcDir.isDirectory() == false) {
406             throw new IOException("Source '" + srcDir + "' is not a directory.");
407         }
408         if (equals(srcDir, destDir) == true) {
409             throw new IOException("Source '" + srcDir + "' and destination '" + destDir + "' are the same.");
410         }
411     }
412
413     private static void doCopyDirectory(File srcDir, File destDir, Settings settings) throws IOException {
414         if (destDir.exists()) {
415             if (destDir.isDirectory() == false) {
416                 throw new IOException("Destination '" + destDir + "' is not a directory.");
417             }
418         } else {
419             if (settings.createDirs == false) {
420                 throw new IOException("Destination '" + destDir + "' doesn't exists.");
421             }
422             if (destDir.mkdirs() == false) {
423                 throw new IOException("Destination '" + destDir + "' directory cannot be created.");
424             }
425             if (settings.preserveDate) {
426                 destDir.setLastModified(srcDir.lastModified());
427             }
428         }
429
430         File[] files = srcDir.listFiles();
431         if (files == null) {
432             throw new IOException("Failed to list contents of '" + srcDir + '\'');
433         }
434
435         IOException exception = null;
436         for (int i = 0; i < files.length; i++) {
437             File destFile = new File(destDir, files[i].getName());
438             try {
439                 if (files[i].isDirectory()) {
440                     if (settings.recursive == true) {
441                         doCopyDirectory(files[i], destFile, settings);
442                     }
443                 } else {
444                     doCopyFile(files[i], destFile, settings);
445                 }
446             } catch (IOException ioex) {
447                 if (settings.continueOnError == true) {
448                     exception = ioex;
449                     continue;
450                 }
451                 throw ioex;
452             }
453         }
454
455         if (exception != null) {
456             throw exception;
457         }
458     }
459
460
461
462     // ---------------------------------------------------------------- move file
463

464     public static void moveFile(String JavaDoc src, String JavaDoc dest) throws IOException {
465         moveFile(new File(src), new File(dest), settings);
466     }
467
468     public static void moveFile(String JavaDoc src, String JavaDoc dest, Settings settings) throws IOException {
469         moveFile(new File(src), new File(dest), settings);
470     }
471
472     public static void moveFile(File src, File dest) throws IOException {
473         moveFile(src, dest, settings);
474     }
475
476     public static void moveFile(File src, File dest, Settings settings) throws IOException {
477         checkFileCopy(src, dest, settings);
478         doMoveFile(src, dest, settings);
479     }
480
481     private static void doMoveFile(File src, File dest, Settings settings) throws IOException {
482         if (dest.exists()) {
483             if (dest.isFile() == false) {
484                 throw new IOException("Destination '" + dest + "' is not a file.");
485             }
486             if (settings.overwriteExisting == false) {
487                 throw new IOException("Destination '" + dest + "' already exists.");
488             }
489             dest.delete();
490         }
491
492         if (src.renameTo(dest) == false) {
493             throw new IOException("Moving of '" + src + "' to '" + dest + "' failed.");
494         }
495     }
496
497     // ---------------------------------------------------------------- move file to dir
498

499
500     public static void moveFileToDir(String JavaDoc src, String JavaDoc destDir) throws IOException {
501         moveFileToDir(new File(src), new File(destDir), settings);
502     }
503     public static void moveFileToDir(String JavaDoc src, String JavaDoc destDir, Settings settings) throws IOException {
504         moveFileToDir(new File(src), new File(destDir), settings);
505     }
506
507     public static void moveFileToDir(File src, File destDir) throws IOException {
508         moveFileToDir(src, destDir, settings);
509     }
510     public static void moveFileToDir(File src, File destDir, Settings settings) throws IOException {
511         if (destDir == null) {
512             throw new IOException("Destination is null.");
513         }
514         if (destDir.exists() && destDir.isDirectory() == false) {
515             throw new IOException("Destination '" + destDir + "' is not a directory.");
516         }
517         moveFile(src, new File(destDir, src.getName()), settings);
518     }
519
520
521
522
523     // ---------------------------------------------------------------- move dir
524

525     public static void moveDir(String JavaDoc srcDir, String JavaDoc destDir) throws IOException {
526         moveDir(new File(srcDir), new File(destDir));
527     }
528
529     public static void moveDir(File srcDir, File destDir) throws IOException {
530         checkDirCopy(srcDir, destDir);
531         doMoveDirectory(srcDir, destDir);
532     }
533
534     private static void doMoveDirectory(File src, File dest) throws IOException {
535         if (dest.exists()) {
536             if (dest.isDirectory() == false) {
537                 throw new IOException("Destination '" + dest + "' is not a directory.");
538             }
539             dest = new File(dest, dest.getName());
540             dest.mkdir();
541         }
542
543         if (src.renameTo(dest) == false) {
544             throw new IOException("Moving of '" + src + "' to '" + dest + "' failed.");
545         }
546     }
547
548
549
550     // ---------------------------------------------------------------- delete file
551

552     public static void deleteFile(String JavaDoc dest) throws IOException {
553         deleteFile(new File(dest));
554     }
555
556     public static void deleteFile(File dest) throws IOException {
557         if (dest == null) {
558             throw new IOException("Destination is null.");
559         }
560         if (dest.exists() == false) {
561             throw new FileNotFoundException("Destination '" + dest + "' doesn't exist");
562         }
563         if (dest.isFile() == false) {
564             throw new IOException("Destination '" + dest + "' is not a file.");
565         }
566         if (dest.delete() == false) {
567             throw new IOException("Unable to delete '" + dest + "'.");
568         }
569     }
570
571
572     // ---------------------------------------------------------------- delete dir
573

574     public static void deleteDir(String JavaDoc dest) throws IOException {
575         deleteDir(new File(dest), settings);
576     }
577     public static void deleteDir(String JavaDoc dest, Settings settings) throws IOException {
578         deleteDir(new File(dest), settings);
579     }
580     public static void deleteDir(File dest) throws IOException {
581         deleteDir(dest, settings);
582     }
583     /**
584      * Deletes a directory.
585      */

586     public static void deleteDir(File dest, Settings settings) throws IOException {
587         cleanDir(dest, settings);
588         if (dest.delete() == false) {
589             throw new IOException("Unable to delete '" + dest + "'.");
590         }
591     }
592
593
594
595     public static void cleanDir(String JavaDoc dest) throws IOException {
596         cleanDir(new File(dest), settings);
597     }
598
599     public static void cleanDir(String JavaDoc dest, Settings settings) throws IOException {
600         cleanDir(new File(dest), settings);
601     }
602
603     public static void cleanDir(File dest) throws IOException {
604         cleanDir(dest, settings);
605     }
606
607     /**
608      * Cleans a directory without deleting it.
609      */

610     public static void cleanDir(File dest, Settings settings) throws IOException {
611         if (dest == null) {
612             throw new IOException("Destination is null.");
613         }
614         if (dest.exists() == false) {
615             throw new FileNotFoundException("Destination '" + dest + "' doesn't exists.");
616         }
617
618         if (dest.isDirectory() == false) {
619             throw new IOException("Destination '" + dest + "' is not a directory.");
620         }
621
622         File[] files = dest.listFiles();
623         if (files == null) {
624             throw new IOException("Failed to list contents of '" + dest + "'.");
625         }
626
627         IOException exception = null;
628         for (int i = 0; i < files.length; i++) {
629             try {
630                 if (files[i].isDirectory()) {
631                     if (settings.recursive == true) {
632                         deleteDir(files[i], settings);
633                     }
634                 } else {
635                     files[i].delete();
636                 }
637             } catch (IOException ioex) {
638                 if (settings.continueOnError == true) {
639                     exception = ioex;
640                     continue;
641                 }
642                 throw ioex;
643             }
644         }
645
646         if (exception != null) {
647             throw exception;
648         }
649     }
650
651     // ---------------------------------------------------------------- read/write string
652

653
654     public static String JavaDoc readString(String JavaDoc source) throws IOException {
655         return readString(new File(source), settings.encoding);
656     }
657
658     public static String JavaDoc readString(String JavaDoc source, String JavaDoc encoding) throws IOException {
659         return readString(new File(source), encoding);
660     }
661
662     public static String JavaDoc readString(File source) throws IOException {
663         return readString(source, settings.encoding);
664     }
665
666     public static String JavaDoc readString(File source, String JavaDoc encoding) throws IOException {
667         if (source == null) {
668             throw new IOException("Source '" + source + "' is null.");
669         }
670         if (source.exists() == false) {
671             throw new FileNotFoundException("Source '" + source + "' doesn't exist.");
672         }
673         if (source.isFile() == false) {
674             throw new IOException("Source '" + source + "' is not a file.");
675         }
676         long len = source.length();
677         if (len >= Integer.MAX_VALUE) {
678             len = Integer.MAX_VALUE;
679         }
680         FileInputStream in = null;
681         try {
682             in = new FileInputStream(source);
683             StringWriter sw = new StringWriter((int) len);
684             StreamUtil.copy(in, sw, encoding);
685             return sw.toString();
686         } finally {
687             StreamUtil.close(in);
688         }
689     }
690
691
692     public static void writeString(String JavaDoc dest, String JavaDoc data) throws IOException {
693         writeString(new File(dest), data, settings.encoding);
694     }
695
696     public static void writeString(String JavaDoc dest, String JavaDoc data, String JavaDoc encoding) throws IOException {
697         writeString(new File(dest), data, encoding);
698     }
699
700     public static void writeString(File dest, String JavaDoc data) throws IOException {
701         writeString(dest, data, settings.encoding);
702     }
703
704     public static void writeString(File dest, String JavaDoc data, String JavaDoc encoding) throws IOException {
705         if (dest == null) {
706             throw new IOException("Destination '" + dest + "' is null.");
707         }
708         if (dest.exists() == true) {
709             if (dest.isFile() == false) {
710                 throw new IOException("Destination '" + dest + "' exist, but it is not a file.");
711             }
712         }
713         FileOutputStream out = null;
714         try {
715             out = new FileOutputStream(dest);
716             out.write(data.getBytes(encoding));
717         } finally {
718             StreamUtil.close(out);
719         }
720     }
721
722     // ---------------------------------------------------------------- read/write bytearray
723

724
725     public static byte[] readBytes(String JavaDoc file) throws IOException {
726         return readBytes(new File(file));
727     }
728
729     public static byte[] readBytes(File source) throws IOException {
730         if (source == null) {
731             throw new IOException("Source '" + source + "' is null.");
732         }
733         if (source.exists() == false) {
734             throw new FileNotFoundException("Source '" + source + "' doesn't exist.");
735         }
736         if (source.isFile() == false) {
737             throw new IOException("Source '" + source + "' exists, but it is not a file.");
738         }
739         long len = source.length();
740         if (len >= Integer.MAX_VALUE) {
741             throw new IOException("Source size is greater then max array size.");
742         }
743         FileInputStream in = null;
744         try {
745             in = new FileInputStream(source);
746             return StreamUtil.readBytes(in);
747         } finally {
748             StreamUtil.close(in);
749         }
750     }
751
752
753
754     public static void writeBytes(String JavaDoc dest, byte[] data) throws IOException {
755         writeBytes(new File(dest), data, 0, data.length);
756     }
757
758     public static void writeBytes(String JavaDoc dest, byte[] data, int off, int len) throws IOException {
759         writeBytes(new File(dest), data, off, len);
760     }
761
762     public static void writeBytes(File dest, byte[] data) throws IOException {
763         writeBytes(dest, data, 0, data.length);
764     }
765
766     public static void writeBytes(File dest, byte[] data, int off, int len) throws IOException {
767         if (dest == null) {
768             throw new IOException("Destination '" + dest + "' is null.");
769         }
770         if (dest.exists() == true) {
771             if (dest.isFile() == false) {
772                 throw new IOException("Destination '" + dest + "' exist but it is not a file.");
773             }
774         }
775         FileOutputStream out = null;
776         try {
777             out = new FileOutputStream(dest);
778             out.write(data, off, len);
779         } finally {
780             StreamUtil.close(out);
781         }
782     }
783
784     // ---------------------------------------------------------------- equals content
785

786     public static boolean compare(String JavaDoc file1, String JavaDoc file2) throws IOException {
787         return compare(new File(file1), new File(file2));
788     }
789
790     /**
791      * Compare the contents of two files to determine if they are equal or
792      * not.
793      * <p>
794      * This method checks to see if the two files are different lengths
795      * or if they point to the same file, before resorting to byte-by-byte
796      * comparison of the contents.
797      * <p>
798      * Code origin: Avalon
799      */

800     public static boolean compare(File file1, File file2) throws IOException {
801         if ((file1 == null) || (file2 == null)) {
802             throw new IOException("One of the files is null.");
803         }
804         boolean file1Exists = file1.exists();
805         if (file1Exists != file2.exists()) {
806             return false;
807         }
808
809         if (file1Exists == false) {
810             return true;
811         }
812
813         if ((file1.isFile() == false) || (file2.isFile() == false)) {
814             throw new IOException("Only files can be compared.");
815         }
816
817         if (file1.length() != file2.length()) {
818             return false;
819         }
820
821         if (equals(file1, file1)) {
822             return true;
823         }
824
825         InputStream input1 = null;
826         InputStream input2 = null;
827         try {
828             input1 = new FileInputStream(file1);
829             input2 = new FileInputStream(file2);
830             return StreamUtil.compare(input1, input2);
831         } finally {
832             StreamUtil.close(input1);
833             StreamUtil.close(input2);
834         }
835     }
836
837     // ---------------------------------------------------------------- time
838

839     public static boolean isNewer(String JavaDoc file, String JavaDoc reference) {
840         return isNewer(new File(file), new File(reference));
841     }
842
843     /**
844      * Test if specified <code>File</code> is newer than the reference <code>File</code>.
845      *
846      * @param file the <code>File</code> of which the modification date must be compared
847      * @param reference the <code>File</code> of which the modification date is used
848      * @return <code>true</code> if the <code>File</code> exists and has been modified more
849      * recently than the reference <code>File</code>.
850      */

851     public static boolean isNewer(File file, File reference) {
852         if (reference == null) {
853             throw new IllegalArgumentException JavaDoc("Reference file is null.");
854         }
855         if (reference.exists() == false) {
856             throw new IllegalArgumentException JavaDoc("The reference file '" + file + "' doesn't exist");
857         }
858         return isNewer(file, reference.lastModified());
859     }
860
861
862     public static boolean isOlder(String JavaDoc file, String JavaDoc reference) {
863         return isOlder(new File(file), new File(reference));
864     }
865
866     public static boolean isOlder(File file, File reference) {
867         if (reference == null) {
868             throw new IllegalArgumentException JavaDoc("Reference file is null.");
869         }
870         if (reference.exists() == false) {
871             throw new IllegalArgumentException JavaDoc("The reference file '" + file + "' doesn't exist");
872         }
873         return isOlder(file, reference.lastModified());
874     }
875
876     /**
877      * Tests if the specified <code>File</code> is newer than the specified time reference.
878      *
879      * @param file the <code>File</code> of which the modification date must be compared.
880      * @param timeMillis the time reference measured in milliseconds since the
881      * epoch (00:00:00 GMT, January 1, 1970)
882      * @return <code>true</code> if the <code>File</code> exists and has been modified after
883      * the given time reference.
884      */

885     public static boolean isNewer(File file, long timeMillis) {
886         if (file == null) {
887             throw new IllegalArgumentException JavaDoc("File is null.");
888         }
889         if (!file.exists()) {
890             return false;
891         }
892         return file.lastModified() > timeMillis;
893     }
894
895     public static boolean isNewer(String JavaDoc file, long timeMillis) {
896         return isNewer(new File(file), timeMillis);
897     }
898
899
900     public static boolean isOlder(File file, long timeMillis) {
901         if (file == null) {
902             throw new IllegalArgumentException JavaDoc("File is null.");
903         }
904         if (!file.exists()) {
905             return false;
906         }
907         return file.lastModified() < timeMillis;
908     }
909
910     public static boolean isOlder(String JavaDoc file, long timeMillis) {
911         return isOlder(new File(file), timeMillis);
912     }
913
914
915     // ---------------------------------------------------------------- smart copy
916

917     public static void copy(String JavaDoc src, String JavaDoc dest) throws IOException {
918         copy(new File(src), new File(dest), settings);
919     }
920
921     public static void copy(String JavaDoc src, String JavaDoc dest, Settings settings) throws IOException {
922         copy(new File(src), new File(dest), settings);
923     }
924
925     public static void copy(File src, File dest) throws IOException {
926         copy(src, dest, settings);
927     }
928     /**
929      * Smart copy. If source is a directory, copy it to destination.
930      * Otherwise, if destination is directory, copy source file to it.
931      * Otherwise, try to copy source file to destination file.
932      */

933     public static void copy(File src, File dest, Settings settings) throws IOException {
934         if (src == null) {
935             throw new IOException("Source file is null.");
936         }
937         if (dest == null) {
938             throw new IOException("Destination file is null.");
939         }
940         if (src.isDirectory() == true) {
941             copyDir(src, dest, settings);
942             return;
943         }
944         if (dest.isDirectory() == true) {
945             copyFileToDir(src, dest, settings);
946             return;
947         }
948         copyFile(src, dest, settings);
949     }
950
951     // ---------------------------------------------------------------- smart move
952

953     public static void move(String JavaDoc src, String JavaDoc dest) throws IOException {
954         move(new File(src), new File(dest), settings);
955     }
956
957     public static void move(String JavaDoc src, String JavaDoc dest, Settings settings) throws IOException {
958         move(new File(src), new File(dest), settings);
959     }
960
961     public static void move(File src, File dest) throws IOException {
962         move(src, dest, settings);
963     }
964     /**
965      * Smart move. If source is a directory, move it to destination.
966      * Otherwise, if destination is directory, move source file to it.
967      * Otherwise, try to move source file to destination file.
968      */

969     public static void move(File src, File dest, Settings settings) throws IOException {
970         if (src == null) {
971             throw new IOException("Source file is null.");
972         }
973         if (dest == null) {
974             throw new IOException("Destination file is null.");
975         }
976         if (src.isDirectory() == true) {
977             moveDir(src, dest);
978             return;
979         }
980         if (dest.isDirectory() == true) {
981             moveFileToDir(src, dest, settings);
982             return;
983         }
984         moveFile(src, dest, settings);
985     }
986
987
988     // ---------------------------------------------------------------- smart delete
989

990     public static void delete(String JavaDoc dest) throws IOException {
991         delete(new File(dest), settings);
992     }
993
994     public static void delete(String JavaDoc dest, Settings settings) throws IOException {
995         delete(new File(dest), settings);
996     }
997
998     public static void delete(File dest) throws IOException {
999         delete(dest, settings);
1000    }
1001
1002    /**
1003     * Smart delete of destination file or directory.
1004     */

1005    public static void delete(File dest, Settings settings) throws IOException {
1006        if (dest == null) {
1007            throw new IOException("Destination is null.");
1008        }
1009        if (dest.isDirectory()) {
1010            deleteDir(dest, settings);
1011            return;
1012        }
1013        deleteFile(dest);
1014    }
1015}
1016
1017
1018
Popular Tags