Package base;
Import Java.io.FileInputStream;
Import Java.io.FileOutputStream;
Import java.io.IOException;
Import Java.io.File;
Import Java.io.InputStream;
Import Java.io.OutputStream;
public class Copy {
public static void Main (string[] args) throws IOException {
File s = new file (".. /test ");
File T = new file (".. /yang ");
CopyFolder (s,t);
}
/**
* Copy a directory and its subdirectories, files to another directory
* @param src
* @param dest
* @throws IOException
*/
static void CopyFolder (file src, file dest) throws IOException {
if (Src.isdirectory ()) {
if (!dest.exists ()) {
Dest.mkdir ();
}
String files[] = Src.list ();
for (String file:files) {
File Srcfile = new file (src, file);
File DestFile = new file (dest, file);
Recursive replication
CopyFolder (Srcfile, destfile);
}
} else {
InputStream in = new FileInputStream (SRC);
OutputStream out = new FileOutputStream (dest);
byte[] buffer = new byte[1024];
int length;
while (length = in.read (buffer)) > 0) {
Out.write (buffer, 0, length);
}
In.close ();
Out.close ();
}
}
}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Java recursively copying files and folders