Usage of datainputstream and dataoutputstream

Source: Internet
Author: User

Usage and precautions of datainputstream and dataoutputstream

Import Java. io. *; </P> <p> public class iotest {<br/> Public static void main (string [] ARGs) throws exception {<br/> fileinputstream in = new fileinputstream ("E:/io.txt"); <br/> bufferedinputstream bin = new bufferedinputstream (in ); <br/> datainputstream din = new datainputstream (BIN); </P> <p> fileoutputstream out = new fileoutputstream ("E:/io.txt "); <br/> bufferedoutputstream bout = new bufferedoutputstream (out); <br/> dataoutputstream dout = new dataoutputstream (bout); </P> <p> dout. writebyte (-12); <br/> dout. writelong (12); <br/> dout. writechar ('1'); <br/> dout. writefloat (1.01f); <br/> dout. writeutf ("good"); <br/> dout. close (); </P> <p> system. out. println (din. readbyte (); <br/> system. out. println (din. readlong (); <br/> system. out. println (din. readchar (); <br/> system. out. println (din. readfloat (); <br/> string s; <br/> S = din. readutf (); <br/> system. out. println (s); </P> <p> din. close (); <br/>}< br/>}

 

If you directly use datainputstream to read the file content, an exception is thrown:

 

 

At this time, even the manual income data cannot be read, and there is no content in the file.

 

Solution: you must first use dataoutputstream to write data, and then use datainputstream to read data.

 

Executed successfully:

 

 

Note: datainputstream and dataoutputstream must appear in pairs. In addition, datainputstream and dataoutputstream can be directly decorated with fileinputstream and fileoutputstream.

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.