Usage Details of datainputstream and dataoutputstream

Source: Internet
Author: User
Both datainputstream and dataoutputstream are decorative classes for input and output streams in Java, which are easy to use. Today we will discuss the encoding problems encountered when using this class. Zookeeper

Package COM. vincent. example; import Java. io. datainputstream; import Java. io. dataoutputstream; import Java. io. fileinputstream; import Java. io. fileoutputstream; import Java. io. ioexception; public class testdatastream {public static void main (string [] ARGs) {// use datainputstream and dataoutputstream to write and read data from the file. Try {// write data stream to the input stream dataoutputstream dos = new dataoutputstream (New fileoutputstream ("datasteam.txt"); dos. writebytes ("world"); // writes data in 2 bytes, all of which are low-level dos writes. writechars ("world"); // write by Unicode // write by UTF-8 (utf8 gets longer, the first 2 bytes are the length information written by the writeutf function, easy to read by the readutf function) dos. writeutf ("world"); dos. flush (); dos. close (); // data stream reads datainputstream Dis = new datainputstream (New fileinputstream ("datasteam.txt"); // reads byte [] B = new byte [2]; DIS. read (B); system. out. println (new string (B, 0, 2); // read the char [] C = new char [2]; for (INT I = 0; I <2; I ++) {C [I] = dis. readchar ();} system. out. println (new string (C, 0, 2); // read utfsystem. out. println (DIS. readutf (); DIS. close ();} catch (ioexception e) {e. printstacktrace ();}}}

Open the file in hexadecimal mode as follows:

It can be seen that the first "world" has been cut off, and both Chinese characters have only been written to a low level, so it must be garbled.

Package COM. vincent. example; import Java. io. datainputstream; import Java. io. dataoutputstream; import Java. io. fileinputstream; import Java. io. fileoutputstream; import Java. io. ioexception; public class testdatastream {public static void main (string [] ARGs) {// use datainputstream and dataoutputstream to write and read data from the file. Try {// write data stream to the input stream dataoutputstream dos = new dataoutputstream (New fileoutputstream ("datasteam.txt"); dos. write ("world ". getbytes (); // write data by UTF-8 encoding (my system's default encoding method) // dos. write ("world ". getbytes ("GBK"); // specify other encoding methods for DOS. writechars ("world"); // write by Unicode // write by UTF-8) DOS. writeutf ("world"); dos. flush (); dos. close (); // data stream reads datainputstream Dis = new datainputstream (New fileinputstream ("datasteam.txt"); // reads byte [] B = new byte [6]; DIS. read (B); system. out. println (new string (B, 0, 6); // read the char [] C = new char [2]; for (INT I = 0; I <2; I ++) {C [I] = dis. readchar ();} system. out. println (new string (C, 0, 2); // read utfsystem. out. println (DIS. readutf (); DIS. close ();} catch (ioexception e) {e. printstacktrace ();}}}

Open the file as follows:


It can be seen that reading the first 6 bytes and decoding according to the UTF-8 can avoid garbled. It is worth noting that the string constructor can also specify other encoding methods for the byte array. For example, new string (B, "GBK "). Using writeutf and readutf at the same time can save the programmer's consideration of length, because these two functions solve the length problem for us and write the file, so we don't need to worry about it. However, additional space is used to record length information.

Zookeeper

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.