Io Stream "byte throttling"

Source: Internet
Author: User
Tags flush

Io Stream "byte throttling"


The method of reading data in InputStream is as follows:

1, int read ()

Function: Reads a byte of data, and returns the read data, if return-1, then read to the end of the input stream.

2, int read (byte[] b)

Function: Reads a certain amount of bytes from the input stream and stores it in byte array B, returns the number of bytes actually read and, if returned-1, reads to the end of the input stream.

3, int read (byte[] b, int off, int len)

Function: Reads the data into a byte array, returns the actual number of bytes read, and, if returned-1, reads to the end of the input stream. OFF specifies the starting offset at which data is stored in array B, and Len specifies the maximum number of bytes read.

4, available ()

Function: returns this input stream the estimated number of bytes a method call can read or skip from this input flow without blocking.

5, Close ()

Function: Closes the input stream, releasing the associated resource for this stream.

The methods for writing data in OutputStream are as follows:

1, int write (int b)

Function: Writes the lowest byte of B to this input stream and discards the other three bytes.

2, int write (byte[] b)

Function: Writes the specified byte array b to this input stream.

3, int write (byte[] b, int off, int len)

Function: Writes the Len byte of the specified byte array from the offset off to the input stream.

4, Flush ()

Function: refreshes this input stream and forces the output bytes of all buffers to be written out.

5, Close ()

Function: Turn off the output stream and release the associated resources for this stream.

① byte array input stream:

Package com.iotest;

Import Java.io.ByteArrayInputStream;
Import java.io.IOException;
public class Bytearryinputstreamdemo {public
    static void Main (string[] args) throws IOException {
        String str = ' A Bcdefghijk ";
        byte[] Strbuf = Str.getbytes ();  The string is converted into a byte array
        bytearrayinputstream Bais = new Bytearrayinputstream (STRBUF);
        int data = Bais.read ();          Reads bytes while
        (data!=-1) {
            char upper = Character.touppercase ((char) data) from the byte array input stream;
            System.out.print (upper+ "");
            data = Bais.read ();
        }
        Bais.close ();
    }

Program Run Result: A B C D E F G H I J K

② byte array output stream:

Package com.iotest;

Import Java.io.ByteArrayOutputStream;
Import java.io.IOException;

public class Bytearrayoutputstreamdemo {public
    static void Main (string[] args) throws IOException {
        Bytearrayoutputstream BAOs = new Bytearrayoutputstream ();
        String s = "Welcome to use Bytearrayoutputstreamdemo";
        byte[] buf = S.getbytes ();  
        Baos.write (BUF); Writes the specified byte array to the byte array output stream
        System.out.println (baos.tostring ());  Converts the contents of a byte array output stream to a string output
        //Copy the contents of the byte array output stream into a byte array
        byte[] b = Baos.tobytearray ();
        for (int i = 0; i < b.length i++) {
            System.out.print ((char) b[i]);
        Baos.close ();
    }

Program Run Result:
Welcome to use Bytearrayoutputstreamdemo
Welcome to use Bytearrayoutputstreamdemo

Use of ③ file input and output streams

Package com.iotest;
Import Java.io.File;
Import Java.io.FileInputStream;
Import java.io.FileNotFoundException;
Import Java.io.FileOutputStream;
Import java.io.IOException; Copy picture public class Fileinputstreamdemo {public static void main (string[] args) throws IOException {File fil
        E = new File ("F:\\shar\\test\\logo17.gif"); FileInputStream fis = new FileInputStream (file); Create an input stream//create an output stream, followed by a parameter true to append, the original content will not be cleared, the default is false fileoutputstream fos = new FileOutputStream ("F:\\sha
        R\\test\\logo18.gif ", false);
        int ch = 0;
        The Way One/*while ((Ch=fis.read ())!=-1) {fos.write (CH);
        }*///Mode two/*byte[] b = new byte[1024];
        while ((Ch=fis.read (b))!=-1) {fos.write (b,0,ch);
        }*///Mode three byte[] b = new byte[fis.available ()]; Fis.read (b);
        First, the contents of the FIS are read into byte array b fos.write (b);//Then the contents of byte array B are written to the specified file//closed stream fos.close ();
    Fis.close ();}

} 

Use of ④ pipeline flow:
A PipedInputStream object must be connected to a PipedOutputStream object to produce a communication pipeline. Typically one thread writes data from the pipe output stream, and another thread reads the data from the pipeline input stream. When thread a executes the read () method for pipeline input, if there is no data at the moment, the thread is blocked and thread a resumes running only if thread B wants to write the data in the pipeline output stream.

Package com.iotest;
Import java.io.IOException;
Import Java.io.PipedInputStream;
Import Java.io.PipedOutputStream;
    * * Pipe Flow * * Class Sender extends thread{private pipedoutputstream out = new PipedOutputStream ();
    Public PipedOutputStream Getout () {return out;
        @Override public void Run () {String s = ' Hello World ';
            try {out.write (s.getbytes ());
        Out.close ();
    catch (Exception e) {//Todo:handle Exception}}} public class Receiver extends thread{
    Private PipedInputStream in;
    Public Receiver (Sender Sender) throws IOException {in = new PipedInputStream (Sender.getout ());
            @Override public void Run () {try {int data;
            while ((Data=in.read ())!=-1) {System.out.print ((char) data);
        } in.close (); catch (Exception e) {//Todo:handle Exception}} PubLic static void Main (string[] args) throws IOException {Sender Sender = new Sender ();
        Receiver r = new Receiver (sender);
        Sender.start ();
    R.start (); }

}

Use of ⑤ buffer streams:

Package com.iotest;
Import Java.io.BufferedInputStream;
Import Java.io.BufferedOutputStream;
Import Java.io.FileInputStream;
Import java.io.FileNotFoundException;
Import Java.io.FileOutputStream;

Import java.io.IOException;
    public class Testprime {private Bufferedinputstream bis = null;
    Private Bufferedoutputstream BOS = NULL;
    String fileName = "F:\\shar\\test\\test2.txt";
    static int s,p;
                Determine if it is prime number public boolean isprime (int n) {for (int i=2;i<=n/2;i++) {if (n%i = 0) {
            return false;
    } return true; } void Printprime (int m) throws ioexception{//byte flow buffer bos = new Bufferedoutputstream (New fileoutputs
        Tream (FileName));
        int j = 0;
                for (int i = 2; i < m i++) {if (IsPrime (i)) {j + +;
                    if (j%s = = 0) {String s = string.valueof (i) + "";
                    Bos.write (S.getbytes ()); BOs.write ("\ r \ n". GetBytes ());
                    }else{String s = string.valueof (i) + "";
                Bos.write (S.getbytes ());
        }} bos.flush ();
    Bos.close (); } void Getprime () throws ioexception{//bytes flow Buffer bis = new Bufferedinputstream (New FileInputStream (fil
        ename));
        int c = Bis.read ();
            while (c!=-1) {Char ch = (char) c;
            System.out.print (CH);
        c = Bis.read (); }/** * @param args * @throws ioexception/public static void main (string[] args) throws I
        oexception {testprime t = new testprime ();
        p = 100;
        s = 10;
        T.printprime (P);
    T.getprime (); }

}

If you do not use buffer flow, the program is to read a data, write a data. This greatly affects efficiency in programs with large amounts of data. Buffer flow is to write data to the buffer, and so the buffer is full, and then write the data to the file. So the efficiency is greatly improved.


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.