01-需求
編寫一個程序,用於實現文件的備份,程序運行時的命令語法爲: java MyCopy <sourcefile> <destfile>
02-思路
1)獲取到兩個文件的路徑。
2)編寫拷貝方法。
3).將a文件從a路徑拷貝到b文件b路徑下面。
03-代碼
package com.eleven;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
/**
* 實現文件的備份 思路:獲取到兩個文件路徑。
*
* @author sywangu
*
*/
public class SevenDemo04 {
public static void main(String[] args) throws Exception {
File source = new File("d:\\aa\\aa.txt"); // 源目標路徑
File dest = new File("e:\\bb\\" + source.getName()); // 目標路徑
copyFile(source, dest);
}
/**
* 複製文件
*
* @param resource
* @param target
*/
public static void copyFile(File resource, File target) throws Exception {
// 輸入流 --> 從一個目標讀取數據
// 輸出流 --> 向一個目標寫入數據
long start = System.currentTimeMillis();
// 文件輸入流並進行緩衝
FileInputStream inputStream = new FileInputStream(resource);
BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
// 文件輸出流並進行緩衝
FileOutputStream outputStream = new FileOutputStream(target);
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(outputStream);
// 緩衝數組
// 大文件 可將 1024 * 2 改大一些,但是 並不是越大就越快
byte[] bytes = new byte[1024 * 2];
int len = 0;
while ((len = inputStream.read(bytes)) != -1) {
bufferedOutputStream.write(bytes, 0, len);
}
// 刷新輸出緩衝流
bufferedOutputStream.flush();
// 關閉流
bufferedInputStream.close();
bufferedOutputStream.close();
inputStream.close();
outputStream.close();
long end = System.currentTimeMillis();
System.out.println("耗時:" + (end - start) / 1000 + " s");
}
}
以下是將A文件裏面的內容複製到B文件中
方式一:普通方法(不推薦)
package com.eleven;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
/**
* 編寫一個程序,用於實現文件的備份,程序運行時的命令語法爲: java MyCopy <sourcefile> <destfile>
* 思路:
* 1.獲取到兩個文件的路徑。
* 2.將a文件從a路徑拷貝到b文件b路徑下面。
* @author sywangu
*
*/
public class SevenDemo {
public static void main(String[] args) throws IOException {
File source = new File("d:/aa/aa.txt");
File dest = new File("e:/bb/bb.txt");
CopyFile(source, dest);
}
// 將一個文件的內容複製到另一個文件中
private static void CopyFile(File source, File dest) throws IOException {
InputStream input = null;
OutputStream output = null;
try {
input = new FileInputStream(source); // 使用FileInputStream讀取文件的字節
output = new FileOutputStream(dest); // 使用FileOutputStream寫入到文件
byte[] buf = new byte[1024]; // 1M
int bytesRead;
while( (bytesRead = input.read(buf)) > 0 ) {
output.write(buf, 0 ,bytesRead);
}
}finally {
input.close();
output.close();
}
}
}
方式二:Java NIO中的transferFrom方法,根據文檔應該比文件流複製的速度更快
package com.eleven;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;
public class SevenDemo01 {
public static void main(String[] args) throws IOException {
File source = new File("d:/aa/aa.txt");
File dest = new File("e:/bb/bb.txt");
copyFile(source, dest);
}
private static void copyFile(File source, File dest) throws IOException {
FileChannel inputChannel = null;
FileChannel outputChannel = null;
try {
inputChannel = new FileInputStream(source).getChannel();
outputChannel = new FileOutputStream(dest).getChannel();
outputChannel.transferFrom(inputChannel, 0, inputChannel.size()); // Java NIO中的transferFrom方法,根據文檔應該比文件流複製的速度更快
} finally {
inputChannel.close();
outputChannel.close();
}
}
}
方式三:Apache Commons IO提供拷貝文件方法在其FileUtils類
package com.eleven;
import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
public class SevenDemo02 {
public static void main(String[] args) throws IOException {
File source = new File("d:/aa/aa.txt");
File dest = new File("e:/bb/bb.txt");
copyFile(source, dest);
}
// Apache Commons IO提供拷貝文件方法在其FileUtils類,可用於複製一個文件到另一個地方
private static void copyFile(File source, File dest) throws IOException {
FileUtils.copyFile(source, dest);
}
}
方式四:JDK7中的Files.copy方法
package com.eleven;
import java.io.File;
import java.io.IOException;
import java.nio.file.FileAlreadyExistsException;
import java.nio.file.Files;
public class SevenDemo03 {
public static void main(String[] args) throws IOException {
File source = new File("d:/aa/aa.txt");
File dest = new File("d:/aa/bb.txt");
copyFile(source, dest);
}
// JDK7中的Files.copy方法
private static void copyFile(File source, File dest) throws IOException {
try {
Files.copy(source.toPath(), dest.toPath()); // 將source裏面的內容拷貝到dest中
} catch (FileAlreadyExistsException e) {
System.out.println(dest + "目標文件已存在");
} catch (IOException e) {
e.printStackTrace(); // 打印其它異常
}
}
}