至於相關jar包能夠到官網獲取 http://commons.apache.org/downloads/index.htmlpackage 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(); } }}