7

現在、ある場所から別の場所にフォルダーをコピーしています。正常に動作していますが、残りのすべてのファイルとフォルダーが存在する元のフォルダーをコピーしていません。これは私が使用しているコードです:

public static void copyFolder(File src, File dest) throws IOException {
  if (src.isDirectory()) {
    //if directory not exists, create it
    if (!dest.exists()) {
      dest.mkdir();
    }
    //list all the directory contents
    String files[] = src.list();
    for (String file : files) {
      //construct the src and dest file structure
      File srcFile = new File(src, file);
      File destFile = new File(dest+"\\"+src.getName(), file);
      //recursive copy
      copyFolder(srcFile,destFile);
    }
  } else {
    //if file, then copy it
    //Use bytes stream to support all file types
    InputStream in = new FileInputStream(src);
    OutputStream out = new FileOutputStream(dest); 
    byte[] buffer = new byte[1024];
    int length;
    //copy the file content in bytes 
    while ((length = in.read(buffer)) > 0){
      out.write(buffer, 0, length);
    }
    in.close();
    out.close();
    System.out.println("File copied from " + src + " to " + dest);
  }
}

だから私はフォルダーsrcを持っていますC:\test\mytest\..all folders..

にコピーしたいC:\test\myfiles

C:\test\myfiles\mytest\..all folders..しかし、私が得る代わりにC:\test\myfiles\..all folders..

4

9 に答える 9

11

代わりに、 Apache Commons IOライブラリのcopyDirectory(File srcDir, File destDir)メソッドを使用してみてください。

于 2012-07-25T14:25:48.180 に答える
1

Apache FileUtilsを試してディレクトリをコピーすることもできます

于 2012-07-25T14:26:17.667 に答える
1

apache commons FileUtilsを試す必要があります

于 2012-07-25T14:26:35.977 に答える
1

このソリューションは非常に単純ですが、コマンドがプレーン テキストでオペレーティング システムに与えられるため、プラットフォームに依存しません。cp(この例は Unix ベースのシェル用です。Windows の場合、コマンドの名前は少し異なりますcopy)。

String source = "/user/.../testDir";
String destination = "/Library/.../testDestination/testDir";
String command = "cp -r " + source + " " + destination;
Process p;
try {
    p = Runtime.getRuntime().exec(command);
    p.waitFor();
} catch (InterruptedException | IOException e) {
    // Error handling
}

オブジェクトを同じ名前のサブフォルダー内にコピーする場合は、それをコピー先パスに追加します。それ以外の場合は省略します。フォルダーの内容はコピー先パスに直接コピーされます。

編集:残念ながら、このソリューションはネットワークドライブでは機能しないことがわかりました。理由は私には不明です(ただし、何らかの理由で掘り下げていないことは認めます)

于 2020-06-16T14:25:47.207 に答える
0

主な問題は次のとおりです。

  dest.mkdir();

親ディレクトリではなく、1 つのディレクトリのみを作成します。最初の手順の後、2 つのディレクトリを作成する必要があるためmkdirmkdirs. その後、再帰 (C:\test\myfiles\mytest\dir1\dir1\subdir1\subdir1... のようなもの) のために子ディレクトリが重複していると思いますので、次の行も修正してみてください。

    File destFile = new File(dest, src.getName());
    /**/
    OutputStream out = new FileOutputStream(new File(dest, src.getName())); 
于 2012-07-25T14:41:45.907 に答える
-1

このコードは、フォルダーをソースから宛先にコピーします。

    public static void copyDirectory(String srcDir, String dstDir)
    {

        try {
            File src = new File(srcDir);
            String ds=new File(dstDir,src.getName()).toString();
            File dst = new File(ds);

            if (src.isDirectory()) {
                if (!dst.exists()) {
                    dst.mkdir();
                }

                String files[] = src.list();
                int filesLength = files.length;
                for (int i = 0; i < filesLength; i++) {
                    String src1 = (new File(src, files[i]).toString());
                    String dst1 = dst.toString();
                    copyDirectory(src1, dst1);

                }
            } else {
                fileWriter(src, dst);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
public static void fileWriter(File srcDir, File dstDir) throws IOException
{
        try {
            if (!srcDir.exists()) {
                System.out.println(srcDir + " doesnot exist");
                throw new IOException(srcDir + " doesnot exist");
            } else {
                InputStream in = new FileInputStream(srcDir);
                OutputStream out = new FileOutputStream(dstDir);
                // 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();

            }
        } catch (Exception e) {

        }
    }
于 2015-01-14T05:55:52.097 に答える