Java calls copy to copy subfolders and files to the specified directory (non-xcopy)

Source: Internet
Author: User

Because I'm bored because I'm bored ... (Why not first check if there is no ready-made orders, regret Ah!!) )

Do not know to have xcopy this command, I want to write a copy subdirectory and file function ... The following story was born

Is this, yesterday in the school to make semi-finished, semi-finished finish night with friends play CF ... He is a hero of the team!!

Then the next morning to go to school to take semi-finished test, found that directly with copy can only copy files Ah, can not directly copy files in subdirectories (directory refers to folders, subdirectories are folders and folders, of course, there may be files) .... What the hell??? Tangled up a bit, began to change the semi-finished slightly, it seems that there is no egg to use.

Then just listen to the song, thinking, in a daze. (To my brother), the man who has been out of the regiment has come to talk to me from time to time.

Then went to eat, anyway, a morning thought of a general idea.

The first thing to know: There are n files in the directory, n directories, n directories with n files and n directories and so on, and so on, and so on (endless false exaggeration)

That's what I was thinking:

1. Determine if the file is copied directly to the target directory, if the directory is saved to the collection, save the target directory at the same time to create the directory to be copied first

if (is directory) {

Create a folder to copy in the destination folder

Save Path to Collection

}esel{

Execute copy command to assign value

}

2. It has just been said that there may be n files and N subdirectories in the catalogue a lot of possibilities (think about it)

Then I'll just add a loop to iterate through the directory in the collection, and then make a judgment, if it is the file or the same as the first time copy directly to the target directory

If the directory continues to be saved to the collection

Probably write like this:

if (is directory) {

Create a folder to copy in the destination folder

Save Path to Collection

for (int i=0 ...) {

if (is directory) {

Create a folder to copy in the destination folder

Save to Collection

}else{

Copy the past directly

}

}

}else{

Execute copy command to assign value

}

This is probably the way of thinking.

Paste the code:

1  PackageCom.d9.util;2 3 ImportJava.io.BufferedReader;4 ImportJava.io.File;5 Importjava.io.IOException;6 ImportJava.io.InputStreamReader;7 Importjava.util.ArrayList;8 Importjava.util.List;9 Ten  Public classDoscopytest { One  A      Public Static voidMain (string[] args)throwsIOException { -String path= "F:\\test1"; -String targetpath= "F:\\copytest"; theFile file1=NewFile (path); -File file2=NewFile (TargetPath); - file2.mkdirs (); -String strname []=file1.list (); +Runtime run=runtime.getruntime (); -Process p=NULL; +List<string> lpath=NewArraylist<string>(); A          for(String fn:strname) { atFile2=NewFile (path+ "\ \" +fn); -             if(File2.isdirectory ()) { -                 NewFile (targetpath+ "\ \" +fn). Mkdirs (); -Lpath.add ("\ \" +fn+ "\ \"); -                  for(intI=0;i<lpath.size (); i++) { -File2=NewFile (path+Lpath.get (i)); inString strname1 []=file2.list (); -                      for(String fn1:strname1) { to                         if(NewFile (Path+lpath.get (i) +fn1). Isdirectory ()) { +                             NewFile (Targetpath+lpath.get (i) +fn1). Mkdirs (); -Lpath.add (Lpath.get (i) +fn1+ "\ \"); the}Else { *P=run.exec ("cmd/c copy" +path+lpath.get (i) +fn1+ "" +targetpath+lpath.get (i) +fn1); $BufferedReader reader =NewBufferedReader (NewInputStreamReader (P.getinputstream ()));Panax NotoginsengString line =NULL; -                              while(line = Reader.readline ())! =NULL) { the System.out.println (line); +                             } A                         } the                     } +                 } -}Else { $P=run.exec ("cmd/c copy" +path+ "\ \" +fn+ "" +targetpath+ "\ \" +fn); $BufferedReader reader =NewBufferedReader (NewInputStreamReader (P.getinputstream ())); -String line =NULL; -                  while(line = Reader.readline ())! =NULL) { the System.out.println (line); -                 }Wuyi             } the         } -     } Wu}

This allows you to copy the tape and files together and test the problem.

In fact, the morning is also written about the same, but the writing is a bit messy, at noon after the meal back in that change, change to class or not change the end.

After class or in that change ... Changed to Super annoying, because the idea is short-circuited, do not know what they are changing what code looks really a bit messy (admire the code you write)

Come home from school and sit for a while and plan to rewrite it again. Opened the machine to open eclipse and then the brain has been thinking of Spring breeze ten miles Spring breeze ten miles Spring breeze ten miles Spring breeze decade, in think my Youku member when expires ~ Expires ~ Expires ~ expires, want to think about directly opened the Youku, incredibly updated to 29 episodes

There are also 3 episodes to see ... At this moment I do not have to struggle to write code, directly open to see, even eat all watching, 3 set was KO. After watching the mood is good! The only thing that bothers me is the heroine's disgusting winter rain.

Good mood write code efficiency is high, write code efficiency high mood will be good. So relax and relax and do what you have to do.

After watching 10 o ' ... Then turn eclipse back on. Re-organized a little bit of thought

15 minutes to finish???

Use the DOS command to assign values to files and directories in the directory using the XCOP command.

such as: xcopy F:\a f:\b

In the future with this style to BB writing code process

Java calls copy to copy subfolders and files to the specified directory (non-xcopy)

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.