Java Multi-Folder merge __java

Source: Internet
Author: User

Has previously completed a single folder copy function, on this basis it is easy to implement a combination of multiple folders, set a For loop on it.


Package com.xx.test.copy;
Import Java.io.BufferedInputStream;
Import Java.io.BufferedOutputStream;
Import Java.io.File;
Import Java.io.FileInputStream;
Import java.io.FileNotFoundException;
Import Java.io.FileOutputStream;

Import java.io.IOException; public class Testcombinedirectory {/** * @param args */public static void main (string[] args) {string[]
		Sourcedirnames = {"D:/luceneex", "D:/luceneex2"};
		
		String targetdirname = "D:/luceneex_backup";
	Combinedirectory (Sourcedirnames,targetdirname); /** * Merge multiple folders into a folder * @param sourcedirnames * @param targetdirname/private static void Combinedirectory (S Tring[] Sourcedirnames,string targetdirname) {if (Sourcedirnames==null | | sourcedirnames.length==0) {throw new Run
		Timeexception ("folder to be merged does not exist ...");
		for (int i=0;i<sourcedirnames.length;i++) {copydir (sourcedirnames[i], targetdirname);
	System.out.println ("Merge all folders complete ..."); /** * Copy Directory, recursive method * @param sourcedirname * @param targetdirname */public static void Copydir (String sourcedirname, String targetdirname) {File SourceDir =
		New File (sourceDirName);
		
		File TargetDir = new file (targetdirname); if (Sourcedir==null | |!sourcedir.exists ()) {throw new RuntimeException ("The folder to be copied does not exist ..." +sourcedir.getabsolutepath ())
		;
		} if (!sourcedir.isdirectory ()) {throw new RuntimeException ("The file to be copied is not a directory ..." +sourcedir.getabsolutepath ());
		} if (!targetdir.exists ()) {targetdir.mkdirs ();
		
		} file[] files = sourcedir.listfiles (); for (int i=0;files!=null && i<files.length;i++) {if (Files[i].isfile ()) {//Copy file CopyFile (files[i)
				
			, New File (Targetdirname+file.separator+files[i].getname ())); }else if (files[i].isdirectory ()) {//Copy directory, recursive method//Copy directory String Dir1 = sourcedirname+file.separator+files[i]  
				. GetName ();  
                String Dir2 = Targetdirname+file.separator+files[i].getname ();  
			Copydir (Dir1, DIR2); } System. Out.println ("Copy folder successful ..." +sourcedir.getabsolutepath ()); /** * Copy Single file * @param sourcefile source file * @param targetfile target file/private static void CopyFile (file Sourcefil
		E, File targetfile) {fileinputstream in = null;
		Bufferedinputstream bis = null;
		FileOutputStream out = null;
		Bufferedoutputstream BOS = NULL;  
			try {//Create a new file input stream and buffer it in = new FileInputStream (sourcefile);  
  
			bis = new Bufferedinputstream (in);  
			Creates a new file output stream and buffers it out = new FileOutputStream (targetfile);  
			  
			BOS = new Bufferedoutputstream (out);  
			Buffer array byte[] b = New byte[1024 * 5];  
			int Len;  
			while (Len =bis.read (b))!=-1) {bos.write (b, 0, Len);  
			
		//Refresh the buffered output stream Bos.flush ();
		catch (FileNotFoundException e) {e.printstacktrace ();
		catch (IOException e) {e.printstacktrace ();
			}finally{try {if (bos!=null) bos.close ();
			catch (IOException e) {e.printstacktrace (); } try {if (biS!=null) Bis.close ();
			catch (IOException e) {e.printstacktrace ();
 }
		}
	}
}


There are wrong places to write or consider incomplete situation, but also please give us a lot of advice. Also, if you want to be more efficient, consider using NIO to complete a single file copy.




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.