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.