콘텐츠/파일을 복사하지 않고 디렉토리 구조를 복사하려고합니다. 폴더 구조 만 복사하고 싶습니다. 샘플 프로그램을 작성했지만 컨텐츠/파일도 복사 중입니다.실제로 콘텐츠를 다른 디렉토리에 복사하지 않고 디렉토리 구조를 복사하는 방법
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.");
}
}
나는이 시점에 충격을 받았다. 감사합니다.
는 모든 파일을 복사 작은 부분 것을보기? 그래, 그냥 ... 그걸 없애 버려? – Corbin
파일을 쓰고 dstPath.createNewFile()로 바꾸는 코드 부분을 삭제하지 않는 이유는 무엇입니까? 빈 파일들로 끝날 것입니다. 빈 파일은 원하는 파일 인 것 같습니다. – Renato
작은 프로그램을 작성했다고 말하면 나는 당신을 믿지 않는다. 그렇지 않으면 파일을 복사하는 코드를 작성했음을 알았을 것입니다. – Sorin