1 package jodd.file; 2 3 import jodd.util.Util; 4 5 import java.io.BufferedInputStream; 6 import java.io.BufferedOutputStream; 7 import java.io.BufferedReader; 8 import java.io.BufferedWriter; 9 import java.io.File; 10 import java.io.FileInputStream; 11 import java.io.FileNotFoundException; 12 import java.io.FileOutputStream; 13 import java.io.FileReader; 14 import java.io.FileWriter; 15 import java.io.IOException; 16 import java.io.InputStreamReader; 17 import java.io.ObjectInputStream; 18 import java.io.ObjectOutputStream; 19 import java.io.OutputStreamWriter; 20 21 24 public final class FileUtil { 25 26 28 33 public static String getWorkingDir() { 34 return System.getProperty("user.dir"); 35 } 36 37 42 public static File getWorkingDirFile() { 43 return new File(System.getProperty("user.dir")); 44 } 45 46 53 public static long getFileSize(String fileName) { 54 return new File(fileName).length(); 55 } 56 57 64 public static long getFileSize(File file) { 65 return file.length(); 66 } 67 68 76 public static boolean equals(String file1, String file2) { 77 return equals(new File(file1), new File(file2)); 78 } 79 87 public static boolean equals(File file1, File file2) { 88 try { 89 file1 = file1.getCanonicalFile(); 90 file2 = file2.getCanonicalFile(); 91 } catch (IOException e) { 92 return false; 93 } 94 return file1.equals(file2); 95 } 96 97 104 public static boolean mkdirs(String dirs) { 105 return new File(dirs).mkdirs(); 106 } 107 114 public static boolean mkdirs(File dirs) { 115 return dirs.mkdirs(); 116 } 117 118 125 public static boolean mkdir(String dir) { 126 return new File(dir).mkdir(); 127 } 128 135 public static boolean mkdir(File dir) { 136 return dir.mkdir(); 137 } 138 139 140 142 145 public static int FILE_BUFFER_SIZE = 32 * 1024; 146 147 156 public static boolean copy(String fileIn, String fileOut) { 157 return copy(new File(fileIn), new File(fileOut), FILE_BUFFER_SIZE, true); 158 } 159 168 public static boolean copySafe(String fileIn, String fileOut) { 169 return copy(new File(fileIn), new File(fileOut), FILE_BUFFER_SIZE, false); 170 } 171 172 182 public static boolean copy(String fileIn, String fileOut, int bufsize) { 183 return copy(new File(fileIn), new File(fileOut), bufsize, true); 184 } 185 195 public static boolean copySafe(String fileIn, String fileOut, int bufsize) { 196 return copy(new File(fileIn), new File(fileOut), bufsize, false); 197 } 198 199 208 public static boolean copy(File fileIn, File fileOut) { 209 return copy(fileIn, fileOut, FILE_BUFFER_SIZE, true); 210 } 211 220 public static boolean copySafe(File fileIn, File fileOut) { 221 return copy(fileIn, fileOut, FILE_BUFFER_SIZE, false); 222 } 223 224 234 public static boolean copy(File fileIn, File fileOut, int bufsize) { 235 return copy(fileIn, fileOut, bufsize, true); 236 } 237 247 public static boolean copySafe(File fileIn, File fileOut, int bufsize) { 248 return copy(fileIn, fileOut, bufsize, false); 249 } 250 251 252 254 266 public static boolean copy(String fileIn, String fileOut, int bufsize, boolean overwrite) { 267 return copy(new File(fileIn), new File(fileOut), bufsize, overwrite); 268 } 269 270 284 public static boolean copy(File fileIn, File fileOut, int bufsize, boolean overwrite) { 285 if (fileIn.exists() == false) { 287 return false; 288 } 289 290 if (fileIn.isFile() == false) { 292 return false; 293 } 294 295 if (fileOut.isDirectory() == true) { 297 fileOut = new File(fileOut.getPath() + File.separator + fileIn.getName()); 298 } 299 300 if (overwrite == false) { 301 if (fileOut.exists() == true) { 302 return false; 303 } 304 } else { 305 if (fileOut.exists()) { try { 307 if (fileIn.getCanonicalFile().equals(fileOut.getCanonicalFile()) == true) { 308 return true; 309 } 310 } catch (IOException ioex) { 311 return false; 312 } 313 } 314 } 315 return copyFile(fileIn, fileOut, bufsize); 316 } 317 318 320 328 public static boolean copyFile(String fileIn, String fileOut) { 329 return copyFile(new File(fileIn), new File(fileOut), FILE_BUFFER_SIZE); 330 } 331 339 public static boolean copyFile(File fileIn, File fileOut) { 340 return copyFile(fileIn, fileOut, FILE_BUFFER_SIZE); 341 } 342 351 public static boolean copyFile(String fileIn, String fileOut, int bufsize) { 352 return copyFile(new File(fileIn), new File(fileOut), bufsize); 353 } 354 355 364 public static boolean copyFile(File fileIn, File fileOut, int bufsize) { 365 FileInputStream in = null; 366 FileOutputStream out = null; 367 boolean result = false; 368 try { 369 in = new FileInputStream(fileIn); 370 out = new FileOutputStream(fileOut); 371 Util.copyPipe(in, out, bufsize); 372 result = true; 373 } catch(IOException ioex) { 374 } finally { 375 if (out != null) { 376 try { 377 out.close(); 378 } catch(IOException ioex) { 379 } 380 } 381 if (in != null) { 382 try { 383 in.close(); 384 } catch(IOException ioex) { 385 } 386 } 387 } 388 return result; 389 } 390 391 393 394 public static boolean move(String fileNameIn, String fileNameOut) { 395 return move(new File(fileNameIn), new File(fileNameOut), true); 396 } 397 public static boolean moveSafe(String fileNameIn, String fileNameOut) { 398 return move(new File(fileNameIn), new File(fileNameOut), false); 399 } 400 401 public static boolean move(File fileIn, File fileOut) { 402 return move(fileIn, fileOut, true); 403 } 404 public static boolean moveSafe(File fileIn, File fileOut) { 405 return move(fileIn, fileOut, false); 406 } 407 408 410 420 public static boolean move(String fileNameIn, String fileNameOut, boolean overwrite) { 421 return move(new File(fileNameIn), new File(fileNameOut), overwrite); 422 } 423 424 436 public static boolean move(File fileIn, File fileOut, boolean overwrite) { 437 if (fileIn.exists() == false) { 439 return false; 440 } 441 442 if (fileIn.isFile() == false) { 444 return false; 445 } 446 447 if (fileOut.isDirectory() == true) { 449 fileOut = new File(fileOut.getPath() + File.separator + fileIn.getName()); 450 } 451 452 if (overwrite == false) { 453 if (fileOut.exists() == true) { 454 return false; 455 } 456 } else { 457 if (fileOut.exists()) { try { 459 if (fileIn.getCanonicalFile().equals(fileOut.getCanonicalFile()) == true) { 460 return true; 461 } else { 462 fileOut.delete(); } 464 } catch (IOException ioex) { 465 return false; 466 } 467 } 468 } 469 470 return fileIn.renameTo(fileOut); 471 } 472 473 475 483 public static boolean moveFile(String src, String dest) { 484 return new File(src).renameTo(new File(dest)); 485 } 486 487 495 public static boolean moveFile(File src, File dest) { 496 return src.renameTo(dest); 497 } 498 499 501 public static boolean moveDir(String fileIn, String fileOut) { 502 return moveDir(new File(fileIn), new File(fileOut)); 503 } 504 505 515 public static boolean moveDir(File fileIn, File fileOut) { 516 if (fileIn.exists() == false) { 518 return false; 519 } 520 521 if (fileIn.isDirectory() == false) { 523 return false; 524 } 525 526 if (fileOut.exists() == true) { 528 try { 529 if (fileIn.getCanonicalFile().equals(fileOut.getCanonicalFile()) == true) { 530 return true; 531 } else { 532 return false; 533 } 534 } catch (IOException ioex) { 535 return false; 536 } 537 } 538 539 return fileIn.renameTo(fileOut); 540 } 541 542 543 public static boolean copyDir(String srcDir, String dstDir) { 544 return copyDir(new File(srcDir), new File(dstDir)); 545 } 546 547 556 public static boolean copyDir(File srcDir, File dstDir) { 557 if (srcDir.isDirectory()) { 558 if (!dstDir.exists()) { 559 dstDir.mkdir(); 560 } 561 String[] files = srcDir.list(); 562 for (int i = 0; i < files.length; i++) { 563 if (copyDir(new File(srcDir, files[i]), new File(dstDir, files[i])) == false) { 564 return false; 565 } 566 } 567 return true; 568 } 569 return copyFile(srcDir, dstDir); 570 } 571 572 573 575 580 public static boolean delete(String fileName) { 581 return delete(new File(fileName)); 582 } 583 584 589 public static boolean delete(File fileIn) { 590 return fileIn.delete(); 591 } 592 593 598 public static boolean deleteDir(String pathName) { 599 return deleteDir(new File(pathName)); 600 } 601 602 611 public static boolean deleteDir(File path) { 612 if (path.isDirectory()) { 613 File[] files = path.listFiles(); 614 for (int i = 0; i < files.length; i++) { 615 if (deleteDir(files[i]) == false) { 616 return false; 617 } 618 } 619 } 620 return path.delete(); 621 } 622 623 624 626 629 public static int STRING_BUFFER_SIZE = 32 * 1024; 630 631 640 public static String readString(String fileName) throws IOException { 641 return readString(new File(fileName), STRING_BUFFER_SIZE); 642 } 643 644 654 public static String readString(String fileName, int bufferSize) throws IOException { 655 return readString(new File(fileName), bufferSize); 656 } 657 658 667 public static String readString(File file) throws IOException { 668 return readString(file, STRING_BUFFER_SIZE); 669 } 670 671 681 public static String readString(File file, int bufferSize) throws IOException { 682 long fileLen = file.length(); 683 if (fileLen <= 0L) { 684 if (file.exists() == true) { 685 return ""; } 687 return null; } 689 if (fileLen > Integer.MAX_VALUE) { throw new IOException("File too big for loading into a String!"); 691 } 692 693 FileReader fr = null; 694 BufferedReader brin = null; 695 char[] buf = null; 696 try { 697 fr = new FileReader(file); 698 brin = new BufferedReader(fr, bufferSize); 699 int length = (int) fileLen; 700 buf = new char[length]; 701 brin.read(buf, 0, length); 702 } finally { 703 if (brin != null) { 704 brin.close(); 705 fr = null; 706 } 707 if (fr != null) { 708 fr.close(); 709 } 710 } 711 return new String(buf); 712 } 713 714 723 public static void writeString(String fileName, String s) throws IOException { 724 writeString(new File(fileName), s, STRING_BUFFER_SIZE); 725 } 726 727 737 public static void writeString(String fileName, String s, int bufferSize) throws IOException { 738 writeString(new File(fileName), s, bufferSize); 739 } 740 741 750 public static void writeString(File file, String s) throws IOException { 751 writeString(file, s, STRING_BUFFER_SIZE); 752 } 753 754 764 public static void writeString(File file, String s, int bufferSize) throws IOException { 765 FileWriter fw = null; 766 BufferedWriter out = null; 767 if (s == null) { 768 return; 769 } 770 try { 771 fw = new FileWriter(file); 772 out = new BufferedWriter(fw, bufferSize); 773 out.write(s); 774 } finally { 775 if (out != null) { 776 out.close(); 777 fw = null; 778 } 779 if (fw != null) { 780 fw.close(); 781 } 782 } 783 } 784 785 787 796 public static String readString(String fileName, String encoding) throws IOException { 797 return readString(new File(fileName), STRING_BUFFER_SIZE, encoding); 798 } 799 800 810 public static String readString(String fileName, int bufferSize, String encoding) throws IOException { 811 return readString(new File(fileName), bufferSize, encoding); 812 } 813 814 823 public static String readString(File file, String encoding) throws IOException { 824 return readString(file, STRING_BUFFER_SIZE, encoding); 825 } 826 827 840 public static String readString(File file, int bufferSize, String encoding) throws IOException { 841 long fileLen = file.length(); 842 if (fileLen <= 0L) { 843 if (file.exists() == true) { 844 return ""; } 846 return null; } 848 if (fileLen > Integer.MAX_VALUE) { throw new IOException("File too big for loading into a String!"); 850 } 851 852 FileInputStream fis = null; 853 InputStreamReader isr = null; 854 BufferedReader brin = null; 855 856 int length = (int) fileLen; 857 char[] buf = null; 858 int realSize = 0; 859 try { 860 fis = new FileInputStream(file); 861 isr = new InputStreamReader(fis, encoding); 862 brin = new BufferedReader(isr, bufferSize); 863 buf = new char[length]; int c; while ((c = brin.read()) != -1) { 866 buf[realSize] = (char) c; 867 realSize++; 868 } 869 } finally { 870 if (brin != null) { 871 brin.close(); 872 isr = null; 873 fis = null; 874 } 875 if (isr != null) { 876 isr.close(); 877 fis = null; 878 } 879 if (fis != null) { 880 fis.close(); 881 } 882 } 883 return new String(buf, 0, realSize); 884 } 885 886 895 public static void writeString(String fileName, String s, String encoding) throws IOException { 896 writeString(new File(fileName), s, STRING_BUFFER_SIZE, encoding); 897 } 898 899 909 public static void writeString(String fileName, String s, int bufferSize, String encoding) throws IOException { 910 writeString(new File(fileName), s, bufferSize, encoding); 911 } 912 913 922 public static void writeString(File file, String s, String encoding) throws IOException { 923 writeString(file, s, STRING_BUFFER_SIZE, encoding); 924 } 925 926 936 public static void writeString(File file, String s, int bufferSize, String encoding) throws IOException { 937 if (s == null) { 938 return; 939 } 940 FileOutputStream fos = null; 941 OutputStreamWriter osw = null; 942 BufferedWriter out = null; 943 try { 944 fos = new FileOutputStream(file); 945 osw = new OutputStreamWriter(fos, encoding); 946 out = new BufferedWriter(osw, bufferSize); 947 out.write(s); 948 } finally { 949 if (out != null) { 950 out.close(); 951 osw = null; 952 fos = null; 953 } 954 if (osw != null) { 955 osw.close(); 956 fos = null; 957 } 958 if (fos != null) { 959 fos.close(); 960 } 961 } 962 } 963 964 965 967 970 public static int OBJECT_BUFFER_SIZE = 32 * 1024; 971 972 980 public static void writeObject(String f, Object o) throws IOException { 981 writeObject(f, o, OBJECT_BUFFER_SIZE); 982 } 983 984 994 public static void writeObject(String f, Object o, int bufferSize) throws IOException { 995 FileOutputStream fos = null; 996 BufferedOutputStream bos = null; 997 ObjectOutputStream oos = null; 998 try { 999 fos = new FileOutputStream(f); 1000 bos = new BufferedOutputStream(fos, bufferSize); 1001 oos = new ObjectOutputStream(bos); 1002 oos.writeObject(o); 1003 } finally { 1004 if (oos != null) { 1005 oos.close(); 1006 bos = null; 1007 fos = null; 1008 } 1009 if (bos != null) { 1010 bos.close(); 1011 fos = null; 1012 } 1013 if (fos != null) { 1014 fos.close(); 1015 } 1016 } 1017 } 1018 1019 1020 1028 public static Object readObject(String f) throws IOException, ClassNotFoundException, FileNotFoundException { 1029 return readObject(f, OBJECT_BUFFER_SIZE); 1030 } 1031 1032 1043 public static Object readObject(String f, int bufferSize) throws IOException, ClassNotFoundException, FileNotFoundException { 1044 Object result = null; 1045 FileInputStream fis = null; 1046 BufferedInputStream bis = null; 1047 ObjectInputStream ois = null; 1048 try { 1049 fis = new FileInputStream(f); 1050 bis = new BufferedInputStream(fis, bufferSize); 1051 ois = new ObjectInputStream(bis); 1052 result = ois.readObject(); 1053 } finally { 1054 if (ois != null) { 1055 ois.close(); 1056 bis = null; 1057 fis = null; 1058 } 1059 if (bis != null) { 1060 bis.close(); 1061 fis = null; 1062 } 1063 if (fis != null) { 1064 fis.close(); 1065 } 1066 } 1067 return result; 1068 } 1069 1070 1072 1073 1081 public static final byte[] readBytes(String s) throws IOException { 1082 return readBytes(new File(s)); 1083 } 1084 1085 1093 public static final byte[] readBytes(File file) throws IOException { 1094 FileInputStream fileinputstream = new FileInputStream(file); 1095 long l = file.length(); 1096 if (l > Integer.MAX_VALUE) { 1097 throw new IOException("File too big for loading into a byte array!"); 1098 } 1099 byte byteArray[] = new byte[(int)l]; 1100 int i = 0; 1101 for (int j = 0; (i < byteArray.length) && (j = fileinputstream.read(byteArray, i, byteArray.length - i)) >= 0; i += j); 1102 if (i < byteArray.length) { 1103 throw new IOException("Could not completely read the file " + file.getName()); 1104 } 1105 fileinputstream.close(); 1106 return byteArray; 1107 } 1108 1109 1110 1111 1112 public static void writeBytes(String filename, byte[] source) throws IOException { 1113 if (source == null) { 1114 return; 1115 } 1116 writeBytes(new File(filename), source, 0, source.length); 1117 } 1118 1119 public static void writeBytes(File file, byte[] source) throws IOException { 1120 if (source == null) { 1121 return; 1122 } 1123 writeBytes(file, source, 0, source.length); 1124 } 1125 1126 1127 public static void writeBytes(String filename, byte[] source, int offset, int len) throws IOException { 1128 writeBytes(new File(filename), source, offset, len); 1129 } 1130 1131 public static void writeBytes(File file, byte[] source, int offset, int len) throws IOException { 1132 if (len < 0) { 1133 throw new IOException("File size is negative!"); 1134 } 1135 if (offset + len > source.length) { 1136 len = source.length - offset; 1137 } 1138 FileOutputStream fos = null; 1139 try { 1140 fos = new FileOutputStream(file); 1141 fos.write(source, offset, len); 1142 } finally { 1143 if (fos != null) { 1144 fos.close(); 1145 } 1146 } 1147 return; 1148 } 1149 1150 1151} 1152 1153 1154 | Popular Tags |