Java uses fileutils files to manipulate artifacts

Source: Internet
Author: User

Objective:

In the work we often encounter a lot of file operation, we also used to write some of their own defined tool class to simplify the file operation, in fact, Apache Commons Fileutils class is such a tool class, using it can greatly simplify our operation of the file.

Preparatory work:

1. Download jar Address: http://commons.apache.org/proper/commons-io/download_io.cgi

2. Import the Commons-io-2.4.jar file into your project

Description

1. Because it is a tool class use is very simple, so this article just classify it, show what it can provide to us.

2. Accustomed to look at the API can directly see the official API Portal

3. Take a look at the official Guidelines guide Portal

4.FileUtils is just one of the Commons-io tools

Description of the classification demo:

1. write Files/folders

Java code
  1. /* Write files
  2. * 1. Here are only 3 ways full Parameter form, API provides partial parameter of method overload
  3. * 2. The last Boolean parameter is whether the Append mode
  4. * 3. If the target file does not exist, Fileutils will automatically create
  5. * */
  6. Static void:write (file file, charsequence data, String encoding, Boolean append)
  7. Fileutils.write (new File ("D:/a/b/cxyapi.txt"), "program for API","UTF-8",true);
  8. Static void:writelines (file file, collection<?> lines, Boolean append)
  9. list<string> lines=New arraylist<string> ();
  10. Lines.add ("Welcome to visit:"); Lines.add ("www.cxyapi.com");
  11. Fileutils.writelines (new File ("D:/a/b/cxyapi.txt"), lines,true);
  12. Static void:writestringtofile (file file, string data, String encoding, Boolean append)
  13. Fileutils.writestringtofile (new File ("D:/a/b/cxyapi.txt"), "Cxy", "UTF-8",true);

2. read files/folders

Java code
    1. Read the file
    2. Static string:readfiletostring (file file, String encoding)
    3. System.out.println (fileutils.readfiletostring (new File ("D:/a/b/cxyapi.txt"), "UTF-8"));
    4. Static list<string>:readlines (file file, String encoding)
    5. System.out.println (Fileutils.readlines (new File ("D:/a/b/cxyapi.txt"), "UTF-8")); //Returns a list

3. Delete files/folders

Java code
    1. Delete Directory
    2. Static void:deletedirectory (File directory)
    3. Fileutils.deletedirectory (new File ("D:/not/cxyapi"));
    4. Static boolean:deletequietly (file file)
    5. fileutils.deletequietly (new File ("D:/not/cxyapi")); //folder is not empty and can be deleted, never throws an exception

4. Move files/folders

Java code
  1. Move a file or folder
  2. Static void:movedirectory (file Srcdir, file DestDir)
  3. Fileutils.movedirectory (New file ("D:/cxyapi1"), new file ("D:/cxyapi2")); //Note that the second parameter file does not exist and throws an exception
  4. Static void:movedirectorytodirectory (file src, file DestDir, Boolean createdestdir)
  5. Fileutils.movedirectorytodirectory (New file ("D:/cxyapi2"), new file ("D:/cxyapi3"), true);
  6. /* The above two methods are different:
  7. * The contents of the Movedirectory:d:/cxyapi2 are d:/cxyapi1 content.
  8. * Movedirectorytodirectory:d:/cxyapi2 folder moved to D:/cxyapi3
  9. *
  10. * The following 3 are relatively simple no examples provided, only the API
  11. * The difference between movetodirectory and other is that it can automatically identify the action file or the folder
  12. */
  13. Static Void:movefiletodirectory (Srcfile, DestDir, Createdestdir)
  14. Static void:movefile (file srcfile, file destfile)
  15. Static void:movetodirectory (file src, file DestDir, Boolean createdestdir)

5.copy

Java code
  1. The result is that Cxyapi and cxyapi1 are in the same directory
  2. Fileutils.copydirectory (New file ("D:/cxyapi"), new file ("D:/cxyapi1"));
  3. The result is a copy of the Cxyapi to Cxyapi2.
  4. Fileutils.copydirectorytodirectory (New file ("D:/cxyapi"), new file ("D:/cxyapi2"));
  5. Copy files
  6. Fileutils.copyfile (New file ("D:/cxyapi.xml"), new file ("D:/cxyapi.xml.bak"));
  7. Copy files to the directory
  8. Fileutils.copyfiletodirectory (New file ("D:/cxyapi.xml"), new file ("D:/cxyapi"));
  9. Copy URL to file
  10. Fileutils.copyurltofile (new URL ("Http://www.cxyapi.com/rss/cxyapi.xml"), new File ("d:/  Cxyapi.xml "));

6. Other

Java code
  1. Determine if a file or folder is included
  2. Boolean b=fileutils.directorycontains (new file ("D:/cxyapi"), new file ("D:/cxyapi/cxyapi.txt"));
  3. System.out.println (b);
  4. Get temp directory and user directory
  5. System.out.println (Fileutils.gettempdirectorypath ());
  6. System.out.println (Fileutils.getuserdirectorypath ());
  7. Open stream, create file and its directory structure if not present
  8. The second parameter indicates whether the file stream is an append method
  9. FileOutputStream Fos=fileutils.openoutputstream (new File ("D:/cxyapi/cxyapi.txt"),true);
  10. Fos.write (new String ("Welcome to: Www.cxyapi.com\r\n"). GetBytes ());
  11. Fos.close ();
  12. File or folder size
  13. System.out.println (fileutils.sizeof ("New File") ("D:/cxyapi"));
  14. System.out.println (Fileutils.sizeofdirectory ("New File") ("D:/cxyapi" ));

Java uses fileutils files to manipulate artifacts

Related Article

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.