Java Learning Lesson 57th-io Stream (11)

Source: Internet
Author: User

First, the pipeline flow

PipedInputStream and PipedOutputStream

Inputs and outputs can be connected directly, combined with thread usage

Pipe flow, as the name implies, write a read one, connected into a pipe

API Documentation: The pipeline input stream should be connected to the pipeline output flow, and the pipeline input stream provides all the data bytes to write to the pipeline output flow. Typically, data is read from an object by a thread PipedInputStream and written by another thread to the corresponding PipedOutputStream . It is not recommended that you try to use a single thread for both objects, because this could be a deadlock thread. The pipeline inflow contains a buffer that can be separated from the read and write operations within the bounds of the buffer. If the thread that provides data bytes to the connection pipeline output stream no longer exists, the pipeline is considered corrupted

Class Input implements Runnable{private PipedInputStream Pis;input (PipedInputStream pis) {this.pis = PiS;} @Overridepublic void Run () {try {byte[] by = new Byte[1024];int len = Pis.read (by); String str = new string (By,0,len); System.out.println ("str =" +str);p is.close ();} catch (Exception e) {//Todo:handle Exception}}}class Output implements Runnable{private PipedOutputStream Pos; Output (PipedOutputStream POS) {//TODO auto-generated constructor stubthis.pos = pos;} @Overridepublic void Run () {//TODO auto-generated method stubtry {thread.sleep;p os.write ("Hello". GetBytes ());// String-To-byte throttle} catch (Exception e) {//Todo:handle Exception}}}public class Main {public static void main (string[] args) throws Ioexception{pipedinputstream pis = new PipedInputStream (); PipedOutputStream pos = new PipedOutputStream ();p is.connect (POS);//two stream docking thread t1 = new Thread (new Input (PiS)); Thread t2 = new Thread (new Output (POS)); T1.start (); T2.start ();}}

Pipeline Flow application is not many, but its own characteristics

Other classes in the IO package

Flow object-datainputstream and DataOutputStream for manipulating basic type data

Manipulating byte arrays-bytearrayinputstream and Bytearrayoutputstream

Manipulating character arrays-chararrayinputstream and Chararrayoutoutstream

Manipulating Strings-stringwriter and StringReader


DataInputStream and DataOutputStream

public static void Read () throws IOException {//TODO auto-generated method Stubdatainputstream dis = new DataInputStream ( New FileInputStream ("Data.txt")); String str = DIS.READUTF (); System.out.println (str);d is.close ();} public static void Datastreamdemo () throws IOException {//TODO auto-generated method Stubdataoutputstream dos = new Datao Utputstream (New FileOutputStream ("Data.txt"));d Os.writeutf ("Hello fat"); use <a Target=_blank href= "in a machine-independent way" >utf-8 Modified version </a> encoding writes a string to the underlying output stream. Dos.close ();}
The rest of the way is similar to OutputStream and InputStream.


Manipulating the flow of an array


both the source and sink are memory . Bytearrayinputstream, Bytearrayoutputstream

Bytearrayoutputstream: This class implements an output stream in which the data is written to a byte array. The buffer grows automatically as data is written continuously. You can use toByteArray() and toString() get data, and closing bytearrayoutputstream is not valid.

public static void Bytestreamdemo () throws IOException {Bytearrayinputstream bis = new Bytearrayinputstream ("ASDFD". GetBytes ()); Bytearrayoutputstream BOS = new Bytearrayoutputstream (), int len = 0;while (len = Bis.read ())!=-1) {bos.write (len);} System.out.println (Bos.tostring ());}

The remaining stream usage and Bytearrayinputstream are the same

Coding issues

The coding problem is nothing more than the conversion between the text and the binary.

Character array, string: encoding

Character array-string: decoding

public static void Demo () {String str = "Hello";//code byte[] by = Str.getbytes (); for (byte b:by) {System.out.println (b);} Decode String str1 = new string (by);/= = new String (by, "GBK"); System.out.println (STR1);}

Encoding is right, it is possible to successfully decode the string in Java, the string "ABCD" and "ab Hello" length, the same as 4 characters, but the number of bytes is not the same as the definition method, according to the maximum number of bytes to take bytes such as: "AB Hello" for example, If you follow 3 bytes to fetch the text, take "AB" and half you, half discard, 4 bytes take "Ab You", 5 bytes or "AB You"
public static void Main (string[] args) throws ioexception{string str = "AB Hello Asdas Satan launched"; int len = Str.getbytes ("GBK"). Leng th;for (int i = 0;i<len;i++) {//According to I bytes take System.out.println ("OK:" +demo (Str,i+1));}} public static string Demo (String Str,int len) throws IOException {byte[] by = str.getbytes ("GBK"); int count = 0;//record from which index to open Beginning, byte is not kanji for (int i = len-1;i>=0;i--) {if (by[i]<0) {count++;} else {break;}} Chinese characters correspond to 2 bytes, number of Chinese characters = negative byte number/2return (Count & 1) = = 0?new string (by,0,len): New string (by,0,len-1);




Java Learning lesson 57th-io Stream (11)

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.