Four ways in which Java implements File replication

Source: Internet
Author: User

Background: There are a lot of Java beginners for the operation of the file copy is always confused, the following I will be in 4 ways to achieve the specified file replication.

Implementation method One: using Fileinputstream/fileoutputstream byte stream for file copy operation

1 Private Static voidStreamcopyfile (file srcfile, file desfile)throwsIOException {2         //using byte streams for file copying3FileInputStream fi =NewFileInputStream (srcfile);4FileOutputStream fo =NewFileOutputStream (desfile);5Integer by = 0;6         //read one byte at a time7          while(by = Fi.read ())! =-1) {8 Fo.write (by);9         }Ten fi.close (); One fo.close (); A}

Implementation method Two: Using Bufferedinputstream/bufferedoutputstream high-efficient byte stream to copy files

1 Private Static voidBufferedstreamcopyfile (file srcfile, file desfile)throwsIOException {2         //file copying using buffered byte streams3Bufferedinputstream bis =NewBufferedinputstream (NewFileInputStream (srcfile));4Bufferedoutputstream BOS =NewBufferedoutputstream (NewFileOutputStream (Desfile));5         byte[] B =New byte[1024];6Integer len = 0;7         //read 1024 bytes of data at a time8          while(len = Bis.read (b))! =-1) {9Bos.write (b, 0, Len);Ten         } One bis.close (); A bos.close (); -}

Implementation method Three: Use the Filereader/filewriter character stream for file copying. (Note that this way you can only copy files that contain only characters, which means that you can read them by opening the file in Notepad.)

1 Private Static voidReaderwritercopyfile (file srcfile, file desfile)throwsIOException {2         //using character streams for file copying, note: Character streams can only copy files that contain only Chinese characters3FileReader FR =NewFileReader (srcfile);4FileWriter FW =NewFileWriter (desfile);5         6Integer by = 0;7          while(by = Fr.read ())! =-1) {8 Fw.write (by);9         }Ten          One fr.close (); A fw.close (); -}

Implementation mode four: Use Bufferedreader/bufferedwriter high-efficient character stream for file copying (note that this way you can only copy files that contain only characters, which means that you can read the file by using Notepad)

1 Private Static voidBufferedreaderwritercopyfile (file srcfile, file desfile)throwsIOException {2         //using efficient character streams with buffers for file copying3BufferedReader br =NewBufferedReader (NewFileReader (srcfile));4BufferedWriter BW =NewBufferedWriter (NewFileWriter (Desfile));5         6         Char[] C =New Char[1024];7Integer len = 0;8          while(len = Br.read (c))! =-1) {9Bw.write (c, 0, Len);Ten         } One          A         //Mode two -         /*String s = null; - While ((s = br.readline ()) = null) { the Bw.write (s); - bw.newline (); -         }*/ -          + br.close (); - bw.close (); +}

The above is the Java in the use of byte stream, high-efficient byte stream, character stream, high-efficient character stream four ways to achieve File replication Method!

Four ways in which Java implements file replication

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.