Data input and output streams in Java-DataInputStream and DataOutputStream

Source: Internet
Author: User

1. Basic Concepts DataOutputStream data output stream allows applications to write Basic Java data types to the output stream in an appropriate way. Then the application can use the data input stream to read data. The DataOutputStream data input stream allows applications to read Basic Java data types from the underlying input stream in a machine-independent manner. Applications can use the data output stream to write data that is later read by the data input stream. Multi-threaded access is not necessarily safe. Thread security is optional and is the responsibility of the user of such methods. 2. For the example [java]/***, you must first use DataOutputStream to write data and then use DataInputStream to read data. ** @ Author Xu Yue **/public class TestClass {public static void main (String [] args) throws Exception {TestClass t = new TestClass (); t. write (); t. read ();} public void write () throws Exception {String path = this. getClass (). getClassLoader (). getResource ("test.txt "). toURI (). getPath (); OutputStream OS = new FileOutputStream (path); DataOutputStream dos = new DataOutputStream (OS); dos. writeDouble (Mat H. random (); dos. writeBoolean (true); dos. writeInt (1, 1000); dos. writeInt (1, 2000); dos. flush (); OS. close (); dos. close ();} public void read () throws Exception {InputStream instream = this. getClass (). getClassLoader (). getResourceAsStream ("test.txt"); DataInputStream dis = new DataInputStream (instream); double d = dis. readDouble (); boolean B = dis. readBoolean (); // first read the int i1 = dis. readInt (); int i2 = Dis. readInt (); instream. close (); dis. close (); System. out. println (d); System. out. println (B); System. out. println (i1); System. out. println (i2) ;}}/*** must first use DataOutputStream to write data and then use DataInputStream to read data. ** @ Author Xu Yue **/public class TestClass {public static void main (String [] args) throws Exception {TestClass t = new TestClass (); t. write (); t. read ();} public void write () throws Exception {String path = this. getClass (). getClassLoader (). getResource ("test.txt "). toURI (). getPath (); OutputStream OS = new FileOutputStream (path); DataOutputStream dos = new DataOutputStream (OS); dos. writeDouble (Math. random (); dos. writeBoolean (true); dos. writeInt (1, 1000); dos. writeInt (1, 2000); dos. flush (); OS. close (); dos. close ();} public void read () throws Exception {InputStream instream = this. getClass (). getClassLoader (). getResourceAsStream ("test.txt"); DataInputStream dis = new DataInputStream (instream); double d = dis. readDouble (); boolean B = dis. readBoolean (); // first read the int i1 = dis. readInt (); int i2 = dis. readInt (); instream. close (); dis. close (); System. out. println (d); System. out. println (B); System. out. println (i1); System. out. println (i2) ;}} output result 0.4683894257027681true10002000

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.