Sockets for Java IO input/output streams

Source: Internet
Author: User

Buffered character Stream Bufferedinputstream/bufferedoutputstream

File Operation Flow Inputstream/outputstream

The socket of the Java input and output stream, test the speed of the flow chain

Because the node flow is not very common in the program, usually through the filter flow to connect multiple flows together, using the characteristics of each flow to work together the data, the multiple flows of the socket constitute a flow chain;

Let's analyze their running time. (because the time may vary on different systems (Maxox,linux,win (32/64)), I've been intercepting it for a few days to compare.

1. buffered character Stream Bufferedinputstream/bufferedoutputstream

Import Java.io.bufferedinputstream;import Java.io.bufferedoutputstream;import java.io.file;import Java.io.fileinputstream;import Java.io.fileoutputstream;import java.io.IOException; Public classMain { Public Static voidMain (string[] args) throws IOException {LongStartTime =System.nanotime (); File Address=NewFile ("D:"+file.separator+"C.txt"); File Address1=NewFile ("D:"+file.separator+"Ww.txt"); Bufferedinputstream bin=NewBufferedinputstream (NewFileInputStream (address)); Bufferedoutputstream bout=NewBufferedoutputstream (NewFileOutputStream (Address1)); intC;  while((C=bin.read ())!=-1) {bout.write (c);        } bin.close ();        Bout.flush ();        Bout.close (); System. out. println ("Copy Complete"); LongEndTime =System.nanotime (); System. out. println ((endtime-starttime) +"Ms"); }}

2. file operation Flow Inputstream/outputstream

ImportJava.io.File;ImportJava.io.FileInputStream;ImportJava.io.FileOutputStream;Importjava.io.IOException;ImportJava.io.InputStream;ImportJava.io.OutputStream; Public classMain { Public Static voidMain (string[] args)throwsIOException {LongStartTime =System.nanotime (); File Address=NewFile ("D:" +file.separator+ "C.txt"); File Address1=NewFile ("D:" +file.separator+ "Ww.txt"); InputStream in=NewFileInputStream (address); OutputStream out=NewFileOutputStream (ADDRESS1); intC;  while((C=in.read ())!=-1) {out.write (c);        } in.close ();        Out.flush ();        Out.close (); System.out.println ("Copy Complete"); LongEndTime =System.nanotime (); System.out.println ((EndTime-starttime) + "MS"); }}

3.sockets for Java input and output streams

ImportJava.io.BufferedInputStream;ImportJava.io.BufferedOutputStream;ImportJava.io.DataInputStream;ImportJava.io.DataOutputStream;ImportJava.io.File;ImportJava.io.FileInputStream;ImportJava.io.FileOutputStream;Importjava.io.IOException;ImportJava.io.InputStream;ImportJava.io.OutputStream; Public classMain { Public Static voidMain (string[] args)throwsIOException {LongStartTime =System.nanotime (); File Address=NewFile ("D:" +file.separator+ "C.txt"); File Address1=NewFile ("D:" +file.separator+ "Ww.txt"); Bufferedinputstream bin=NewBufferedinputstream (NewFileInputStream (address)); Bufferedoutputstream bout=NewBufferedoutputstream (NewFileOutputStream (Address1)); InputStream din=NewDataInputStream (BIN); OutputStream dout=Newdataoutputstream (bout); intC;  while((c = din.read ())!=-1) {dout.write (c);        } bin.close ();        Bout.flush ();                Bout.close ();        Din.close ();        Dout.flush ();                Dout.close (); System.out.println ("Copy succeeded"); LongEndTime =System.nanotime (); System.out.println ((EndTime-starttime) + "MS"); }}

Their uptime is somewhat similar because they read only one character at a time and then write to the file.

Their use needs to look at the scene selection:

Select FileInputStream class for normal file input and output

Select the buffering class when faster input and output is required

Selecting a socket for the Java input and output stream at multiple inputs and outputs makes it easy to process data and improve processing efficiency.

Sockets for Java IO input/output streams

Related Article

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.