2012-03-27 3 views
0

콘텐츠/파일을 복사하지 않고 디렉토리 구조를 복사하려고합니다. 폴더 구조 만 복사하고 싶습니다. 샘플 프로그램을 작성했지만 컨텐츠/파일도 복사 중입니다.실제로 콘텐츠를 다른 디렉토리에 복사하지 않고 디렉토리 구조를 복사하는 방법

import java.io.*; 
    import java.nio.channels.*; 

    @SuppressWarnings("unused") 
    public class CopyDirectory{ 
    public static void main(String[] args) throws IOException{ 
     CopyDirectory cd = new CopyDirectory(); 
     BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); 
     String source = "C:\\abcd\\Documents\\1"; 
     File src = new File(source);   
     String destination = "C:\\abcd\\Documents\\2"; 
     File dst = new File(destination); 
     cd.copyDirectory(src, dst); 
     } 

    public void copyDirectory(File srcPath, File dstPath) throws IOException{ 
     if (srcPath.isDirectory()) 
     { 
      if (!dstPath.exists()) 
      { 
       dstPath.mkdir(); 
      } 

      String files[] = srcPath.list(); 
      for(int i = 0; i < files.length; i++) 
      { 
       System.out.println("\n"+files[i]); 
       copyDirectory(new File(srcPath, files[i]), new File(dstPath, files[i])); 
      } 
     } 

     System.out.println("Directory copied."); 
    } 
    } 

나는이 시점에 충격을 받았다. 감사합니다.

+3

는 모든 파일을 복사 작은 부분 것을보기? 그래, 그냥 ... 그걸 없애 버려? – Corbin

+1

파일을 쓰고 dstPath.createNewFile()로 바꾸는 코드 부분을 삭제하지 않는 이유는 무엇입니까? 빈 파일들로 끝날 것입니다. 빈 파일은 원하는 파일 인 것 같습니다. – Renato

+6

작은 프로그램을 작성했다고 말하면 나는 당신을 믿지 않는다. 그렇지 않으면 파일을 복사하는 코드를 작성했음을 알았을 것입니다. – Sorin

답변

1

이것은 나를 위해 일한 :

import java.io.File; 

public class StartCloneFolderOnly { 

    /** 
    * @param args 
    */ 
    public static void main(String[] args) {   
     cloneFolder("C:/source", 
       "C:/target");  
    } 

    public static void cloneFolder(String source, String target) { 
     File targetFile = new File(target); 
     if (!targetFile.exists()) { 
      targetFile.mkdir(); 
     } 
     for (File f : new File(source).listFiles()) { 
     if (f.isDirectory()) { 
       String append = "/" + f.getName(); 
       System.out.println("Creating '" + target + append + "': " 
         + new File(target + append).mkdir()); 
       cloneFolder(source + append, target + append); 
      } 
     } 
    } 
} 
0

그래서 내가 옳다면 폴더를 복사하기 만하면됩니다.

1) 3A.) 인스턴스화는 ArrayList를 3B로 상위 디렉토리에있는 파일을 나열 할 때마다 하위 디렉토리 및 파일 2) 장소 복사 디렉토리 . 새 서브 폴더를 인스턴스화하여 arrayList에 나열합니다. 3c) 인스턴스화하여 각 하위 폴더의 모든 파일을 자신의 배열 목록에 나열합니다. 4.) for-loop를 사용하여 새 디렉토리 및 하위 폴더의 모든 파일을 삭제하십시오.

이렇게하면 모든 파일이 제거 된 새 디렉토리의 복사본이 있어야합니다.

0
import java.io.*; 
    import java.nio.channels.*; 

    @SuppressWarnings("unused") 

    public class CopyDirectory{ 
    public static void main(String[] args) throws IOException{ 
    CopyDirectory cd = new CopyDirectory(); 
    BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); 
    String source = "C:\\abcd\\Documents\\1"; 
    File src = new File(source);   
    String destination = "C:\\abcd\\Documents\\2"; 
    File dst = new File(destination); 
    cd.copyDirectory(src, dst); 
    } 

public void copyDirectory(File srcPath, File dstPath) throws IOException{ 
    if (srcPath.isDirectory()) 
    { 
     if (!dstPath.exists()) 
     { 
      dstPath.mkdir(); 
     } 

     String files[] = srcPath.list(); 

     for(int i = 0; i < files.length; i++) 
     { 
      System.out.println("\n"+files[i]); 
      copyDirectory(new File(srcPath, files[i]), new File(dstPath, files[i])); 
     } 
    } 

    System.out.println("Directory copied."); 
} 

}

관련 문제