import java.io.*;
public class CopyDir {
public static void main(String[] argv) throws Exception {
process("c:\\test", "d:\\test");
}
private static void process(String d1, String d2) throws IOException {
File f = new File(d1);
if (f.isDirectory()) {
(new File(d2)).mkdir();
String list[] = f.list();
for (int i = 0; i < list.length; i++) {
process( d1 + File.separator + list[i],
d2 + File.separator + list[i]);
}
} else if (f.isFile()) {
copy(d1, d2);
}
}
public static void copy(String fromname, String toname) throws IOException {
InputStream is = new FileInputStream(fromname);
OutputStream os = new FileOutputStream(toname);
byte[] b = new byte[100000];
int n;
while ((n = is.read(b)) >= 0) {
os.write(b, 0, n);
}
is.close();
os.close();
}
}
Fundet lige her på eksperten:
http://www.eksperten.dk/spm/406633