Apache commons-io provides many classes. Here we only introduce the fileutils class.
The fileutils class provides some practical methods to operate on file objects, including File Reading, writing, copying, and comparison.
For example, you can use the following method to read and write a file one by one:
File file = new File("E:/data/a.csv");List lines = FileUtils.readLines(file,"UTF-8");FileUtils.writeLines(file2, lines, true);
Among them, public static void writelines (File file, collection <?> Lines, Boolean append) throws ioexception
You can write a file one by one. If the Boolean append parameter is set to "true", the file is added instead of the new file.
When merging multiple files, you must set it to true. If you only operate on a single file, you do not need to set this parameter.
The following example describes how to use commons-Io to read and write a single file.
Requirement: Read the content in the "E:/data/station once/ABA Tibetan ..csv" file and write it to "E:/Data // bus stop .csv ".
1. Create a Java project.
2. Export jar package, commons-io-2.4.jar.
3. The Code is as follows:
Public class fileoperation {public static void main (string [] ARGs) {sigle ();} public static void sigle () {file file1 = new file ("E: /data/station once/ABA Tibetan zu Autonomous Region .csv "); file file2 = new file (" E:/Data // bus stop .csv "); try {list lines = fileutils. readlines (file1, "GBK"); fileutils. writelines (file2, lines, true);} catch (ioexception E1) {e1.printstacktrace ();}}}