How to read and write binary files in java

Source: Internet
Author: User

Interface: Writerable
Copy codeThe Code is as follows: package com. geoway. pad. common;

Import java. io. DataInput;
Import java. io. DataOutput;
Import java. io. IOException;

Public interface Writerable {
// Write
Public void write (DataOutput data) throws IOException;
// Read
Public void read (DataInput di) throws IOException;
}

Interface implementation class: UserWriter
Copy codeThe Code is as follows: package com. geoway. pad. common;

Import java. io. ByteArrayInputStream;
Import java. io. ByteArrayOutputStream;
Import java. io. DataInput;
Import java. io. DataInputStream;
Import java. io. DataOutput;
Import java. io. DataOutputStream;
Import java. io. FileInputStream;
Import java. io. FileNotFoundException;
Import java. io. FileOutputStream;
Import java. io. IOException;
Import java. io. InputStream;
Import java. io. OutputStream;
Import java. util. UUID;

Public class UserWriter implements Writerable {
// Getter setter methods
Public int getNumber (){
Return number;
}
Public void setNumber (int number ){
This. number = number;
}
Public String getId (){
Return id;
}
Public void setId (String id ){
This. id = id;
}
Public int getAge (){
Return age;
}
Public void setAge (int age ){
This. age = age;
}
Public boolean isAdministrator (){
Return administrator;
}
Public void setAdministrator (boolean administrator ){
This. administrator = administrator;
}
Public String getUserName (){
Return userName;
}
Public void setUserName (String userName ){
This. userName = userName;
}
Public void setPassword (String password ){
This. password = password;
}
Public String getPassword (){
Return password;
}
Public void setText (String text ){
This. text = text;
}
Public String getText (){
Return text;
}
Public void setImage (byte [] image ){
This. image = image;
}
Public byte [] getImage (){
Return image;
};
// Private fields
Private int number;
Private String id;
Private int age;
Private boolean administrator;
Private String userName;
Private String password;
Private String text;
Private byte [] image;

@ Override
Public void write (DataOutput data) throws IOException {
Data. writeInt (number );
Data. writeUTF (id );
Data. writeInt (age );
Data. writeBoolean (administrator );
Data. writeUTF (userName );
Data. writeUTF (password );
Data. writeUTF (text );
Data. write (image );

}
@ Override
Public void read (DataInput di) throws IOException {
This. number = di. readInt ();
This. id = di. readUTF ();
This. age = di. readInt ();
This. administrator = di. readBoolean ();
This. userName = di. readUTF ();
This. password = di. readUTF ();
This. text = di. readUTF ();
// Read the image
This. image = new byte [1, 2048];
Try {
Di. readFully (this. image );
} Catch (Exception e ){
System. out. println (e. toString ());
}
}
Public static byte [] in2byte (InputStream is) throws IOException {
Byte [] bs = new byte [1024];
ByteArrayOutputStream bos = new ByteArrayOutputStream ();
Int len =-1;
While (len = is. read (bs ))! =-1 ){
Bos. write (bs, 0, len );
}
Bs = bos. toByteArray ();
Return bs;
}
Public static void copy (InputStream in, OutputStream out) throws IOException {
Byte [] buf = new byte [1, 1024];
While (true ){
Int len = in. read (buf );
If (len <0) break;
Out. write (buf, 0, len );
}
}
Public static void main (String [] args) throws FileNotFoundException, IOException {
UserWriter user = new UserWriter ();
User. setUserName ("likehua ");
User. setPassword ("password ");
User. setId (UUID. randomUUID (). toString ());
User. setNumber (123 );
User. setAdministrator (false );
// Add a text clip
User. setText ("to make a weather forecast, you can save the name of the city to be displayed on the Add city page. My key operations on rms are as follows (SetCanvas. cityName is a Vector used to save the city name. Before executing this code segment, I have added the newly added city name to this Vector ");
// Input an image
User. setImage (in2byte (new FileInputStream ("c: \ 1.jpg ")));
User. write (new DataOutputStream (new FileOutputStream ("c: \ user. data ")));
System. out. println ("finished ");
System. out. println ("... start reading ...");
UserWriter u = new UserWriter ();
U. read (new DataInputStream (new FileInputStream ("c: \ user. data ")));
System. out. println (u. getUserName ());
System. out. println (u. getPassword ());
System. out. println (u. isAdministrator ());
System. out. println (u. getText ());
// Take out the image
Copy (new ByteArrayInputStream (u. getImage (), new FileOutputStream ("c: \ copy.jpg "));

}

}

This example is only written when I test the DataOutput and DataInput interfaces. It is not used in the project, and I do not know if there is any problem with using it in the project.

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.