Copy objects in Java

Source: Internet
Author: User

well, I haven't written a blog for a long time. Today I encountered a problem. I had to copy a file and wanted to look for it online. I found that none of the files were very concise, maybe there is a reason for the company's network restrictions, so you can write only one simple one by yourself.

Import Java. io. file; <br/> Import Java. io. fileinputstream; <br/> Import Java. io. fileoutputstream; <br/> Import Java. io. ioexception; <br/>/** <br/> * use <SPAN class = 'wp _ keywordlink '> composition </span> to process the composition, currently, only files and folders are copied. <Br/> * @ author Jie. xiang <br/> */<br/> public class fileutil {<br/> Public final static int byte_length = 1024; <br/> Public final static void copyfile (File sourcefile, file targetfolder) <br/> throws ioexception {<br/> If (! Sourcefile. exists () |! Targetfolder. exists () {<br/> return; <br/>}< br/> string targetname = targetfolder. getabsolutepath () + file. separator <br/> + sourcefile. getname (); <br/> file targetfile = new file (targetname); <br/> If (sourcefile. isdirectory () {<br/> If (! Targetfile. exists () {<br/> targetfile. mkdir (); <br/>}< br/> file [] children = sourcefile. listfiles (); <br/> for (file child: Children) {<br/> copyfile (child, targetfile ); <br/>}< br/>} else {<br/> copy (sourcefile, targetfile ); <br/>}< br/>/** <br/> * copy the directory of the source file to the target file. <Br/> * @ Param sourcefile <br/> * @ Param targetfile <br/> * @ throws ioexception <br/> *@ author Jie. xiang <br/> */<br/> Public static void copy (File sourcefile, file targetfile) <br/> throws ioexception {<br/> If (! Sourcefile. isfile ()&&! Targetfile. exists () {<br/> return; <br/>}< br/> fileinputstream in = new fileinputstream (sourcefile ); <br/> fileoutputstream out = new fileoutputstream (targetfile); <br/> byte [] arr = new byte [byte_length]; <br/> int length =-1; <br/> while (length = in. read (ARR ))! =-1) {<br/> out. write (ARR, 0, length); <br/>}< br/> in. close (); <br/> out. flush (); <br/>}< br/>/** <br/> * source file and target file directory <br/> * @ Param sourcefilepath <br/> * @ Param targetfolderpath <br/> * @ throws ioexception <br/> * @ author Jie. xiang <br/> */<br/> Public final static void copyfile (string sourcefilepath, <br/> string targetfolderpath) throws ioexception {<br/> file sourcefile = new file (sourcefilepath); <br/> file targetfolder = new file (targetfolderpath); <br/> copyfile (sourcefile, targetfolder ); <br/>}< br/>/** <br/> * @ Param ARGs <br/> */<br/> Public static void main (string [] ARGs) {<br/> // todo auto-generated method stub <br/> try {<br/> copyfile ("E: // workspace // Java API ", "D: // Java API"); <br/>}catch (ioexception e) {<br/> E. printstacktrace (); <br/>}< br/>

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.