Every time the film (boys Know Yo), each movie is placed in a separate folder, look at the time is very inconvenient Ah, has been repeated into the folder, back, and then back to the operation, and manually put these movies are all copied out and too cumbersome. So in order to solve this problem, I wrote a gadget with IO.
The following code only implements the multi-level folder to copy all the files into a folder, if you need to specify the format of the copied file, you can add a judgment at 1111. If you need to delete the original folder at the same time, you can add a recursive delete method.
Package Bao;
Import Java.io.BufferedInputStream;
Import Java.io.BufferedOutputStream;
Import Java.io.File;
Import Java.io.FileInputStream;
Import Java.io.FileOutputStream;
Import java.io.IOException;
public class Copyfolderdemo {
public static void Main (string[] args) throws IOException {
Encapsulate Data source file
File Srcfile = new file ("d:\\ folder name"); //Fill in the source folder path here
CopyFolder (Srcfile);
CopyFile (Srcfile);
}
private static void CopyFolder (File srcfile)
Throws IOException {
Determine if the file is a folder or a document
File[] Ff=srcfile.listfiles ();
for (File f:ff) {
If it is a folder, it is called recursively
if (F.isdirectory ()) {
CopyFolder (f);
}else{ ///11111
If it's a file, copy it.
CopyFile (f);
System.out.println (F.getname ());
}
}
}
Implementing file replication with byte buffer streams
private static void CopyFile (File srcfile) throws IOException {
File DestFile = new file ("D:\\ folder name", Srcfile.getname ()); //Write destination folder path here
Bufferedinputstream bis = new Bufferedinputstream (New FileInputStream (
Srcfile));
Bufferedoutputstream BOS = new Bufferedoutputstream (
New FileOutputStream (DestFile));
byte[] bys = new byte[1024];
int len = 0;
while (len = Bis.read (bys))! =-1) {
Bos.write (bys, 0, Len);
}
Bos.close ();
Bis.close ();
}
}
Unify all files in a multi-level folder into one folder with Java implementation