bookaccount.java
来自「《精通JAVA手机游戏与应用程序设计》随书光盘」· Java 代码 · 共 102 行
JAVA
102 行
/*
* 基本的记录字段分割和转换字节数组
*/
package PhoneBook;
import java.io.*;
public class BookAccount {
private String userName = "";
private String mobilePhone = "";
private String email = "";
private String phone = "";
public BookAccount(String userName, String mobilePhone, String email,
String phone) {
this.userName = userName;
this.mobilePhone = mobilePhone;
this.email = email;
this.phone = phone;
}
public BookAccount() {
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getMobilePhone() {
return mobilePhone;
}
public void setMobilePhone(String mobilePhone) {
this.mobilePhone = mobilePhone;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public byte[] serialize() throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(baos);
dos.writeUTF(userName);
dos.writeUTF(mobilePhone);
dos.writeUTF(email);
dos.writeUTF(phone);
baos.close();
dos.close();
return baos.toByteArray();
}
public static BookAccount deserialize(byte[] data) throws IOException {
ByteArrayInputStream bais = new ByteArrayInputStream(data);
DataInputStream dis = new DataInputStream(bais);
BookAccount account = new BookAccount();
account.userName = dis.readUTF();
account.mobilePhone = dis.readUTF();
account.email = dis.readUTF();
account.phone = dis.readUTF();
bais.close();
dis.close();
return account;
}
public static boolean matches(byte[] data, String userName)
throws IOException {
ByteArrayInputStream bais = new ByteArrayInputStream(data);
DataInputStream dis = new DataInputStream(bais);
try {
return (dis.readUTF()).equals(userName);
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?