Comparison of usage and efficiency of Java-io byte stream

Source: Internet
Author: User

I'm going to make a series that says basic character stream usage, this blog introduces the basic usage of byte stream:

First, basic use:

Basic byte stream:

FileInputStream FileOutputStream

Bufferedinputstream Bufferedoutputstream

Note that the use of byte stream and character stream is basically similar, the difference is that using a byte stream to read and write files is not necessary to use the flush function, the reason:

1, the character stream needs to use the flush function to write the characters in the buffer to the file, because the character stream = encoded dictionary + byte stream, in fact, the character stream reads or bytes, but after reading a byte, it will find the corresponding character according to the character set;

2, the byte stream is the direct operation of bytes, so it is written to write directly to the file, do not need to flush;

3, FileInputStream FileOutputStream is inherited by the flush method, but this method has nothing to do. In FileOutputStream the flush source code is like this:

    public void Flush () throws IOException {    }

The reason why this function is required is that the subclass may be needed.

Here is the basic code for the demo:

The two global variables are:

1, Line_separate: The current system environment line break;

2, Root_dir: The current project's running directory (we create the file if there is no special designation is in this directory);

 private final static String line_separate = System.getproperty ("Line.separator"    );    Private final static String Root_dir=system.getproperty ("User.dir");        public static void Main (string[] args) throws IOException {System.out.println (root_dir);            Try (outputstream out = new FileOutputStream ("In.txt")) {Out.write ("Lakeside"). GetBytes ());            Out.write (Line_separate.getbytes ());            Out.write ("123456". GetBytes ());        Out.write (Line_separate.getbytes ());            } try (InputStream in = new FileInputStream ("In.txt")) {byte[] Contentarray = new byte[512];            int length = In.read (Contentarray);                Try (outputstream out = new FileOutputStream ("NewIn.txt")) {out.write (contentarray, 0, length);            System.out.println (New String (Contentarray, 0, length)); }        }    }

Note that what I write above is very short, and I read the 512-bit array all at once, so I don't use the while loop.

The following is the output result:

E:\EXE\eke.test.first Lakeside 123456

Second, using Bufferedinputstream and bufferedoutputstream with buffers and using different methods to copy a file compare efficiency:

(Comparisons of these methods are written after watching the podcast video)

Directly on the code:

public static void Main (string[] args) throws IOException {
Long copy_2_start_time = System.currenttimemillis ();
Copy_2 ();
Long copy_2_end_time = System.currenttimemillis ();
System.out.println ("Copy_2 Run Time:" + (Copy_2_end_time-copy_2_start_time) + "MS");

Long copy_1_start_time = System.currenttimemillis ();
Copy_1 ();
Long copy_1_end_time = System.currenttimemillis ();
System.out.println ("Copy_1 Run Time:" + (Copy_1_end_time-copy_1_start_time) + "MS");

Long copy_0_start_time = System.currenttimemillis ();
Copy_0 ();
Long copy_0_end_time = System.currenttimemillis ();
System.out.println ("Copy_0 Run Time:" + (Copy_0_end_time-copy_0_start_time) + "MS");

}

private static void Copy_1 () throws IOException {
Try (FileInputStream in = new FileInputStream ("Green small video. mp4")) {
Try (bufferedinputstream Bufin = new Bufferedinputstream (in, 102400)) {
int content;
Try (FileOutputStream out = new FileOutputStream ("Copy_1.mp4")) {
Try (bufferedoutputstream bufout = new Bufferedoutputstream (out, 102400)) {
while (content = In.read ())! =-1) {
Bufout.write (content);
}
}
}
}
}
}

private static void Copy_0 () throws IOException {
Try (FileInputStream in = new FileInputStream ("Green small video. mp4")) {
Try (FileOutputStream out = new FileOutputStream ("Copy_0.mp4")) {
byte[] Contentarray = new byte[102400];
int length = 0;
while (length = In.read (Contentarray))! =-1) {
Out.write (contentarray, 0, length);
}
}
}
}

private static void Copy_2 () throws IOException {
Try (FileInputStream in = new FileInputStream ("Green small video. mp4")) {
Try (FileOutputStream out = new FileOutputStream ("Copy_2.mp4")) {
int ch;
while ((ch = in.read ())! =-1) {
Out.write (CH);
}
}
}
}

Output Result:

Connected to the target VM, address: ' 127.0.0.1:53146 ', Transport: ' Socket ' copy_2 run time: 69494 milliseconds copy_1 run time: 9119 milliseconds copy_0 shipped Line time: 9 Ms disconnected from the target VM, address: ' 127.0.0.1:53146 ', Transport: ' Socket '

Analysis:

The Copy_2 method is to use FileOutputStream and FileInputStream, one of their own bytes of reading; slow to die;

The Copy_1 method uses Bufferedinputstream and bufferedoutputstream with buffers, and the speed is greatly improved, but still a slow one;

The Copy_0 method uses a buffer of its own creation, which takes only 9 milliseconds to read 102,400 bytes at a time.

The difference between copy_1 and COPY_0 is that both reads and writes are taken from the respective buffer, but Copy_1 does not use an array, or only one byte at a time, so the speed is dropped by the copy_0.

Comparison of usage and efficiency of Java-io byte stream

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.