使用apache commons的FileUtils复制文件,,package cn.o
分享于 点击 16990 次 点评:218
使用apache commons的FileUtils复制文件,,package cn.o
package cn.outofmemory.snippets.core;import java.io.File;import java.io.IOException;import org.apache.commons.io.FileUtils;public class FileCopy { public static void main(String[] args) { // We take a reference to original .txt File file1 = new File("test.txt"); // We take a reference to the copy .txt File file2 = new File("test(copy).txt"); try { // We copy the file with copyFile method FileUtils.copyFile(file1, file2); // We compare the files to test the result String content1 = FileUtils.readFileToString(file1); String content2 = FileUtils.readFileToString(file2); System.out.println("Content of file 1: " + content1); System.out.println("Content of file 2: " + content2); } catch (IOException e) { e.printStackTrace(); } }}
用户点评