至於相關jar包可以到官網獲取 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;
/**
* 測試io工具包
* 本人英語水平不過關,簡單理解下
* (注釋的第一行是測試的方法,第二行是官方文檔的解釋,第三行是我測試做的結論)
* @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();
// 復制操作
// cleanDirectory(File directory)
// Cleans a directory without deleting it.
// 刪除該文件夾下的所有東西(如果該路徑是文件或者路徑不存在是會報java.lang.IllegalArgumentException異常)
try {
FileUtils.cleanDirectory(file);
System.out.println("刪除成功");
} catch (IOException e) {
e.printStackTrace();
}
// contentEquals(File file1, File file2)
// Compares the contents of two files to determine if they are equal or not.
// 測試兩個文件的內容相不相同
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.
// 復制第一個參數的文件夾下的所有內容復制到第二個參數的文件夾下
try {
FileUtils.copyDirectory(file,file3);
System.out.println("復制成功");
} catch (IOException e) {
e.printStackTrace();
}
// copyDirectoryToDirectory(File srcDir, File destDir)
// Copies a directory to within another directory preserving the file dates.
// 將第一個參數的整個文件夾(即iotest文件夾)復制到第二個路徑下
try {
FileUtils.copyDirectoryToDirectory(file, file3);
System.out.println("復制成功");
} catch (IOException e) {
e.printStackTrace();
}
// copyFile(File srcFile, File destFile)
// Copies a file to a new location preserving the file date.
// 將一個文件的內容復制到另一個文件中
try {
FileUtils.copyFile(file1, file2);
System.out.println("復制成功");
} catch (IOException e) {
e.printStackTrace();
}
// copyFileToDirectory(File srcFile, File destDir)
// Copies a file to a directory preserving the file date.
// 將一個文件復制到另一個文件夾下(不做測試了)
System.out.println("==================================================");
// 讀寫操作
// readFileToString(File file, String encoding)
// Reads the contents of a file into a String.
// 讀取文件內容返回String,原樣輸出
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 by line to a List of Strings.
// 返回String集合
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 does not exist.
// 將String數據寫入文件(只能輸入一行,如果后續還有輸入,將覆蓋前面的內容,下一個方法將是追加)
try {
Scanner in = new Scanner(System.in);
while (true){
System.out.println("請輸入內容(結束請輸入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 does not exist.
// 將String數據寫入文件,多個string數據就追加(記住一點文件里不會換行)
try {
Scanner in = new Scanner(System.in);
while (true){
System.out.println("請輸入內容(結束請輸入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("============================================");
// 網絡相關
// copyURLToFile(URL source, File destination)
// Copies bytes from the URL source to a file destination.
// 獲取一個網頁的源代碼,寫入本地文件,還可以向本地寫圖片的等等
// 下面是兩個例子,一個獲取頁面,一個獲取圖片(如果文件不存在會自動創建)
try {
FileUtils.copyURLToFile(new URL("https://blog.csdn.net/lixin2302007/article/details/78354929"),
new File("H:\\javaTest\\iotest\\test.html"));
System.out.println("復制完畢");
} catch (IOException e) {
e.printStackTrace();
}
// 寫圖片
try {
FileUtils.copyURLToFile(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("復制完畢");
} catch (IOException e) {
e.printStackTrace();
}
}
}