Java开发网 Java开发网
注册 | 登录 | 帮助 | 搜索 | 排行榜 | 发帖统计  

您没有登录

» Java开发网 » 技术文章库  

按打印兼容模式打印这个话题 打印话题    把这个话题寄给朋友 寄给朋友    该主题的所有更新都将Email到你的邮箱 订阅主题
flat modethreaded modego to previous topicgo to next topicgo to back
作者 如何用java拷贝本地文件夹
wuchensir



发贴: 0
积分: 0
于 2003-10-27 16:15 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
声明:很多东西都是偶搜集整理实践过得,具体从那的来得也不清楚了,所以不著名出处

利用java本地拷贝文件及文件夹




如何实现文件夹对文件夹的拷贝呢?如果文件夹里还有文件夹怎么办呢?

接下来我就将我的方法演示给一些初学者,希望能够帮助你们!

/*
* CopyRight by http://www.designac.org/
*/
package copyfile;
import java.io.*;
/**
* @author Administrator
*
* Class Function:
*/
public class copyDirectory {
public static void main(String args[]) throws IOException {
String url1="C:/Downloads/kele";
String url2="d:/java/copyfile/copy";
(new File(url2)).mkdirs();
File[] file=(new File(url1)).listFiles();
for(int i=0;i<file.length;i++){
if(file[i].isFile()){
FileInputStream input=new FileInputStream(file[i]);
FileOutputStream output=new FileOutputStream(url2+"/"+file[i].getName());
byte[] b=new byte[1024*5];
int len;
while((len=input.read(b))!=-1){
output.write(b,0,len);
}
output.flush();
output.close();
input.close();
}
if(file[i].isDirectory()){
copyDirectiory(url2+"/"+file[i].getName(),url1+"/"+file[i].getName());
}
}
}
public static void copyDirectiory(String file1,String file2) throws IOException{
(new File(file1)).mkdirs();
File[] file=(new File(file2)).listFiles();
for(int i=0;i<file.length;i++){
if(file[i].isFile()){
FileInputStream input=new FileInputStream(file[i]);
FileOutputStream output=new FileOutputStream(file1+"/"+file[i].getName());
byte[] b=new byte[1024*5];
int len;
while((len=input.read(b))!=-1){
output.write(b,0,len);
}
output.flush();
output.close();
input.close();
}
if(file[i].isDirectory()){
copyDirectiory(file1+"/"+file[i].getName(),file2+"/"+file[i].getName());
}

}

}
}


floater edited on 2003-10-27 23:05

作者 Re:如何用java拷贝本地文件夹 [Re:wuchensir]
reddream



发贴: 0
积分: 0
于 2003-11-27 14:33 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
只有这种方法吗?
这样好象效率不好啊。



作者 Re:如何用java拷贝本地文件夹 [Re:wuchensir]
javaD



发贴: 0
积分: 0
于 2003-12-02 17:21 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
不管,收藏先



作者 Re:如何用java拷贝本地文件夹 [Re:wuchensir]
SQL20000





发贴: 66
积分: 0
于 2003-12-06 09:03 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
不错呀只是里面有些地方能否给解释一下看不太明白能不能给大概的解说一下比如::::

(new File(url2)).mkdirs();
File[] file=(new File(url1)).listFiles();
for(int i=0;i<file.length;i++){
if(file[i].isFile()){
FileInputStream input=new FileInputStream(file[i]);
FileOutputStream output=new FileOutputStream(url2+"/"+file[i].getName());
byte[] b=new byte[1024*5];
int len;
while((len=input.readBeer)!=-1){
output.write(b,0,len);
}
output.flush();
output.close();
input.close();
}
if(file[i].isDirectory()){
copyDirectiory(url2+"/"+file[i].getName(),url1+"/"+file[i].getName());
}
}
}
public static void copyDirectiory(String file1,String file2) throws IOException{
(new File(file1)).mkdirs();
File[] file=(new File(file2)).listFiles();
for(int i=0;i<file.length;i++){
if(file[i].isFile()){
FileInputStream input=new FileInputStream(file[i]);
FileOutputStream output=new FileOutputStream(file1+"/"+file[i].getName());
byte[] b=new byte[1024*5];
int len;
while((len=input.readBeer)!=-1){
output.write(b,0,len);
}
output.flush();
output.close();
input.close();
}
if(file[i].isDirectory()){
copyDirectiory(file1+"/"+file[i].getName(),file2+"/"+file[i].getName());
}



作者 Re:如何用java拷贝本地文件夹3 [Re:wuchensir]
SQL20000





发贴: 66
积分: 0
于 2003-12-06 09:05 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
还有编译完之后怎么运行...
是否编译完就copy完了呢??
是不是要在本地先建两个文件夹呢/



作者 如何应用呀.. [Re:wuchensir]
SQL20000





发贴: 66
积分: 0
于 2003-12-06 09:09 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
我编译这个类了说编译成功.
可是我的文件夹里的东西还是在原来的地方呀没有copy过去呀

怎么应用这个例子呀///



作者 Re:如何用java拷贝本地文件夹 [Re:wuchensir]
hotyaya





发贴: 101
积分: 0
于 2004-02-15 07:58 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
好东西,先收好了呀,嘻嘻


作者 Re:如何用java拷贝本地文件夹 [Re:wuchensir]
Lomone



发贴: 0
积分: 0
于 2004-03-09 21:27 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
good
thank you for you article



作者 Re:如何用java拷贝本地文件夹 [Re:wuchensir]
PrimeJava

一切皆有可能



发贴: 142
积分: 15
于 2004-03-16 17:05 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
提个小小的建议:代码加些注释可能会好些!


I Believe I can fly!

flat modethreaded modego to previous topicgo to next topicgo to back
  已读帖子
  新的帖子
  被删除的帖子
Jump to the top of page

   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923