Workaround for "Go" Java read-write binary files

Source: Internet
Author: User
Tags gettext uuid

Original URL: http://www.jb51.net/article/36940.htm

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.) {
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 ();
Reading pictures
This.image=new byte[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[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 paragraph of text
User.settext ("Make a weather forecast that can save the city name to show in the Add City page, My RMS key operations are as follows (where Setcanvas.cityname is a vector that holds the city name, and I have added the new added city name to the vector before executing this code);
Pass in a picture
User.setimage (In2byte (New FileInputStream ("c:\\1.jpg"));
User.write (New DataOutputStream (New FileOutputStream ("C:\\user.data"));
System.out.println ("complete");
System.out.println (".... Read start ... ");
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 picture
Copy (New Bytearrayinputstream (U.getimage ()), New FileOutputStream ("c:\\copy.jpg");

}

}


This example is just I test dataoutput and Datainput interface when written, and is not used in the project, do not know in the project such use will not have any problems.

Workaround for "Go" Java read-write binary files

Related Article

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.