Common operations for Apache.commons.io.FileUtils

Source: Internet
Author: User

As for the relevant jar package can be obtained from the official website http://commons.apache.org/downloads/index.html

Package com.wz.apache.fileUtils;

Import Org.apache.commons.io.FileUtils;
Import Org.apache.commons.io.IOUtils;

Import javax.print.attribute.standard.Copies;
Import java.io.*;
Import Java.net.URL;
Import java.util.List;
Import Java.util.Scanner;

/**
* Test IO Toolkit
* My English proficiency is not clear, simple understanding under
* (the first line of the note is the method of testing, the second line is the interpretation of the official document, the third line is the conclusion of my test)
* @author Wzlove
* @create 2018-04-30 12:30
*/
public class Maintest {

public static void Main (string[] args) {
String Path = "H:\\javatest\\iotest";
String path1 = "H:\\javatest\\iotest\\1.txt";
String path2 = "H:\\javatest\\iotest\\2.txt";
String path3 = "H:\\mytest";
String Path4 = "H:\\mytest\\test";
File File = new file (path);
File File1 = new file (path1);
File File2 = new file (path2);
File File3 = new file (PATH3);
File File4 = new file (PATH4);
FileUtils fu = new FileUtils ();

Copy operation

Cleandirectory (File directory)
Cleans a directory without deleting it.
Delete everything under the folder (if the path is a file or the path does not exist it will report java.lang.IllegalArgumentException exception)
try {
Fileutils.cleandirectory (file);
System.out.println ("delete succeeded");
} catch (IOException e) {
E.printstacktrace ();
}

Contentequals (file file1, file file2)
Compares the contents of the files to determine if they is equal or not.
Testing the contents of two files is different
try {
System.out.println (Fileutils.contentequals (file1,file2));
} catch (IOException e) {
E.printstacktrace ();
}

CopyDirectory (file Srcdir, file DestDir)
Copies a whole directory to a new location preserving the file dates.
Copy all the contents of the folder under the first parameter to the folder under the second parameter
try {
Fileutils.copydirectory (FILE,FILE3);
System.out.println ("copy succeeded");
} catch (IOException e) {
E.printstacktrace ();
}

Copydirectorytodirectory (file Srcdir, file DestDir)
Copies a directory to within another directory preserving the file dates.
Copy the entire folder of the first parameter (that is, the Iotest folder) to the second path
try {
Fileutils.copydirectorytodirectory (file, file3);
System.out.println ("copy succeeded");
} catch (IOException e) {
E.printstacktrace ();
}

CopyFile (file srcfile, file destfile)
Copies a file to a new location preserving the file date.
Copy the contents of one file to another file
try {
Fileutils.copyfile (File1, file2);
System.out.println ("copy succeeded");
} catch (IOException e) {
E.printstacktrace ();
}

Copyfiletodirectory (file srcfile, file DestDir)
Copies a file to a directory preserving the file date.
Copy a file to another folder (do not test it)

System.out.println ("==================================================");

Read and write operations

Readfiletostring (file file, String encoding)
Reads the contents of a file into a String.
Read file contents return string, output as-is
try {
String str = fileutils.readfiletostring (file1, "UTF-8");
System.out.println (str);
} catch (IOException e) {
E.printstacktrace ();
}

ReadLines (file file, String encoding)
Reads the contents of a file line from line to a List of Strings.
Returns a String collection
try {
list<string> strlist = Fileutils.readlines (File1, "UTF-8");
for (String s:strlist) {
System.out.println (s);
}
System.out.println (Strlist.size ());
} catch (IOException e) {
E.printstacktrace ();
}

Writestringtofile (file file, string data, string encoding)
Writes a String to a file creating the file if it is does not exist.
Writes string data to a file (only one line can be entered, and if there is further input, the previous content will be overwritten, the next method will be appended)
try {
Scanner in = new Scanner (system.in);
while (true) {
System.out.println ("Please enter the content (end Please enter break):");
String str = in.nextline ();
if (!str.equals ("break")) {
Fileutils.writestringtofile (File2, str, "UTF-8");
} else {
Break
}
}
} catch (IOException e) {
E.printstacktrace ();
}

Writestringtofile (file file, string data, String encoding, Boolean append)
Writes a String to a file creating the file if it is does not exist.
Writes string data to a file, appending multiple string data (remembering that there is no line break in a file)
try {
Scanner in = new Scanner (system.in);
while (true) {
System.out.println ("Please enter the content (end Please enter break):");
String str = in.nextline ();
if (!str.equals ("break")) {
Fileutils.writestringtofile (File2, str, "UTF-8", true);
} else {
Break
}
}
} catch (IOException e) {
E.printstacktrace ();
}

System.out.println ("============================================");
Network-related

Copyurltofile (URL source, File destination)
Copies bytes from the URL of source to a file destination.
Get the source code of a Web page, write local files, write pictures locally, etc.
Here are two examples, one gets the page, one gets the picture (if the file does not exist it will be created automatically)
try {
Fileutils.copyurltofile (The New URL ("78354929"),
New File ("h:\\javatest\\iotest\\test.html"));
System.out.println ("copy Complete");
} catch (IOException e) {
E.printstacktrace ();
}
Write a picture
try {
Fileutils.copyurltofile (The New URL ("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000& sec=1525082437668&di=18d455af6f3ffedc1f4d2e022cfe4af5&imgtype=0&src=http%3a%2f%2fpic.eastlady.cn% 2fuploads%2ftp%2f201705%2f9999%2f7e03b578dc.jpg "),
New File ("h:\\javatest\\iotest\\1.jpg"));
System.out.println ("copy Complete");
} catch (IOException e) {
E.printstacktrace ();
}

}

}

Common operations for Apache.commons.io.FileUtils

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.