JAVA io copies files using a byte stream in the buffer area __java

Source: Internet
Author: User

A good memory is better than a pen.

Examples of character streams with buffer areas

Package com.

Ckinghan.outputstream;
Import Java.io.BufferedInputStream;
Import Java.io.BufferedOutputStream;
Import Java.io.FileInputStream;
Import java.io.FileNotFoundException;
Import Java.io.FileOutputStream;
Import java.io.IOException;
Import Java.io.InputStream;

Import Java.io.OutputStream; /** * * @author Ckinghan * Description: Byte stream with efficient buffer * * public class Bufferedinputstreamdemo {public static void main (St

        Ring[] args {/** * Read the copied file using a byte stream with an efficient buffer, copy: Copy one character at a time/bufferedinputstreamreader ();

    /** * Read the copied file in a byte stream with an efficient buffer, copying: Copy the specified number of bytes at a time/bufferedinputstreamcopyfile (); /** * @ Description: Read the copied file using a byte stream with an efficient buffer, copy by: Copy the specified number of bytes at a time: */public static void Bufferedinputstreamco
        Pyfile () {///create byte stream object InputStream inputstream = null;

        OutputStream outputstream = null; try {//instantiated efficient buffer byte stream input, output object InputStream = new Bufferedinputstream (New FileinputstreAM ("E:/weblogin.log"));
            OutputStream = new Bufferedoutputstream (New FileOutputStream ("E:/new.log"));
            The number of file bytes per read int len =-1;
            Each read the file byte data byte[] bs = new byte[1024]; Iterate through the file until it is read to the end of the file while (len = Inputstream.read (BS))!=-1 {//write to file outputs
            Tream.write (BS, 0, Len);
            //empty buffer to save the data written to the file Outputstream.flush ();
        Replication Successful output System.out.println ("Use efficient buffer byte to read a specified number of bytes at a time to copy files completed");
        catch (FileNotFoundException e) {e.printstacktrace ();
        catch (IOException e) {e.printstacktrace (); }finally {try {//close stream if (outputstream!= null) outputstre
                Am.close ();
            if (InputStream!= null) inputstream.close ();
  catch (IOException E2) {e2.printstacktrace ();          /** * @ Description: Read the copied file using a byte stream with an efficient buffer copy: Copy one character at a time * @ Created:/public static VO
        ID Bufferedinputstreamreader () {///create byte stream object InputStream inputstream = null;

        OutputStream outputstream = null; try {//instantiate efficient buffer byte stream input, output object InputStream = new Bufferedinputstream (New FileInputStream ("E:/weblogin
            . log "));

            OutputStream = new Bufferedoutputstream (New FileOutputStream ("E:/login.log"));

            Bytes read acsii code int val =-1; Iterate through the file until it is read to the end of the file while ((val = Inputstream.read ())!=-1) {//write to file Outputstrea
            M.write (Val);
            //empty buffer to save the data written to the file Outputstream.flush ();
        Copy successfully output SYSTEM.OUT.PRINTLN ("read one byte at a time with efficient buffer stream copy file complete");
        catch (FileNotFoundException e) {e.printstacktrace ();
        catch (IOException e) {e.printstacktrace ();Finally {try {//close stream if (outputstream!= null) outputstr
                Eam.close ();
            if (InputStream!= null) inputstream.close ();
            catch (IOException E2) {e2.printstacktrace ();
 }
        }

    }

}

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.