1. 文件复制 完成一个程序,这个程序可以将指定文件夹中的内容复制到另一个文件夹中去。 设计要求: 1) 实

设计要求:
1) 实现文件夹复制;
2) 程序中需要考虑输入输出异常处理;
3) 用户可以指定不同的参数:用-ext指定要复制的文件的扩展名,可以同时指定多个扩展名;用-empty指定不复制空目录;用-zip表示进行压缩;还可以增加其它选项
4) 在dos控制台窗口环境下,通过命令行参数运行程序,
本人急,希望朋友们多多帮助!我给高分.对了用java实现!别的不行!!!!

第1个回答  2011-12-12
package com.qw.copy;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipOutputStream;

public class JCopy {
private static List<String> exts;
private static boolean empty;
private static boolean zip;
public static void main(String[] args) {
if(args.length < 2){
System.out.println("usage : JCopy -from -to [-ext ***] [-empty] [-zip]");
return;
}
String from = args[0];
String to = args[1];
for (int i = 0; i < args.length; i++){
if(args[i].equals("-ext")){
//指定文件扩展名,可以指定多个,以逗号分开
exts = new ArrayList<String>();
Collections.addAll(exts, args[i+1].split(","));
}
if(args[i].equals("-empty")){
empty = false;
}
if(args[i].equals("-zip")){
zip = true;
}
}

File fFile = new File(from).getAbsoluteFile();
File tFile = new File(to).getAbsoluteFile();
if(!fFile.exists()){
System.out.println("from file not found");
System.out.println("usage : JCopy from to");
return;
}
//如果指定文件扩展名,则待拷贝的必须是文件而不能是目录
if(exts != null && exts.size() != 0){
if(fFile.isDirectory()){
System.out.println("指定了文件扩展名,则待拷贝的必须是文件");
return;
}
}
if(tFile.toString().indexOf(".") == -1){
//目标文件参数是目录,则建立对应目录,没有文件扩展名,则看成是目录
tFile.mkdirs();
}else{
//如果目标文件是文件,则生成文件所需的目录
File dir = new File(tFile.toString().substring(0,tFile.toString().lastIndexOf("\\")));
System.out.println("make dirs: "+dir.toString());
dir.mkdirs();
if(exts != null && exts.size() != 0){
exts.add(tFile.toString().substring(tFile.toString().indexOf(".")+1));
}
}
if(exts != null && !exts.isEmpty()){
for (String ext : exts) {
copy(fFile,tFile,ext);
}
}else{
copy(fFile,tFile,null);
}

}

public static void copy(File fFile, File tFile,String ext){
if(fFile.isFile()){
if(tFile.isDirectory()){
if(ext != null && !ext.isEmpty()){
tFile = new File(tFile.toString() + File.separator + fFile.getName().replaceAll("\\.\\w+", "."+ext));
}else{
tFile = new File(tFile.toString()+File.separator+fFile.getName());
}
if(zip){
copyZipFile(fFile, new File(tFile.toString().replaceAll("\\.\\w+", ".zip")));
}else{
copyFile(fFile,tFile);
}

}else{
if(ext != null && !ext.isEmpty()){
tFile = new File(tFile.toString().replaceAll("\\.\\w+", "."+ext));
}
if(zip){
copyZipFile(fFile,new File(tFile.toString().replaceAll("\\.\\w+", ".zip")));
}else{
copyFile(fFile, tFile);
}

}

}
//如果是目录,则逐层建立目录
if(fFile.isDirectory()){
/*//创建目录
tFile.mkdir();*/
File[] files = fFile.listFiles();
System.out.println(Arrays.toString(files));
if(!empty){
if(Arrays.asList(files).isEmpty()){
return;
}
}
tFile.mkdirs();
for (File file : files) {
copy(new File(fFile.toString()+File.separator+file.getName()),new File(tFile.toString()+File.separator+file.getName()),null);
}

}
}

//生成压缩文件
private static void copyZipFile(File fFile, File tFile) {
BufferedInputStream in = null;
BufferedOutputStream out = null;
ZipOutputStream zipout = null;
try{
in = new BufferedInputStream(new FileInputStream(fFile));
out = new BufferedOutputStream(new FileOutputStream(tFile));
zipout = new ZipOutputStream(out);
try{
zipout.putNextEntry(new ZipEntry(fFile.getName()));
byte[] b = new byte[4096];
int n;
while((n = in.read(b)) != -1){
zipout.write(b,0,n);
}
zipout.closeEntry();
}catch(Exception e){
e.printStackTrace();
}finally{
try {
in.close();
out.close();
zipout.close();
} catch (IOException e) {
e.printStackTrace();
}
}
} catch(FileNotFoundException e){
System.err.println("文件未找到");
e.printStackTrace();
}
}

//拷贝文件
private static void copyFile(File fFile, File tFile) {
BufferedInputStream in = null;
BufferedOutputStream out = null;
//如果是文件则直接复制
try {
in = new BufferedInputStream(new FileInputStream(fFile));
out = new BufferedOutputStream(new FileOutputStream(tFile));
try{
byte[] b = new byte[4096];
int i = 0;
while((i = in.read(b)) != -1){
out.write(b, 0, i);
}
out.flush();
}catch (Exception e) {
} finally{
try {
in.close();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
} catch (FileNotFoundException e) {
System.out.println("文件未找到..."+e.getMessage());
e.printStackTrace();
}
}

}

//刚回答完一个同样的问题,估计是你同学提的
//依赖ant.jar包,这个包属于apache ant项目,楼主可以自己去下。
用法:java JCopy e:\aa\abc.txt e:\mm -empty -ext txt,html本回答被提问者采纳
相似回答