Apache commons-Io for reading and writing a single file

Source: Internet
Author: User

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 ();}}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.