1 import Java.io.ByteArrayInputStream;2 import Java.io.ByteArrayOutputStream;3 import Java.io.File;4 import Java.io.FileInputStream;5 import Java.io.FileOutputStream;6 import java.io.IOException;7 import Java.io.InputStream;8 import Java.io.OutputStream;9 Ten /** One * Copy of File: A * Idea: By recursively implementing the file directory replication, through the stream to copy files. - * Method: - * 1. Traverse directories and files and, if directory, first copy the directory and its subdirectories to the specified directory. the * 2. If a non-directory copies the files in the directory to the specified directory in the form of a stream. - * 3. Call your own method to implement recursion. - * 4. So that all subdirectories and files under the directory and its directory are replicated to the specified directory. - * Technology used: + * Use the file class to replicate the files directory. IO stream for copying of file text - * @author User + * A */ at - Public classCopyfiletest { - - Public Static voidMain (string[] args) { -String Srcpath ="E:\\WORKSPACES\\DAY16"; -String DestPath ="F:\\WORKSPACES\\DAY16"; inFile src =NewFile (Srcpath);//source File -File dest =NewFile (DestPath);//destination File to + Try { - copyfolder (SRC, dest); the}Catch(IOException e) { * //TODO auto-generated Catch block $ e.printstacktrace ();Panax Notoginseng } -System. out. println ("Program Execution Complete"); the } + A Private Static voidcopyfolder (file src, file dest) throws IOException { the if(Src.isdirectory ()) {//Interpreting whether the table of contents + if(!dest.exists ()) {//determine if the path name exists -Dest.mkdirs ();//Create a file directory and its subdirectories $ } $ //src.list () returns an array of strings that specify the files and directories in the directory represented by this abstract path name. -String files[] = Src.list ();//create a string array to store the file path - //traversing file Directories the for(String file:files) { - //creates a new File instance based on the parent abstract pathname and child pathname stringWuyiFile Srcfile =Newfile (src, file); theFile DestFile =Newfile (dest, file); - //Recursive replication Wu CopyFolder (Srcfile, destfile); - } About}Else { $ //creating a Stream object -InputStreaminch=NewFileInputStream (SRC); -OutputStream out=NewFileOutputStream (dest); - A //Create a byte array to be used as a buffer + byte[] buffer =New byte[1024x768]; the - //adding data to the buffer array $ inch. Read (buffer); the the //load data into the cache theBytearrayinputstream bis =Newbytearrayinputstream (buffer); the - //Instantiate a Bytearrayoutputstream inBytearrayoutputstream BOS =NewBytearrayoutputstream (); the the //defining a String data AboutString str =bis.tostring (); the //define a byte[] array the byte[] B =str.getbytes (); the //Copy the contents of B to a buffer +Bos.write (b,0, b.length); - //gets the size of the current buffer theSystem. out. println (Bos.size ());Bayi the //writes the data of the buffer to the OutputStream theBos.writeto ( out); - - //Close the stream the inch. Close (); the out. Close (); the } the } - the}
java--using recursion and IO streams for file replication