Topic: 一道不难的java题(拷贝一个文件夹及其子文件夹)

  Print this page

1.一道不难的java题(拷贝一个文件夹及其子文件夹) Copy to clipboard
Posted by: zerog
Posted on: 2006-08-15 16:11

“编写程序,使其能够拷贝一个文件夹及其子文件夹”
请问高手们,这个正确的编写步骤是怎样的?

2.Re:一道不难的java题 [Re: zerog] Copy to clipboard
Posted by: zcjl
Posted on: 2006-08-15 16:35

1.判断URL是否文件夹,是则跳到步骤3
2.构造目标文件的URL,并通过IO进行文件复制
3.遍历文件夹下的所有文件(夹),以每个文件(夹)的URL作为条件进入到步骤1(递归)

3.Re:一道不难的java题(拷贝一个文件夹及其子文件夹) [Re: zerog] Copy to clipboard
Posted by: zerog
Posted on: 2006-08-15 16:47

我是个初学者,您说的很难理解,能否把代码写下来呢!?

4.Re:一道不难的java题(拷贝一个文件夹及其子文件夹) [Re: zerog] Copy to clipboard
Posted by: why
Posted on: 2006-08-15 16:49

http://javaalmanac.com/egs/java.io/CopyDir.html?l=new

5.Re:一道不难的java题(拷贝一个文件夹及其子文件夹) [Re: zerog] Copy to clipboard
Posted by: zerog
Posted on: 2006-08-15 19:01

还是不太明白怎么把大侠说的三步编程代码,我看了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);
  }

6.Re:一道不难的java题(拷贝一个文件夹及其子文件夹) [Re: zerog] Copy to clipboard
Posted by: why
Posted on: 2006-08-16 03:50

閣下寫過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

7.Re:一道不难的java题(拷贝一个文件夹及其子文件夹) [Re: why] Copy to clipboard
Posted by: zcjl
Posted on: 2006-08-16 09:21

真是佩服WHY的耐心和细致,不知道楼主对此还有什么问题吗?


   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