Org. apache. commons. io -- FileUtils learning notes, fileutils
FileUtils Application
1. Write a file;
2. Read from the file;
3. Create a folder, including a folder;
4. Copy files and folders;
5. delete files and folders;
6. Get the file from the URL address;
7. list files and folders through file filters and extensions;
8. Compare the file content;
9. last modification time of the file;
10. Calculate the checksum.
1. copy a file or folder:
Example:
1 public class CopyFileorDirectory {2 public static void main (String [] args) throws Exception {3 File file1 = new File ("path1 "); 4 File file2 = new File ("path2"); 5 File file3 = new File ("path3"); 6 File file4 = new File ("path4 "); 7 File file5 = new File ("path5"); 8 // copy the File to the specified folder to save the time of the File date. 9 // copy the content of the specified source file to a file with the same name in the specified target directory. 10 // if not, create the target directory. If the target file exists, this method overwrites it. 11 FileUtils. copyFileToDirectory (file1, file2); // file life-free 12 // copy the file to a new place (rename the file) and save the file date. 13 FileUtils. copyFile (file1, file3); 14 15 // copy the folder to the specified directory. If the specified directory does not exist, create 16 FileUtils. copyDirectoryToDirectory (file2, file4); 17 18 // copy the folder to the specified directory and rename 19 FileUtils. copyDirectory (file4, file5); 20 21 // This method copies the specified source directory structure to the specified target directory. 22 FileUtils. copyDirectory (file4, file5, DirectoryFileFilter. DIRECTORY); 23 24 // copy the file 25 IOFileFilter txtSuffixFilter = FileFilterUtils specified in the first-level content in the folder. suffixFileFilter (". txt "); 26 IOFileFilter txtFiles = FileFilterUtils. and (FileFileFilter. FILE, txtSuffixFilter); 27 FileUtils. copyDirectory (file4, file5, txtFiles); 28 29 // copy the file directory structure and specify the suffix file 30 FileFilter filter = FileFilterUtils In the first directory of the folder. or (DirectoryFileFilt Er. DIRECTORY, txtFiles); 31 FileUtils. copyDirectory (file4, file5, filter, false); // The default value of preserveFileDate is true. 32 33 // copy the byte from the URL source to the file destination. If they do not exist, they are created to the destination directory. If it already exists, the file will be overwritten. 34 URL source = new URL ("http://imgsrc.baidu.com/baike/pic/ewe.jpg"); 35 FileUtils. copyURLToFile (source, file5,); 36 37 // wait until the NFS propagation file is created and the Force times out. This method repeats the test File. exists () until it returns true, or until the maximum time specified in seconds. 38 File file = new File ("/abc/"); 39 boolean d = FileUtils. waitFor (file, 100); 40 System. out. println (d); 41} 42}
Ii. Methods for deleting files or files
1 public class FileorDirectoryDelete {2 public static void main (String [] args) throws Exception {3 File file = new File ("path1 "); 4 File directory = new File ("path2"); 5 // recursively delete a directory (including content ). 6 FileUtils. deleteDirectory (directory); 7 8 // delete a file without throwing an exception. If the file is a directory, delete it and all subdirectories. 9 FileUtils. deleteQuietly (file); 10 11 // clear the content without deleting it. 12 FileUtils. cleanDirectory (directory); 13 14 // if you delete a file, an exception is thrown. 15 // if the file is a folder, all contents in the folder and folder are deleted. If file is a file, delete it. 16 // if a file/folder cannot be deleted for some reason, an exception 17 FileUtils. forceDelete (file); 18} 19} will be thrown}
3. Create a directory
1 public class CreatDirectory {2 public static void main (String [] args) throws Exception {3 File file = new File ("path"); 4 // create a folder, if it cannot be created for some reason, an exception is thrown. 5 // you can create a single-level or multi-level directory 6 FileUtils at a time. forceMkdir (new File ("/Users/wuguibin/Downloads/folder"); 7 // The parent directory of the File created for the specified File 8 FileUtils. forceMkdirParent (file); 9} 10}
4. Move files or folders
// Move the folder and rename FileUtils. moveDirectory (new File ("/Users/Downloads/file1"), new File ("/Users/Downloads/file2/file3"); // move the folder, and whether to rename FileUtils. moveDirectoryToDirectory (new File ("/Users/Downloads/file1"), new File ("/Users/Downloads/file2/"), false );
// Move the File to the specified folder and rename FileUtils. moveFile (file1, new File ("/Users/Downloads/sea grape. jpen "));
// Move the file to the specified folder and specify whether to create the FileUtils folder. moveFileToDirectory (new File ("/Users/Downloads/ .jpeg"), new File ("/Users/Downloads/file2"), false );
5. Determine whether the file is the same or contains a link, and obtain the file or folder size
// Determine whether the parent directory contains the specified child element (a file or directory ). That is, whether directory contains file2. Before comparison, files are standardized. Boolean a = FileUtils. directoryContains (directory, file2); // compare the content of the two files to determine whether they are the same. Boolean B = FileUtils. contentEquals (file1, file2)
// Obtain the size of the specified file or folder, which may overflow and change to a negative value
Long l = FileUtils. sizeOf (file1 );
System. out. println (l + "KB ");
// Obtain the size of the specified file or folder without Overflow
BigInteger bi = FileUtils. sizeOfAsBigInteger (file1 );
System. out. println (bi + "kb ");
// Recursively calculate the size of a directory (the total length of all files ).
// Note that sizeOfDirectory () does not detect overflow. If overflow occurs, the returned value may be negative. The sizeOfDirectoryAsBigInteger () method does not overflow.
FileUtils. sizeOfDirectory (file1 );
FileUtils. sizeOfDirectoryAsBigInteger (file1 );
6. Compare old and new files
// Compare whether the specified file is later than the time after the reference file is created or modified. boolean B = FileUtils. isFileNewer (file1, file2); // if the specified file is updated than the specified date. SimpleDateFormat date = new SimpleDateFormat ("yyyy/MM/dd"); String date1 = "2017/06/20"; boolean c = FileUtils. isFileNewer (file1, date. parse (date1); boolean d = FileUtils. isFileNewer (file1, 23243); // specifies whether the file is created or modified earlier than the Referenced File or date. isFileOlder (file1, 232434); FileUtils. isFileOlder (file1, System. currentTimeMillis ());
VII. Writing files
// Write the content in the set to the file and end the writing with the specified string
// Void writeLines (File file, Collection <?> Lines, String lineEnding, boolean append)
ArrayList <String> list = new ArrayList <> (); String str1 = "Java"; String str2 = "JSP"; list. add (str1); list. add (str2); FileUtils. writeLines (file8, "GBK", list, "java", true );
// Write the string to the file // parameter 1: the file to be written. If the file does not exist, it is automatically created. Parameter 2: content to be written // parameter 3: encoding format parameter 4: whether the append mode is used (ture: append mode, append the string to the end of the original content) string data1 = "serious"; FileUtils. writeStringToFile (file, data1, "UTF-8", true); // write the byte array into the file byte [] buf = {13,123, 34}; System. out. println (new String (buf); FileUtils. writeByteArrayToFile (file13, buf, 0, buf. length, true );
8. Reading files and retrieving input and output streams
// Read the object content into a string. String str = FileUtils. readFileToString (file, "UTF-16"); FileUtils. readFileToByteArray (file); // read the File to byte [] readFileToByteArray (final file) in the byte array // read the File as a string; Charset encoding: encoding format: String readFileToString (final File file, final Charset encoding) // read the File into a String set; Charset encoding: encoding format List <String> list4 = FileUtils. readLines (new File ("/Users/Shared/NOTE/java.txt"), "UTF-8"); Iterator it = list4.iterator (); while (it. hasNext () {Object obj = it. next (); System. out. println (obj);} // gets the input stream FileUtils. openInputStream (file); // gets the output stream FileUtils. openOutputStream (file );