package com.qiang.IO;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class FileCopy {
public static void main(String[] args) {
copy("D:/Ideal wrok space/aa.txt","D:/Ideal wrok space/bb.txt");
}
/**
* 文件拷贝
* @param sourcePath 源文件的绝对为止
* @param targetPath 目标文件的绝对位置
*/
public static void copy(String sourcePath, String targetPath){
FileInputStream fileInputStream = null;
FileOutputStream fileOutputStream = null;
try {
fileInputStream = new FileInputStream(sourcePath);
fileOutputStream = new FileOutputStream(targetPath);
byte[] bytes = new byte[1024];
int len = 0;
while ((len= fileInputStream.read(bytes))>=0){
fileOutputStream.write(bytes, 0,len);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fileInputStream!= null){
try {
fileInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fileOutputStream != null){
try {
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
|