Interface: writerable
Copy Code code 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 Code code 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 ();
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, you 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 name of the city, and I have added the new city name to this 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 ("finished");
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 only my test dataoutput and Datainput interface written, and not used in the project, also do not know in the project this use will have any problems.