Data flow of I/O streams in Java

Source: Internet
Author: User

Data flow in Java:

  For a question: Is there a way to write a long type of data to a file?

Go to string → write through GetBytes (), laborious, and in this process the number of long types need to be constantly converted.

Now, the data flow in Java can solve this problem very well (no need to convert, write directly in)

1. DataInputStream and DataOutputStream inherit from InputStream, OutputStream respectively, it belongs to the processing flow, need to be nested in InputStream, OutputStream type of node flow above .

2. DataInputStream and DataOutputStream provide methods that can access machine-independent Java primitive type data (such as int, double, etc.).

3. DataInputStream and DataOutputStream are constructed by:

DataInputStream (InputStream in)

DataOutputStream (Outputstram out)

"Note" UTF (Unicode string): Often used on the network; UTF-8 than UTF-16.

Demo_1:

Import Java.io.*;class Test {public static void main (string[] args) {Bytearrayoutputstream BAOs = new Bytearrayoutputstrea M (); First allocate a byte array on top of the memory, and a pipe is connected (above a piece of memory) dataoutputstream dos = new DataOutputStream (BAOs); BAOs pipe is not easy to use, the outside that again set up a pipe dostry {dos.writedouble (Math.random ());d Os.writeboolean (false); Bytearrayinputstream Bais = new Bytearrayinputstream (Baos.tobytearray ()); The reference System.out.println (Bais.available ()) about this array can be obtained by BAOs's Tobytearray (); How many bytes in the Bais are available for reading; DataInputStream dis = new DataInputStream (Bais); What is inside (string) when reading from a byte array? ... Not clear), it is necessary to set up a layer of pipe System.out.println (dis.readdouble ()); Write first read System.out.println (Dis.readboolean ());d os.close (), Baos.close ();d is.close (); Bais.close ();} catch (IOException e) {e.printstacktrace ();}}}

Operation Result:

9
0.1088353250890215
False

Data flow of I/O streams in Java

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.