Import the Java. IO. *;
Public class FileDirTest01 {
Public static void main (String [] args) {
//copy the folder
The File in=new File (" ");
//copy the target (directory)
The File out=new File (" ");
//call the method
CopyDir (in, out);
}
/* *
* copy files inside folders and documents,
* @ param copy files in directory
* @ param out copies of the target (directory)
*/
Private static void CopyDir (File, in the File out) {
If (in isFile ()) {
//srcFile if a file is, the recursive end,
//is time need to copy files,
//... Read write,
FileInputStream input=null;
FileOutputStream output=null;
Try {
//read the file
Input=new FileInputStream (in);
//write files to the new directory as out. The getAbsolutePath () + "\ " + in the getName ()
Output=new FileOutputStream (out getAbsolutePath () + "/" + in the getName ());
Byte [] bytes=new byte (1024 * 1024),//a copy 1 m
Int readCount=0;
/* *
* copy files a copy of 1024 * 1024 bytes (1 m)
*/
While ((readCount=input. Read (bytes))!=1) {
The output. The write (bytes, 0, readCount);
}
The output. The flush ();
} the catch (FileNotFoundException e) {
e.printStackTrace();
}
The catch (IOException e) {
e.printStackTrace();
} the finally {
If (input!=null) {
Try {
Input. The close ();
} the catch (IOException e) {
e.printStackTrace();
}
}
If (the output!=null) {
Try {
The output. The close ();
} the catch (IOException e) {
e.printStackTrace();
}
}
}
return;
}
The File files []=in. ListFiles ();
For (the File File: files) {
If (the file. The isDirectory ()) {//determine whether is folder, it is
//new directory as out. GetAbsolutePath () + "/" + in the getName ()
File newDir=new File (out getAbsolutePath () + "\ " + in the getName ());
if (! NewDir. The exists ()) {//determine whether this folder, no
NewDir. The file.mkdirs ();//create subdirectories
}
CopyDir (file, newDir);
System. The out. Println (newDir getAbsolutePath ());
System. The out. Println (file. GetAbsolutePath ());
}
//recursive method CopyDir
CopyDir (file, out);
}
}
}