JAVA实现整个目录的COPY(10分)

  • 主题发起人 zhang12321
  • 开始时间
Z

zhang12321

Unregistered / Unconfirmed
GUEST, unregistred user!
JAVA实现整个目录的COPY?
谁能告诉我??
谢谢!!
我只有10分了!
不好意思!
 
谁能告诉我啊??
 
package test.usefileexample.copyfile;
import java.io.*;
/**
* Title: Description: Copyright: Copyright (c) 2002 Company:
*
*@author
*@created 2002年4月24日
*@version 1.0
*/
public class FileCopy {
/**
* 用于复制文件,使用BufferedReader从文件流中读入数据,并将其写入另一个文件
*/
public FileCopy() { }
public static void copy(String from_name,String to_name) throws Exception{
File from_file = new File(from_name);
File to_file = new File(to_name);
if(!from_file.exists()) abort("no such source file:" + from_name);
if(!from_file.isFile()) abort("can't copy directory" + from_name);//当输入的是目录时,抛出异常
if(!from_file.canRead()) abort("source file is unreadable" + from_name);
if(to_file.isDirectory()) to_file = new File(to_file,from_file.getName());//parent是文件的根目录,用于产生一个文件for example:
//File directory = new File("e:/swat");
File newFile = new File(directory,"swat.exe");
//如果目标存在,确定它是否可写
//如果目标不存在,确定目录是否存在,是否可写
//只要文件不能写,则确定抛出异常,并声明其错误
if(to_file.exists()){
if(!to_file.canWrite()) abort("destination file is unwriteable:" + to_file);
System.out.print("Overwrite existing file" + to_file.getName() + "[y/n]");//
//数据发送到桌面上,并保存起来
System.out.flush();
BufferedReader in = new BufferedReader(new java.io.InputStreamReader(System.in));
String response = in.readLine();
if(!response.equalsIgnoreCase("y")) abort("existing file was not overwritten");
}
else
{
String parent = to_file.getParent();
if(parent == null) //如果文件不存在并且目录也不存在时
parent = System.getProperty("user.dir");//使用当前目录
File dir = new File(parent);
//文件有可能仍不存在
if(!dir.exists()) abort("destination directorydo
esn't exist : " + parent);
if(!dir.canWrite()) abort("destination directory is unwritten!");
}
//至此开始正确的复制文件
java.io.FileInputStream from = null;
java.io.FileOutputStream to = null;
try{
from = new FileInputStream(from_file);//创建输入流
to = new FileOutputStream(to_file);//创建输出流
byte[] buffer = new byte[4096];
int byte_read;
while((byte_read = from.read(buffer))!=-1)
//使用FileOutputStream.write写入<4096个字符的数组返回读入的字符数组的个数
to.write(buffer,0,byte_read);
}finally{
if(from != null) try{ from.close();} catch(IOException e){
System.err.println(e);
}
if(to != null) try{to.close();}catch(IOException e1){
System.err.print(e1);
}
}
}
protected static void abort(String msg) throws IOException{
throw new IOException("FileCopy: " + msg);
}
/**
* The main program for the FileCopy class
* 每次读入10个文件,并写入另外的10个文件中
*
*@param args The command line arguments
*/
public static void main(String[] args) throws Exception{
FileCopy fileCopy = new FileCopy();
java.io.BufferedReader in = new java.io.BufferedReader(new java.io.InputStreamReader(System.in));//从键盘上面读入字符,并通过InputStreamReader读入数据
String copyFileFrom[] = new String[10];//从10个文件中读入数据,准备写入文件
String copyFileTo[] = new String[10];//写入10个文件中
String input = "";
boolean success = true;
for(int i=0;i<10;i++){
System.out.println("What filedo
you want copy from");
input = in.readLine();
if(input.trim().equalsIgnoreCase("quit")||input == null||input.trim().equals("")) break;//当回车或读入的文件为空,程序执行停止
copyFileFrom = input;
success = false;
System.out.println("What filedo
you want copy to");
input = in.readLine();
if(input.trim().equalsIgnoreCase("quit")||input == null||input.trim().equals("")) break;//当回车或读入的文件为空,程序执行停止
copyFileTo = input;
success = true;
}
if(!success||copyFileFrom.length < 1) throw new java.lang.IllegalArgumentException("输入的文件对有误,请更正,谢谢");
for(int j =0 ;j<copyFileFrom.length;j++){
if(copyFileFrom[j] != null)
fileCopy.copy(copyFileFrom[j],copyFileTo[j]);
}
}
}
以上是我写的一个例子。希望对您有所帮助![:)]
 
对不起,以上的代码是实现单个文件的拷贝。
如果实现目录的拷贝时:
1、目录 File directory = new File();
2、File[] fromFile = directory.listFiles();
3、fromFile.isDirectory()
goto 1
4、!file.isDirectory()
上面的代码
 
我研究一下,谢 谢你!!
 
顶部