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
- /* Write files
- * 1. Here are only 3 ways full Parameter form, API provides partial parameter of method overload
- * 2. The last Boolean parameter is whether the Append mode
- * 3. If the target file does not exist, Fileutils will automatically create
- * */
- Static void:write (file file, charsequence data, String encoding, Boolean append)
- Fileutils.write (new File ("D:/a/b/cxyapi.txt"), "program for API","UTF-8",true);
- Static void:writelines (file file, collection<?> lines, Boolean append)
- list<string> lines=New arraylist<string> ();
- Lines.add ("Welcome to visit:"); Lines.add ("www.cxyapi.com");
- 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"), "Cxy", "UTF-8",true);
2. read files/folders
Java code
- Read the file
- Static string:readfiletostring (file file, String encoding)
- System.out.println (fileutils.readfiletostring (new File ("D:/a/b/cxyapi.txt"), "UTF-8"));
- Static list<string>:readlines (file file, String encoding)
- System.out.println (Fileutils.readlines (new File ("D:/a/b/cxyapi.txt"), "UTF-8")); //Returns a list
3. Delete files/folders
Java code
- Delete Directory
- Static void:deletedirectory (File directory)
- Fileutils.deletedirectory (new File ("D:/not/cxyapi"));
- Static boolean:deletequietly (file file)
- 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
- Move a file or folder
- Static void:movedirectory (file Srcdir, file DestDir)
- Fileutils.movedirectory (New file ("D:/cxyapi1"), new file ("D:/cxyapi2")); //Note that the second parameter file does not exist and throws an exception
- Static void:movedirectorytodirectory (file src, file DestDir, Boolean createdestdir)
- Fileutils.movedirectorytodirectory (New file ("D:/cxyapi2"), new file ("D:/cxyapi3"), true);
- /* The above two methods are different:
- * The contents of the Movedirectory:d:/cxyapi2 are d:/cxyapi1 content.
- * Movedirectorytodirectory:d:/cxyapi2 folder moved to D:/cxyapi3
- *
- * The following 3 are relatively simple no examples provided, only the API
- * The difference between movetodirectory and other is that it can automatically identify the action file or the folder
- */
- Static Void:movefiletodirectory (Srcfile, DestDir, Createdestdir)
- Static void:movefile (file srcfile, file destfile)
- Static void:movetodirectory (file src, file DestDir, Boolean createdestdir)
5.copy
Java code
- 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 a copy of the Cxyapi to Cxyapi2.
- Fileutils.copydirectorytodirectory (New file ("D:/cxyapi"), new file ("D:/cxyapi2"));
- Copy files
- Fileutils.copyfile (New file ("D:/cxyapi.xml"), new file ("D:/cxyapi.xml.bak"));
- Copy files to the directory
- Fileutils.copyfiletodirectory (New file ("D:/cxyapi.xml"), new file ("D:/cxyapi"));
- Copy URL to file
- Fileutils.copyurltofile (new URL ("Http://www.cxyapi.com/rss/cxyapi.xml"), new File ("d:/ Cxyapi.xml "));
6. Other
Java code
- Determine if a file or folder is included
- Boolean b=fileutils.directorycontains (new file ("D:/cxyapi"), new file ("D:/cxyapi/cxyapi.txt"));
- System.out.println (b);
- Get temp directory and user directory
- System.out.println (Fileutils.gettempdirectorypath ());
- System.out.println (Fileutils.getuserdirectorypath ());
- Open stream, create file and its directory structure if not present
- The second parameter indicates whether the file stream is an append method
- FileOutputStream Fos=fileutils.openoutputstream (new File ("D:/cxyapi/cxyapi.txt"),true);
- Fos.write (new String ("Welcome to: Www.cxyapi.com\r\n"). GetBytes ());
- Fos.close ();
- File or folder size
- System.out.println (fileutils.sizeof ("New File") ("D:/cxyapi"));
- System.out.println (Fileutils.sizeofdirectory ("New File") ("D:/cxyapi" ));
Java uses fileutils files to manipulate artifacts