1 16 package org.apache.commons.io; 17 18 import java.io.File ; 19 import java.io.FileInputStream ; 20 import java.io.FileNotFoundException ; 21 import java.io.FileOutputStream ; 22 import java.io.IOException ; 23 import java.io.InputStream ; 24 import java.io.FileFilter ; 25 import java.io.OutputStream ; 26 import java.net.URL ; 27 import java.util.Collection ; 28 import java.util.Date ; 29 30 import org.apache.commons.io.filefilter.DirectoryFileFilter; 31 import org.apache.commons.io.filefilter.FalseFileFilter; 32 import org.apache.commons.io.filefilter.FileFilterUtils; 33 import org.apache.commons.io.filefilter.IOFileFilter; 34 import org.apache.commons.io.filefilter.SuffixFileFilter; 35 import org.apache.commons.io.filefilter.TrueFileFilter; 36 37 83 public class FileUtils { 84 85 88 public FileUtils() { } 89 90 93 public static final long ONE_KB = 1024; 94 95 98 public static final long ONE_MB = ONE_KB * ONE_KB; 99 100 103 public static final long ONE_GB = ONE_KB * ONE_MB; 104 105 113 public static String byteCountToDisplaySize(long size) { 114 String displaySize; 115 116 if (size / ONE_GB > 0) { 117 displaySize = String.valueOf(size / ONE_GB) + " GB"; 118 } else if (size / ONE_MB > 0) { 119 displaySize = String.valueOf(size / ONE_MB) + " MB"; 120 } else if (size / ONE_KB > 0) { 121 displaySize = String.valueOf(size / ONE_KB) + " KB"; 122 } else { 123 displaySize = String.valueOf(size) + " bytes"; 124 } 125 126 return displaySize; 127 } 128 129 130 137 public static void touch(File file) throws IOException { 138 OutputStream out = new java.io.FileOutputStream (file); 139 IOUtils.closeQuietly(out); 140 } 141 142 143 private static void innerListFiles(Collection files, File directory, IOFileFilter filter) { 144 File [] found = directory.listFiles((FileFilter )filter); 145 for (int i = 0; i < found.length; i++) { 146 if (found[i].isDirectory()) { 147 innerListFiles(files, found[i], filter); 148 } else { 149 files.add(found[i]); 150 } 151 } 152 } 153 154 155 162 public static File [] convertFileCollectionToFileArray(Collection files) { 163 return (File [])files.toArray(new File [files.size()]); 164 } 165 166 167 192 public static Collection listFiles(File directory, IOFileFilter fileFilter, IOFileFilter dirFilter) { 193 if (!directory.isDirectory()) { 194 throw new IllegalArgumentException ("Parameter 'directory' is not a directory"); 195 } 196 if (fileFilter == null) { 197 throw new NullPointerException ("Parameter 'fileFilter' is null"); 198 } 199 200 IOFileFilter effFileFilter = FileFilterUtils.andFileFilter(fileFilter, 202 FileFilterUtils.notFileFilter(DirectoryFileFilter.INSTANCE)); 203 204 IOFileFilter effDirFilter; 206 if (dirFilter == null) { 207 effDirFilter = FalseFileFilter.INSTANCE; 208 } else { 209 effDirFilter = FileFilterUtils.andFileFilter(dirFilter, 210 DirectoryFileFilter.INSTANCE); 211 } 212 213 Collection files = new java.util.LinkedList (); 215 innerListFiles(files, directory, 216 FileFilterUtils.orFileFilter(effFileFilter, effDirFilter)); 217 return files; 218 } 219 220 221 227 private static String [] toSuffixes(String [] extensions) { 228 String [] suffixes = new String [extensions.length]; 229 for (int i = 0; i < extensions.length; i++) { 230 suffixes[i] = "." + extensions[i]; 231 } 232 return suffixes; 233 } 234 235 236 245 public static Collection listFiles(File directory, String [] extensions, boolean recursive) { 246 IOFileFilter filter; 247 if (extensions == null) { 248 filter = TrueFileFilter.INSTANCE; 249 } else { 250 String [] suffixes = toSuffixes(extensions); 251 filter = new SuffixFileFilter(suffixes); 252 } 253 return listFiles(directory, filter, 254 (recursive ? TrueFileFilter.INSTANCE : FalseFileFilter.INSTANCE)); 255 } 256 257 258 267 public static boolean contentEquals(File file1, File file2) 268 throws IOException { 269 boolean file1Exists = file1.exists(); 270 if (file1Exists != file2.exists()) { 271 return false; 272 } 273 274 if (!file1Exists) { 275 return true; 277 } 278 279 if (file1.isDirectory() || file2.isDirectory()) { 280 throw new IOException ("Can't compare directories, only files"); 282 } 283 284 InputStream input1 = null; 285 InputStream input2 = null; 286 try { 287 input1 = new java.io.FileInputStream (file1); 288 input2 = new java.io.FileInputStream (file2); 289 return IOUtils.contentEquals(input1, input2); 290 291 } finally { 292 IOUtils.closeQuietly(input1); 293 IOUtils.closeQuietly(input2); 294 } 295 } 296 297 303 public static File toFile(URL url) { 304 if (url.getProtocol().equals("file") == false) { 305 return null; 306 } else { 307 String filename = 308 url.getFile().replace('/', File.separatorChar); 309 return new File (filename); 310 } 311 } 312 313 320 public static URL [] toURLs(File [] files) throws IOException { 321 URL [] urls = new URL [files.length]; 322 323 for (int i = 0; i < urls.length; i++) { 324 urls[i] = files[i].toURL(); 325 } 326 327 return urls; 328 } 329 330 331 345 public static void copyFileToDirectory( 346 File source, 347 File destinationDirectory) 348 throws IOException { 349 if (destinationDirectory.exists() 350 && !destinationDirectory.isDirectory()) { 351 throw new IllegalArgumentException ("Destination is not a directory"); 352 } 353 354 copyFile(source, new File (destinationDirectory, source.getName()), true); 355 } 356 357 374 public static void copyFile(File source, File destination) 375 throws IOException { 376 copyFile(source, destination, true); 377 } 378 379 380 398 public static void copyFile(File source, File destination, boolean preserveFileDate) 399 throws IOException { 400 if (!source.exists()) { 402 String message = "File " + source + " does not exist"; 403 throw new FileNotFoundException (message); 404 } 405 406 if (destination.getParentFile() != null 408 && !destination.getParentFile().exists()) { 409 destination.getParentFile().mkdirs(); 410 } 411 412 if (destination.exists() && !destination.canWrite()) { 414 String message = 415 "Unable to open file " + destination + " for writing."; 416 throw new IOException (message); 417 } 418 419 if (source.getCanonicalPath().equals(destination.getCanonicalPath())) { 421 String message = 422 "Unable to write file " + source + " on itself."; 423 throw new IOException (message); 424 } 425 426 FileInputStream input = new FileInputStream (source); 427 try { 428 FileOutputStream output = new FileOutputStream (destination); 429 try { 430 CopyUtils.copy(input, output); 431 } finally { 432 IOUtils.closeQuietly(output); 433 } 434 } finally { 435 IOUtils.closeQuietly(input); 436 } 437 438 if (source.length() != destination.length()) { 439 String message = 440 "Failed to copy full contents from " 441 + source 442 + " to " 443 + destination; 444 throw new IOException (message); 445 } 446 447 if (preserveFileDate) { 448 destination.setLastModified(source.lastModified()); 450 } 451 } 452 453 469 public static void copyURLToFile(URL source, File destination) 470 throws IOException { 471 if (destination.getParentFile() != null 473 && !destination.getParentFile().exists()) { 474 destination.getParentFile().mkdirs(); 475 } 476 477 if (destination.exists() && !destination.canWrite()) { 479 String message = 480 "Unable to open file " + destination + " for writing."; 481 throw new IOException (message); 482 } 483 484 InputStream input = source.openStream(); 485 try { 486 FileOutputStream output = new FileOutputStream (destination); 487 try { 488 CopyUtils.copy(input, output); 489 } finally { 490 IOUtils.closeQuietly(output); 491 } 492 } finally { 493 IOUtils.closeQuietly(input); 494 } 495 } 496 497 498 503 public static void deleteDirectory(File directory) 504 throws IOException { 505 if (!directory.exists()) { 506 return; 507 } 508 509 cleanDirectory(directory); 510 if (!directory.delete()) { 511 String message = 512 "Unable to delete directory " + directory + "."; 513 throw new IOException (message); 514 } 515 } 516 517 522 public static void cleanDirectory(File directory) 523 throws IOException { 524 if (!directory.exists()) { 525 String message = directory + " does not exist"; 526 throw new IllegalArgumentException (message); 527 } 528 529 if (!directory.isDirectory()) { 530 String message = directory + " is not a directory"; 531 throw new IllegalArgumentException (message); 532 } 533 534 IOException exception = null; 535 536 File [] files = directory.listFiles(); 537 for (int i = 0; i < files.length; i++) { 538 File file = files[i]; 539 try { 540 forceDelete(file); 541 } catch (IOException ioe) { 542 exception = ioe; 543 } 544 } 545 546 if (null != exception) { 547 throw exception; 548 } 549 } 550 551 560 public static boolean waitFor(File file, int seconds) { 561 int timeout = 0; 562 int tick = 0; 563 while (!file.exists()) { 564 if (tick++ >= 10) { 565 tick = 0; 566 if (timeout++ > seconds) { 567 return false; 568 } 569 } 570 try { 571 Thread.sleep(100); 572 } catch (InterruptedException ignore) {} catch (Exception ex) { 573 break; 574 } 575 } 576 return true; 577 } 578 579 580 597 public static String readFileToString( 598 File file, String encoding) throws IOException { 599 InputStream in = new java.io.FileInputStream (file); 600 try { 601 return IOUtils.toString(in, encoding); 602 } finally { 603 IOUtils.closeQuietly(in); 604 } 605 } 606 607 624 public static void writeStringToFile(File file, 625 String data, String encoding) throws IOException { 626 OutputStream out = new java.io.FileOutputStream (file); 627 try { 628 out.write(data.getBytes(encoding)); 629 } finally { 630 IOUtils.closeQuietly(out); 631 } 632 } 633 634 649 public static void forceDelete(File file) throws IOException { 650 if (file.isDirectory()) { 651 deleteDirectory(file); 652 } else { 653 if (!file.exists()) { 654 throw new FileNotFoundException ("File does not exist: " + file); 655 } 656 if (!file.delete()) { 657 String message = 658 "Unable to delete file: " + file; 659 throw new IOException (message); 660 } 661 } 662 } 663 664 670 public static void forceDeleteOnExit(File file) throws IOException { 671 if (file.isDirectory()) { 672 deleteDirectoryOnExit(file); 673 } else { 674 file.deleteOnExit(); 675 } 676 } 677 678 683 private static void deleteDirectoryOnExit(File directory) 684 throws IOException { 685 if (!directory.exists()) { 686 return; 687 } 688 689 cleanDirectoryOnExit(directory); 690 directory.deleteOnExit(); 691 } 692 693 698 private static void cleanDirectoryOnExit(File directory) 699 throws IOException { 700 if (!directory.exists()) { 701 String message = directory + " does not exist"; 702 throw new IllegalArgumentException (message); 703 } 704 705 if (!directory.isDirectory()) { 706 String message = directory + " is not a directory"; 707 throw new IllegalArgumentException (message); 708 } 709 710 IOException exception = null; 711 712 File [] files = directory.listFiles(); 713 for (int i = 0; i < files.length; i++) { 714 File file = files[i]; 715 try { 716 forceDeleteOnExit(file); 717 } catch (IOException ioe) { 718 exception = ioe; 719 } 720 } 721 722 if (null != exception) { 723 throw exception; 724 } 725 } 726 727 728 734 public static void forceMkdir(File directory) throws IOException { 735 if (directory.exists()) { 736 if (directory.isFile()) { 737 String message = 738 "File " 739 + directory 740 + " exists and is " 741 + "not a directory. Unable to create directory."; 742 throw new IOException (message); 743 } 744 } else { 745 if (false == directory.mkdirs()) { 746 String message = 747 "Unable to create directory " + directory; 748 throw new IOException (message); 749 } 750 } 751 } 752 753 759 public static long sizeOfDirectory(File directory) { 760 if (!directory.exists()) { 761 String message = directory + " does not exist"; 762 throw new IllegalArgumentException (message); 763 } 764 765 if (!directory.isDirectory()) { 766 String message = directory + " is not a directory"; 767 throw new IllegalArgumentException (message); 768 } 769 770 long size = 0; 771 772 File [] files = directory.listFiles(); 773 for (int i = 0; i < files.length; i++) { 774 File file = files[i]; 775 776 if (file.isDirectory()) { 777 size += sizeOfDirectory(file); 778 } else { 779 size += file.length(); 780 } 781 } 782 783 return size; 784 } 785 786 796 public static boolean isFileNewer(File file, File reference) { 797 if (reference == null) { 798 throw new IllegalArgumentException ("No specified reference file"); 799 } 800 if (!reference.exists()) { 801 throw new IllegalArgumentException ("The reference file '" + file + "' doesn't exist"); 802 } 803 804 return isFileNewer(file, reference.lastModified()); 805 } 806 807 816 public static boolean isFileNewer(File file, Date date) { 817 if (date == null) { 818 throw new IllegalArgumentException ("No specified date"); 819 } 820 return isFileNewer(file, date.getTime()); 821 } 822 823 833 public static boolean isFileNewer(File file, long timeMillis) { 834 if (file == null) { 835 throw new IllegalArgumentException ("No specified file"); 836 } 837 if (!file.exists()) { 838 return false; 839 } 840 841 return file.lastModified() > timeMillis; 842 } 843 844 } 845 | Popular Tags |