File, InputStream, and OutputStream, Befferedinputstream, and Bufferedoutputstream three ways to copy files

Source: Internet
Author: User

1 ImportJava.io.File;2 3  Public classShowfile {4 5      Public Static voidMain (string[] args) {6 7File dir =NewFile ("E:/aaa");8         if(Dir.exists ()) {9 Ten Show (dir, dir); One}Else { ASYSTEM.OUT.PRINTLN ("This directory does not exist"); -         }         -     } the  -     /** - * Show -      *  +      * @paramfile1: - * Current object (directory or file) +      * @paramfile2: A * Root directory at      */ -      -      Public Static voidShow (file file1, file file2) { -         if(File1.isdirectory ()) { -  -file[] Files =file1.listfiles (); in  -              for(inti = 0; i < files.length; i++) { to              +Showfile SF =Newshowfile (); -                 intn =Sf.num (Files[i], file2); the                  *                  for(intj = 0; J < N; J + +) { $System.out.print ("\ t");Panax Notoginseng                 } - System.out.println (Files[i].getname ()); the                 //Recursive invocation + Show (Files[i], file2); A  the             } +         } -     } $  $     intb = 0; -      Public  intnum (file file1, file file2) { -  the         //Compare the current file and root directory -         //if File1.getparentfile () and file2 are equal, they represent folders of the same rankWuyi         if(File1.getparentfile (). Equals (File2)) { the             returnb; -}Else { Wub++; -             returnnum (File1.getparentfile (), file2); About         } $  -     } -  -}
1  Public classCopy {2      Public Static voidMain (string[] args)throwsIOException {3String Path = "E:" +file.separator+ "SDF";4     5File File =NewFile (Path, "Abc.rar" );6     7File file1 =NewFile (Path, "Cba.rar");8         9InputStream is =Newfileinputstream (file);Ten          OneOutputStream Ops =NewFileOutputStream (file1); A          -         byte[] by =New byte[20*1024]; -          the         intn = 0;  -      -          while(N=is.read (by))!=-1){ -          + Ops.write (by); - System.out.println (n); +              A         } at          -         /** - read (file file) in the *io stream, - * Reads a byte of data from this input stream. If no input is available, this method will block.  - returns: The next data byte, or 1 if the end of the file has been reached.  - read (file file) ultimately returns the total number of bytes owned by the target file being read in          */ -          to is.close (); + Ops.flush (); - ops.close (); the          *     } $ Panax Notoginseng}
1 /**2 *bufferedinputstream adds some functionality to another input stream, which is buffered input and the ability to support the mark and reset methods. 3 * When creating bufferedinputstream, an internal buffer array is created. 4 * When reading or skipping bytes in a stream, the internal buffer can be populated again from the included input stream as needed, populating multiple bytes at once. 5 The *mark operation Records a point in the input stream, and the reset action causes the new byte to be fetched from the included input stream before the6 * Read all bytes read again from the last mark operation. 7  */8  Public classbuffercopy {9      Public Static voidMain (string[] args)throwsIOException {TenString Path = "E:" +file.separator+ "SDF" +file.separator+ "Abc.rar"; One          A         //determine if the same file exists under this path -         if(!NewFile (NewFile (Path). Getparentfile (), "Sdfd.rar"). Exists ()) { -  the             //creates an output stream that reads the file data of the Files object -InputStream is =NewFileInputStream (NewFile (path)); -             //implementing the output stream that gets the data -Bufferedinputstream bis =NewBufferedinputstream (IS); +              -             //creates an output stream that writes file data to the Files object +OutputStream OS =NewFileOutputStream (NewFile (NewFile (Path)). Getparentfile (), "Sdfd.rar")); A             //implementing an output stream that writes data atBufferedoutputstream BOS =Newbufferedoutputstream (OS); -             //Create a byte[] buffer to ensure that the capacity is large enough -             byte[] by =New byte[20*1024]; -             intn = 0; -              while((n = is.read ())! = 1){ - Os.write (by); in             } -              to is.close (); + Os.flush (); - os.close (); the         } *System.out.println ("exists"); $         Panax Notoginseng     } -  the}

File, InputStream, and OutputStream, Befferedinputstream, and Bufferedoutputstream three ways to copy files

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.