Code:
Public Static voidcopyFile (String srcpath, String destpath) throws IOException {//the origin and purpose of establishing a file objectFile src =NewFile (Srcpath); File dest=NewFile (DestPath); //determines whether the input stream is a file because it can only copy files if(!Src.isfile ()) {System. out. println ("only files can be copied!"); Throw NewIOException ("only files can be copied!"); } //file input and outputInputStream is=NewFileInputStream (SRC); OutputStream OS=NewFileOutputStream (dest); //reads the file size and loops out the file byte[] Flush =New byte[1024x768]; intLen =0; while(-1! = (len = is. Read (flush))) {Os.write (flush,0, Len); } //refreshes this output stream and forces all buffered output bytes to be written outOs.flush (); //freeing resources (typically open first and then off)Os.close (); is. Close (); System. out. println ("Copy succeeded!"); }
Write a simple file copy program in Java.