1. 文件拷贝2. 文件夹拷贝
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;
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;
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);
}
}