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/>