Java IO learning notes (3) Conversion stream, data stream, byte array stream, and io learning notes

Source: Internet
Author: User

Java IO learning notes (3) Conversion stream, data stream, byte array stream, and io learning notes

Conversion stream

1. Conversion stream: Convert byte streams to a bytes stream. After conversion, you can write content to the program with one character and one character. You can also call the write (String s) method of the character node stream, you can also apply BufferedReader () and BufferedWriter outside and use their readLine and newLine methods. 2. There are two types of conversion streams: InputStreamReader and OutputStreamWriter exercise applet 1: PackageTest. io. transfer; ImportJava. io. FileOutputStream; ImportJava. io. IOException; ImportJava. io. OutputStreamWriter; Public ClassOutputStreamWriterTest { Public Static VoidMain (String args []) {FileOutputStream fos = Null; FileOutputStream fos1 = Null; OutputStreamWriter opsw = Null; Try{Fos = NewFileOutputStream ("E:/technology learning/java/test/Socket/test4.txt"); fos1 = NewFileOutputStream ("E:/technology learning/java/test/Socket/test5.txt"); opsw = NewOutputStreamWriter (fos); opsw. write ("conversion stream exercise"); System. Out. Println (opsw. getEncoding (); // obtain the encoding format opsw. flush (); opsw. close (); opsw = NewOutputStreamWriter (fos1, "GBK"); opsw. write ("conversion stream exercise"); System. Out. Println (opsw. getEncoding (); // obtain the encoding format opsw. flush ();} Catch(IOException e) {e. printStackTrace ();} Finally{ Try{Fos. close (); opsw. close ();} Catch(IOException e) {e. printStackTrace () ;}}} exercise applet 2: PackageTest. io. transfer; ImportJava. io. BufferedReader; ImportJava. io. IOException; ImportJava. io. InputStreamReader; Public ClassInputStreamReaderTest { Public Static VoidMain (String args []) {InputStreamReader isr = NewInputStreamReader (System. In);// System. in is a subclass of inputstream.BufferedReader bf = NewBufferedReader (isr); String ss = Null; Try{ While(Ss = bf. readLine ())! = Null){ If(Ss. equals ("exit ")){ Break;} System. Out. Println (ss );}} Catch(IOException e) {e. printStackTrace ();} Finally{ Try{Isr. close (); bf. close ();} Catch(IOException e) {e. printStackTrace ();}}}} Data Stream, byte array stream1. Data Stream: Generally, byte streams can only read and write data of the byte [] type, and can only write data of the int, byte, and byte [] type; the character input stream can only read data of the char [] type, and the character output stream can only write data of the char, char [], int, and String types. If you want to read and write data of other types, such as long, double, and boolean, data streams are required. 2. A data stream is a processing stream. There are two types of data streams: DataInputStream and DataOutputStream, which are inherited from InputStream and OutputStream respectively. They need to perform operations on InputStream and OutputStream byte streams, and extract some data. For details, see the help documentation. DataInputStream:

 

DataOutputStream:

 

DataInputStream constructor: DataInputStream (InputStream in) DataOutputStream constructor: DataOutputStream (OutputStream out) 3. byte array stream (byte stream): ByteArrayInputStream: the byte array input stream creates a byte array buffer in the memory. The data read from the input stream is stored in the byte array buffer and read from the byte array. ByteArrayOutputStream: When a byte array output stream is created, the Memory creates a default byte array for the output stream to store written bytes. 4. ByteArrayInputStream (byte [] buf); ByteArrayInputStream (byte [] buf, int offset, int length); ByteArrayOutputStream (); when a byte output stream is created, a byte [] byte array is created in the memory. The default size is. ByteArrayOutputStream (int size); When a byte output stream is created, a byte [] byte array of size is created in the memory. You can use byte [] B = byteArrayOutputStream. toByteArray () returns the byte array in the stream, and then creates ByteArrayInputStream bai = new ByteArrayInputStream (byte [] B) to connect the input stream to the byte array. Example: PackageTest. io. data; ImportJava. io. ByteArrayInputStream; ImportJava. io. ByteArrayOutputStream; ImportJava. io. DataInputStream; ImportJava. io. DataOutputStream; ImportJava. io. IOException; Public ClassDataByteArrayStreamTest { Public Static VoidMain (String args []) {ByteArrayOutputStream baos = NewByteArrayOutputStream (); DataOutputStream dos = NewDataOutputStream (baos); ByteArrayInputStream bais = Null; DataInputStream dis = Null; Try{Dos. writeDouble (Math. Random(); Dos. writeBoolean ( True); Dos. flush (); bais = NewByteArrayInputStream (baos. toByteArray (); dis = NewDataInputStream (bais); System. Out. Println (dis. available (); // first-in-first-out, first-in-first-read. If this is the first-read Boolean, the first byte of the double data is automatically found, read System as a boolean type. Out. Println (dis. readDouble (); System. Out. Println (dis. readBoolean ());} Catch(IOException e) {e. printStackTrace ();} Finally{ Try{ If(Baos! = Null) {Baos. close ();} If(Dos! = Null) {Dos. close ();} If(Bais! = Null) {Bais. close ();} If(Dis! = Null) {Dis. close ();}} Catch(IOException e) {e. printStackTrace () ;}}} console output: 90.02241410199042526 true

 

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.