Java reads/writes binary files

Source: Internet
Author: User

Import java. io. dataInputStream; import java. io. dataOutputStream; import java. io. file; import java. io. fileInputStream; import java. io. fileOutputStream; import java. io. IOException;/* @ cow.a.com (Shenzhen) Information Technology Co., Ltd. * @ author steven. wu * @ since 2009.8.11 * @ description read and write stream for binary file. */Public class BinaryReadWrite {private DataInputStream dis = null; private DataOutputStream dos = null; private String s_FilePath = "d:/bin. dat "; private byte [] m_datapadding = {0x00}; // fill in the blank to fill in the number of bytes. public BinaryReadWrite () {// TODO Auto-generated constructor stub init ();} private void init () {try {if (! New File (s_FilePath ). exists () {new File (s_FilePath ). createNewFile ();} dis = new DataInputStream (new FileInputStream (new File (s_FilePath); dos = new DataOutputStream (new FileOutputStream (new File (s_FilePath )));} catch (Exception e) {e. printStackTrace () ;}} public void writeBinaryStream () {try {if (dos! = Null) {for (int I = 0; I <2; I ++) {// write boolean value. dos. writeBoolean (true); // write a char value. char c = 'a'; dos. writeChar (int) c); Double d = 12.567d; dos. writeDouble (d); Float f = 56.782f; dos. writeFloat (f); int k = 105; dos. writeInt (k); long l = 98765l; dos. writeLong (l); short st = 12; dos. writeShort (st); String cs = "Java binary file read/write"; String cs1 = "binary file"; if (I = 0) {dos. writeUTF (cs);} else {dos. writeUTF (cs1 );} Dos. write (m_datapadding);} dos. flush (); dos. close () ;}} catch (Exception e) {e. printStackTrace () ;}} public void readBinaryStream () {try {www.2cto.com if (dis! = Null) {while (dis. available ()> 0) {System. out. println (dis. available (); System. out. println (dis. readBoolean (); char c = (char) dis. readChar (); System. out. println (c); System. out. println (dis. readDouble (); System. out. println (dis. readFloat (); System. out. println (dis. readInt (); System. out. println (dis. readLong (); System. out. println (dis. readShort (); System. out. println (dis. readUTF (); System. out. println (di S. read (m_datapadding) ;}} catch (Exception e) {e. printStackTrace () ;}} public static void main (String [] args) throws IOException {BinaryReadWrite bin = new BinaryReadWrite (); bin. writeBinaryStream (); bin. readBinaryStream () ;}} import java. io. dataInputStream; import java. io. dataOutputStream; import java. io. file; import java. io. fileInputStream; import java. io. fileOutputStream; import java. io. IOEx Ception;/* @ coprocessor A (Shenzhen) Information Technology Co., Ltd. * @ author steven. wu * @ since 2009.8.11 * @ description read and write stream for binary file. */Public class BinaryReadWrite {private DataInputStream dis = null; private DataOutputStream dos = null; private String s_FilePath = "d:/bin. dat "; private byte [] m_datapadding = {0x00}; // fill in the blank to fill in the number of bytes. public BinaryReadWrite () {// TODO Auto-generated constructor stub init ();} private void init () {try {if (! New File (s_FilePath ). exists () {new File (s_FilePath ). createNewFile ();} dis = new DataInputStream (new FileInputStream (new File (s_FilePath); dos = new DataOutputStream (new FileOutputStream (new File (s_FilePath )));} catch (Exception e) {e. printStackTrace () ;}} public void writeBinaryStream () {try {if (dos! = Null) {for (int I = 0; I <2; I ++) {// write boolean value. dos. writeBoolean (true); // write a char value. char c = 'a'; dos. writeChar (int) c); Double d = 12.567d; dos. writeDouble (d); Float f = 56.782f; dos. writeFloat (f); int k = 105; dos. writeInt (k); long l = 98765l; dos. writeLong (l); short st = 12; dos. writeShort (st); String cs = "Java binary file read/write"; String cs1 = "binary file"; if (I = 0) {dos. writeUTF (cs);} else {dos. writeUTF (cs1 );} Dos. write (m_datapadding);} dos. flush (); dos. close () ;}} catch (Exception e) {e. printStackTrace () ;}} public void readBinaryStream () {try {if (dis! = Null) {while (dis. available ()> 0) {System. out. println (dis. available (); System. out. println (dis. readBoolean (); char c = (char) dis. readChar (); System. out. println (c); System. out. println (dis. readDouble (); System. out. println (dis. readFloat (); System. out. println (dis. readInt (); System. out. println (dis. readLong (); System. out. println (dis. readShort (); System. out. println (dis. readUTF (); System. out. println (dis. read (m_datapadding) ;}} catch (Exception e) {e. printStackTrace () ;}} public static void main (String [] args) throws IOException {BinaryReadWrite bin = new BinaryReadWrite (); bin. writeBinaryStream (); bin. readBinaryStream ();}}

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.