특별히 어려운 부분이 없으니 예제만 참고하세요~
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class CopyDir {
public static void copyDirectory(File sourcelocation , File targetdirectory)
throws IOException {
//디렉토리인 경우
if (sourcelocation.isDirectory()) {
//복사될 Directory가 없으면 만듭니다.
if (!targetdirectory.exists()) {
targetdirectory.mkdir();
}
String[] children = sourcelocation.list();
for (int i=0; i<children.length; i++) {
copyDirectory(new File(sourcelocation, children[i]),
new File(targetdirectory, children[i]));
}
} else {
//파일인 경우
InputStream in = new FileInputStream(sourcelocation);
OutputStream out = new FileOutputStream(targetdirectory);
// Copy the bits from instream to outstream
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
in.close();
out.close();
}
}
public static void main(String[] args) throws IOException {
//c:\LOG의 내용을 c:\Temp에 복사 합니다.
File source = new File("c:\\LOG");
File target = new File("c:\\Temp");
copyDirectory(source , target);
}
}
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class CopyDir {
public static void copyDirectory(File sourcelocation , File targetdirectory)
throws IOException {
//디렉토리인 경우
if (sourcelocation.isDirectory()) {
//복사될 Directory가 없으면 만듭니다.
if (!targetdirectory.exists()) {
targetdirectory.mkdir();
}
String[] children = sourcelocation.list();
for (int i=0; i<children.length; i++) {
copyDirectory(new File(sourcelocation, children[i]),
new File(targetdirectory, children[i]));
}
} else {
//파일인 경우
InputStream in = new FileInputStream(sourcelocation);
OutputStream out = new FileOutputStream(targetdirectory);
// Copy the bits from instream to outstream
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
in.close();
out.close();
}
}
public static void main(String[] args) throws IOException {
//c:\LOG의 내용을 c:\Temp에 복사 합니다.
File source = new File("c:\\LOG");
File target = new File("c:\\Temp");
copyDirectory(source , target);
}
}
'Programming > JAVA' 카테고리의 다른 글
JDK1.4와 1.5의 다른점(Collection Data 다루기) (0) | 2008.04.28 |
---|---|
디렉토리 하위 탐색 하기 (0) | 2008.04.28 |
문자변수 비교 방법(Null Point Exception 예방하기) (0) | 2008.04.28 |
JAR 파일로 프로그램 실행 (0) | 2008.04.28 |
윤년 구하는 메소드 (0) | 2008.04.28 |