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

您没有登录

» Java开发网 » Java SE 综合讨论区  

按打印兼容模式打印这个话题 打印话题    把这个话题寄给朋友 寄给朋友    该主题的所有更新都将Email到你的邮箱 订阅主题
flat modethreaded modego to previous topicgo to next topicgo to back
作者 一道不难的java题(拷贝一个文件夹及其子文件夹)
zerog





发贴: 15
积分: 0
于 2006-08-15 16:11 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
“编写程序,使其能够拷贝一个文件夹及其子文件夹”
请问高手们,这个正确的编写步骤是怎样的?


why edited on 2006-08-15 16:26

作者 Re:一道不难的java题 [Re:zerog]
zcjl

涅槃



发贴: 537
积分: 65
于 2006-08-15 16:35 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
1.判断URL是否文件夹,是则跳到步骤3
2.构造目标文件的URL,并通过IO进行文件复制
3.遍历文件夹下的所有文件(夹),以每个文件(夹)的URL作为条件进入到步骤1(递归)



作者 Re:一道不难的java题(拷贝一个文件夹及其子文件夹) [Re:zerog]
zerog





发贴: 15
积分: 0
于 2006-08-15 16:47 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:zerog]
why

問題兒童

总版主


发贴: 4629
积分: 388
于 2006-08-15 16:49 user profilesend a private message to usersend email to whysearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
http://javaalmanac.com/egs/java.io/CopyDir.html?l=new


作者 Re:一道不难的java题(拷贝一个文件夹及其子文件夹) [Re:zerog]
zerog





发贴: 15
积分: 0
于 2006-08-15 19:01 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
还是不太明白怎么把大侠说的三步编程代码,我看了why给我的例子,但是只是其中一部分,我又把几步写在一起,但是不知道具体怎么写,下面是我自己写的,很乱,不知道怎么写?希望哪位大侠给纠正下!
public class test {
  public static void show(){
    File file = new File("f:/test");
    File dir = new File("g:/TEST");
      if(file.isDirectory())
      {
        if(!dir.isDirectory())
        {
          String[] str = dir.list();
     for (int i=0; i<str.length; i++) {
       show(new File(dir, str[i]));
     }
          dir.mkdir();
        }
        String[] str = file.list();
        for(int i=0; i<str.length; i++){
          show(new File(file, str[i]),new File(dir,str[i]));
          
        }
      }else
        copyFile (file,dir);
  }



作者 Re:一道不难的java题(拷贝一个文件夹及其子文件夹) [Re:zerog]
why

問題兒童

总版主


发贴: 4629
积分: 388
于 2006-08-16 03:50 user profilesend a private message to usersend email to whysearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
閣下寫過HelloWorld.java嗎?

不明白閣下為何要把copyDirectory改亂了


import java.io.*;

public class CopyFolder {

public static void main(String[] args) {
File srcDir = new File("F:/test");
File dstDir = new File("G:/test");
try {
copyDirectory(srcDir, dstDir);
} catch (IOException ioe) {
ioe.printStackTrace();
}
}

// Copies all files under srcDir to dstDir.
// If dstDir does not exist, it will be created.
static void copyDirectory(File srcDir, File dstDir) throws IOException {
if (srcDir.isDirectory()) { // 1.判断File是否文件夹,是则跳到步骤3
if (!dstDir.exists()) {
dstDir.mkdir();
}

// 3.遍历文件夹下的所有文件(夹),以每个文件(夹)的File作为条件进入到步骤1(递归)
String[] children = srcDir.list();
for (int i=0; i<children.length; i++) {
copyDirectory(new File(srcDir, children[i]),
new File(dstDir, children[i]));
}
} else {
// ~2.[构造目标文件的URL,并] 通过IO进行文件复制
// This method is implemented in e1071 Copying a File
copyFile(srcDir, dstDir);
}
}

// Copies src file to dst file.
// If the dst file does not exist, it is created
static void copyFile(File src, File dst) throws IOException {
InputStream in = new FileInputStream(src);
OutputStream out = new FileOutputStream(dst);

// Transfer bytes from in to out
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
in.close();
out.close();
}
}


or


import java.io.*;

public class CopyFolder {

public static void main(String[] args) {
CopyFolder cf = new CopyFolder();

File srcDir = new File("C:/temp/1");
File dstDir = new File("C:/temp/2");
try {
cf.copyDirectory(srcDir, dstDir);
} catch (IOException ioe) {
ioe.printStackTrace();
}
}

// Copies all files under srcDir to dstDir.
// If dstDir does not exist, it will be created.
void copyDirectory(File srcDir, File dstDir) throws IOException {
...
}

// Copies src file to dst file.
// If the dst file does not exist, it is created
void copyFile(File src, File dst) throws IOException {
...
}
}


or use org.apache.commons.io.FileUtils
http://jakarta.apache.org/commons/io/api-release/org/apache/commons/io/FileUtils.html


why edited on 2006-08-16 04:26

作者 Re:一道不难的java题(拷贝一个文件夹及其子文件夹) [Re:why]
zcjl

涅槃



发贴: 537
积分: 65
于 2006-08-16 09: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
真是佩服WHY的耐心和细致,不知道楼主对此还有什么问题吗?



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