Commons IO is a utility library that helps develop IO functions.
It mainly includes six aspects:
- Utility class - perform common tasks using static methods
- input - useful input stream and reader implementation
- output - useful output stream and writer implementation
- Filter Various implementations of file filter
- comparator Various implementations of java.util.Comparator for files
- File Watcher - Components for monitoring file system events
Official API address:
http://commons.apache.org/proper/commons-io/javadocs/api-release/index.html?org/apache/commons/io/FileUtils.html
User manual: http://commons.apache.org/proper/commons-io/description.html
/* Writing file * 1.Only three full parameter forms are listed here. api provides method overloading of partial parameters * 2.The last Boolean parameter is whether it is append mode or not * 3.If the target file does not exist, FileUtils will be created automatically * */ //static void:write(File file, CharSequence data, String encoding, boolean append) FileUtils.write(new File("D:/a/b/cxyapi.txt"), "Program switching api","UTF-8",true); //static void:writeLines(File file, Collection<?> lines, boolean append) List lines=new ArrayList(); lines.add("Welcome to visit:");lines.add("blog.gzczy.top"); FileUtils.writeLines(new File("D:/a/b/cxyapi.txt"),lines,true); //static void:writeStringToFile(File file, String data, String encoding, boolean append) FileUtils.writeStringToFile(new File("D:/a/b/cxyapi.txt"), "Author: czy", "UTF-8",true);
//Read files or folders //static String:readFileToString(File file, String encoding) System.out.println(FileUtils.readFileToString(new File("D:/a/b/cxyapi.txt"), "UTF-8")); //static List :readLines(File file, String encoding) System.out.println(FileUtils.readLines(new File("D:/a/b/cxyapi.txt"), "UTF-8")); //Return a list
The common way we used to delete files / folders was to delete them recursively, repeatedly traverse subfolders to find and delete files one by one
//Delete directory //static void:deleteDirectory(File directory) FileUtils.deleteDirectory(new File("D:/not/cxyapi")); //Note: when deleting files under path in this way, if the file read / write stream under path is not closed, it cannot be deleted; //The second way (to solve the above problems, force deletion): //static boolean:deleteQuietly(File file) FileUtils.deleteQuietly(new File("D:/not/cxyapi")); //The folder is not empty and can be deleted without exception
//Move files or folders //static void: moveDirectory(File srcDir, File destDir) FileUtils.moveDirectory(new File("D:/cxyapi1"), new File("D:/cxyapi2")); //Note that if the second parameter file does not exist, an exception will be thrown //static void:moveDirectoryToDirectory(File src, File destDir, boolean createDestDir) FileUtils.moveDirectoryToDirectory(new File("D:/cxyapi2"), new File("D:/cxyapi3"), true); /* The difference between the above two methods is: * moveDirectory: D:/cxyapi2 The content in is the content of D / cxyapi1. * moveDirectoryToDirectory: D:/cxyapi2 Move folder to D:/cxyapi3 * * The following three are relatively simple, no examples are provided, only the api is provided * The difference between moveToDirectory and others is that it can automatically identify operation files or folders */ //static void:moveFileToDirectory(srcFile, destDir, createDestDir) //static void:moveFile(File srcFile, File destFile) //static void:moveToDirectory(File src, File destDir, boolean createDestDir)
//The result is that cxyapi and cxyapi1 are in the same directory FileUtils.copyDirectory(new File("D:/cxyapi"), new File("D:/cxyapi1")); //The result is to copy cxyapi to cxyapi2 FileUtils.copyDirectoryToDirectory(new File("D:/cxyapi"), new File("D:/cxyapi2")); //Copy file FileUtils.copyFile(new File("d:/cxyapi.xml"), new File("d:/cxyapi.xml.bak")); //Copy files to directory FileUtils.copyFileToDirectory(new File("d:/cxyapi.xml"), new File("d:/cxyapi")); //Copy url to file FileUtils.copyURLToFile(new URL("https://blog.gzczy.topl"), new File("d:/cxyapi.xml"));
To view the original: https://blog.gzczy.top/index.php/2018/11/22/apache-commons-io%e4%bd%bf%e7%94%a8%e5%b0%8f%e6%8a%80%e5%b7%a7/