Java read-write binary file operations

Source: Internet
Author: User

/** *  binary read/write file  */import java.io.BufferedInputStream;import  Java.io.bufferedoutputstream;import java.io.datainputstream;import java.io.dataoutputstream;import  java.io.fileinputstream;import java.io.fileoutputstream;public class mainclass{/** * The OutputStream and its subclasses in the  java.io package are dedicated to writing binary data.  * fileoutputstream is its subclass, which can be used to write binary data to a file.  * dataoutputstream is another subclass of OutputStream, which can be  *  connected to a fileoutputstream for easy writing of data of various basic data types.  */public void writemethod1 () {string filename= "C:/kuka1.dat";int value0=255;int  value1=0;int value2=-1;try{//Connect DataOutputStream with FileOutputStream to output different types of data// The constructor of the FileOutputStream class is responsible for opening the file Kuka.dat, if the file does not exist,//creates a new file and replaces it with the newly created file if it already exists. Then the object of the fileoutputstream//class is connected to a DataOutputStream object, and the DataOutputStream class has methods for writing//various data types. Dataoutputstream out=new dataoutputstream (New fileoutputstream (FileName)); Out.writeInt (VALUE0) ; Out.writeint (value1); Out.writeint (value2); out.clOSE ();}  catch  (exception e) {e.printstacktrace ();}} For writing large amounts of data, using the buffered stream Bufferedoutputstream class can improve efficiency public void writemethod2 () {string filename= "c:/ Kuka2.txt "; Try{dataoutputstream out=new dataoutputstream ( new bufferedoutputstream (  New fileoutputstream (FileName)); Out.writeint (10); System.out.println (out.size () + " bytes have been written."); O Ut.writedouble (31.2); System.out.println (out.size () + " bytes have been written."); O Ut.writebytes ("JAVA"); System.out.println (out.size () + " bytes have been written."); O Ut.close ();}  catch  (exception e) {e.printstacktrace ();}} /** *  common classes for binary files are fileinputstream,datainputstream * bufferedinputstream and so on. Similar to dataoutputstream,datainputstream *  also provides a number of methods for reading Boolean, Byte, character, shape, long, short,  *  single-precision, double-precision and other data.  */public void readmethod1 () {string filename= "C:/kuka1.dat"; int sum=0;try{ DatainputstreaM in=new datainputstream (   new bufferedinputstream (   new  FileInputStream (FileName)); Sum+=in.readint (); Sum+=in.readint (); Sum+=in.readint (); System.out.println ("The sum is:" +sum); In.close ();}  catch  (exception e) {e.printstacktrace ();}} Public void readmethod2 () {Try{fileinputstream stream=new fileinputstream ("C:/kuka.dat"); Int c;while ((C=stream.read ())!=-1) {System.out.println (c);}}  catch  (exception e) {e.printstacktrace ();}}}

Java read and write binary file operations

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.