Unify all files in a multi-level folder into one folder with Java implementation

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.