Java 高階程式設計-檔案拷貝
阿新 • • 發佈:2018-12-16
1. 檔案拷貝
package IOKnowledge;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
/**
* 檔案拷貝處理
* @author sunlh
*
*/
public class Test {
public static void main(String[] args) throws Exception {
if(args.length != 2) {// 程式執行出錯
System.out.println("命令執行錯誤,執行結構:java Test 拷貝原始檔路徑 拷貝目標檔案");
System.exit(1);
}
long start = System.currentTimeMillis();
FileUtil fu = new FileUtil(args[0], args[1 ]);
System.out.println(fu.copy()?"檔案拷貝成功":"檔案拷貝失敗");
long end = System.currentTimeMillis();
System.out.println("拷貝完成的時間:" + (end - start));
}
}
/**
* 檔案操作工具類
* @author sunlh
*
*/
class FileUtil{
private File srcFile;// 原始檔路徑
private File desFile;// 目標檔案路徑
public FileUtil (String src, String des) {
this(new File(src), new File(des));
}
public FileUtil(File srcFile, File desFile) {
this.srcFile = srcFile;
this.desFile = desFile;
}
public boolean copy() throws Exception {// 檔案拷貝處理
if (!this.srcFile.exists()) {// 原始檔必須存在
System.out.println("拷貝的原始檔不存在");
return false;// 拷貝失敗
}
if (!this.desFile.getParentFile().exists()) {// 建立父目錄
this.desFile.getParentFile().mkdirs();
}
byte [] data = new byte[1024];// 開闢一個拷貝的緩衝區
InputStream input = null;
OutputStream output = null;
try {
input = new FileInputStream(this.srcFile);
output = new FileOutputStream(this.desFile);
int len = 0;
// 1.讀取資料到陣列之中,隨後返回讀取個數
// 2.判斷個數是否是-1,如果不是則進行寫入
while((len = input.read(data)) != -1) {
output.write(data, 0, len);
}
return true;
} catch (Exception e) {
throw e;
} finally {
if (input != null) {
input.close();
}
if (output != null) {
output.close();
}
}
}
}
2. 資料夾拷貝
package IOKnowledge;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
/**
* 檔案拷貝處理
* @author sunlh
*
*/
public class Test {
public static void main(String[] args) throws Exception {
if(args.length != 2) {// 程式執行出錯
System.out.println("命令執行錯誤,執行結構:java Test 拷貝原始檔路徑 拷貝目標檔案");
System.exit(1);
}
long start = System.currentTimeMillis();
FileUtil fu = new FileUtil(args[0], args[1]);
if(new File(args[0]).isFile()) {// 檔案拷貝
System.out.println(fu.copy()?"檔案拷貝成功":"檔案拷貝失敗");
} else {// 目錄拷貝
System.out.println(fu.copyDir()?"檔案拷貝成功":"檔案拷貝失敗");
}
long end = System.currentTimeMillis();
System.out.println("拷貝完成的時間:" + (end - start));
}
}
/**
* 檔案操作工具類
* @author sunlh
*
*/
class FileUtil{
private File srcFile;// 原始檔路徑
private File desFile;// 目標檔案路徑
public FileUtil(String src, String des) {
this(new File(src), new File(des));
}
public FileUtil(File srcFile, File desFile) {
this.srcFile = srcFile;
this.desFile = desFile;
}
// 目錄拷貝
public boolean copyDir() throws Exception {
try {
this.copyImpl(this.srcFile);
return true;
} catch (Exception e) {
return false;
}
}
private void copyImpl(File file) throws Exception {
if (file.isDirectory()) {// 是目錄
File [] results = file.listFiles();
if (results != null) {
for(int i = 0; i < results.length; i++) {
copyImpl(results[i]);
}
}
} else {// 是檔案
String newFilePath = file.getPath().replace(this.srcFile.getPath() + File.separator, "");
File newFile = new File(this.desFile, newFilePath); //拷貝的目標路徑
copyFileImpl(file, newFile);
}
}
private boolean copyFileImpl(File srcFile, File desFile) throws Exception{
if (!desFile.getParentFile().exists()) {// 建立父目錄
desFile.getParentFile().mkdirs();
}
byte [] data = new byte[1024];// 開闢一個拷貝的緩衝區
InputStream input = null;
OutputStream output = null;
try {
input = new FileInputStream(srcFile);
output = new FileOutputStream(desFile);
int len = 0;
// 1.讀取資料到陣列之中,隨後返回讀取個數
// 2.判斷個數是否是-1,如果不是則進行寫入
while((len = input.read(data)) != -1) {
output.write(data, 0, len);
}
return true;
} catch (Exception e) {
throw e;
} finally {
if (input != null) {
input.close();
}
if (output != null) {
output.close();
}
}
}
public boolean copy() throws Exception {// 檔案拷貝處理
if (!this.srcFile.exists()) {// 原始檔必須存在
System.out.println("拷貝的原始檔不存在");
return false;// 拷貝失敗
}
return this.copyFileImpl(this.srcFile, this.desFile);
}
}