Black Horse programmer efficiency Comparison---Basic byte stream and efficient byte stream

Source: Internet
Author: User

------- Android Training , Java training , look forward to communicating with you! ----------

1: byte stream
(1) Inheritance system
inputstream fileinputstream
outputstream    fileoutputstream
2: Efficient flow
(1) Character efficient flow
Span style= "White-space:pre" > bufferedreader
bufferedwriter
Span style= "White-space:pre" > (2) bytes efficient flow
bufferedinputstream
Span style= "White-space:pre" > bufferedoutputstream
(3) characters special functions of efficient flow
bufferedreader String readLine ()
bufferedwriter  void newLine ()

3: Comparison of basic byte stream and efficient byte stream:


Package Com.itheima;import Java.io.bufferedinputstream;import Java.io.bufferedoutputstream;import Java.io.fileinputstream;import Java.io.fileoutputstream;import Java.io.ioexception;public class Test11 {public static void Main (string[] args) throws IOException {Long start = System.currenttimemillis (); Method1 ("C:\\ snow. mkv", " Test.mkv "); Long end = System.currenttimemillis (); System.out.println ("The basic byte stream reads and writes one bytes at a time:" + (End-start) + "milliseconds"); start = System.currenttimemillis (); Method2 ("C:\\ snow. mkv", "Test.mkv"); end = System.currenttimemillis (); System.out.println ("The basic byte stream reads and writes a bytes array at a time:" + (End-start) + "milliseconds"); start = System.currenttimemillis (); Method3 ("C:\\ snow. mkv "," test.mkv "); end = System.currenttimemillis (); System.out.println ("High-efficiency byte stream reads and writes one byte at a time:" + (End-start) + "milliseconds"); start = System.currenttimemillis (); METHOD4 ("C:\\ snow. mkv", "Test.mkv"); end = System.currenttimemillis (); SYSTEM.OUT.PRINTLN ("High efficiency byte stream reads and writes one byte array at a time:" + (End-start) + "milliseconds");} High efficiency byte stream read and write one byte array private static void Method4 (String srcstring, StriNg deststring) throws IOException {Bufferedinputstream bis = new Bufferedinputstream (new FileInputStream (srcstring)); Bufferedoutputstream BOS = new Bufferedoutputstream (new FileOutputStream (deststring)); byte[] Bys = new Byte[1024];int Len = 0;while (len = Bis.read (bys))! =-1) {bos.write (bys, 0, Len);} Bos.close (); Bis.close ();} High-efficiency byte stream reads and writes one byte at a time private static void Method3 (String srcstring, String deststring) throws IOException {Bufferedinputstream bis = new Bufferedinputstream (new FileInputStream (srcstring));  Bufferedoutputstream BOS = new Bufferedoutputstream (new FileOutputStream (deststring)); int by = 0;while (by = Bis.read ()) ! =-1) {bos.write (by);} Bos.close (); Bis.close ();} The basic byte stream reads and writes a bytes array private static void Method2 (String srcstring, String deststring) throws IOException {FileInputStream FIS = new FileInputStream (srcstring); FileOutputStream fos = new FileOutputStream (deststring); byte[] bys = new Byte[1024];int len = 0;while (len = Fis.read (bys )! =-1) {fos.write (bys, 0, Len);} Fos.closE (); Fis.close ();}  A basic byte stream reads and writes a bytes of private static void Method1 (String srcstring, String deststring) throws IOException {FileInputStream FIS = New FileInputStream (srcstring); FileOutputStream fos = new FileOutputStream (deststring); int by = 0;while (by = Fis.read ())! =-1) {fos.write (by);} Fos.close (); Fis.close ();}}

Results after the run:

The basic byte stream reads and writes one bytes at a time: 95580 milliseconds read and write a byte array at a time: 153 milliseconds high efficient byte stream reads and writes one byte at a time: 375 milliseconds high-efficiency byte stream reads and writes a single octet at a time: 127 milliseconds

4: Buffered character stream (Bufferedreader/bufferedwriter)

1, the use of buffer processing is to improve efficiency, if there is no cache, such as the FileReader object, each time the read () method is called, it will go directly to the file read bytes, converted into characters and returned, so that the frequent reading of the file efficiency is very low.

2. The appearance of buffered character stream improves the operation efficiency of convection, the principle is to encapsulate the array.

3, in the use of buffered character stream object, the existence of buffering is to enhance the function of the stream, so in the establishment of a buffered character stream object, the existence of a stream object first.


BufferedReader Unique method: public string ReadLine ();//read one line at a time, and return the character data before the row tag as a string when the row is marked. When the end is read, NULL is returned.

BufferedWriter Unique method: Publicvoid newLine ();//write the platform-related line delimiter to mark the end of a line. The Windows platform is ' \ n '.

<pre name= "code" class= "Java" >public class Copyfiledemo {public static void main (string[] args) throws IOException { Encapsulates the data source BufferedReader br = new BufferedReader (New FileReader ("Bufferedreaderdemo.java"));//package destination BufferedWriter BW = New BufferedWriter (New FileWriter ("Copy.java"));//Read and write String line = Null;while ((line = Br.readline ()) = null) {Bw.write (l INE); Bw.newline (); Bw.flush ();} Release resources Bw.close (); Br.close ();}}



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Black Horse programmer efficiency Comparison---Basic byte stream and efficient 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.