Java中根基数据范例与流的操纵要领
副标题#e#
Java中除了二进制文件和利用文本文件外尚有基于Data的数据操纵,这里的Data指的是Java的根基数据范例和String。根基数据范例包罗byte、int、char、long、float、double、boolean和short。
说到Java的根基数据范例必需谈到的两个类是DataInputStream和DataOutputStream。它们提供了对Java根基数据范例的操纵,可是这些要领事实上是在两个重要的接口中界说的DataInput和DataOutput,它们的成果就是把二进制的字节约转换成Java的根基数据范例,同时还提供了从数据中利用UTF-8编码构建String的成果。有一个重要的类RandomAccessFile实现了DataInput和DataOutput两个接口使得他可以或许对文件同时举办写和读的操纵。
在DataInputStream和DataOutputStream两个类中的要领都很简朴,根基布局为readXXXX()和writeXXXX()个中XXXX代表根基数据范例可能String。在这里不多报告,不外值得一提的是我们有须要读读java中unicode的编码法则,在API doc中有较量具体的先容。凡是我们的工具有许多都是由java的根基数据范例组成的,好比一小我私家的信息包罗姓名,电子信箱,电话号码和性别等。其实我们可以用DataInputStream中的要领和DataOutputStream中的要领凭据必然的序列把数据写入流中再凭据沟通的序列把他们读取出来,这就是我们本身实现的序列化,这可以用在数据传输中,好比在J2ME联网措施中利用序列化机制传输数据。下面我们看看如何本身实现序列化,首先我们要有两个结构函数个中一个参数为空。
public Account()
{
}
public Account(String userName, String email, int age, boolean gender)
{
this.userName = userName;
this.email = email;
this.age = age;
this.gender = gender;
}
当我们举办序列化的时候也很简朴,我们只是往DataOutputStream中凭据顺序写入工具的成员变量。譬喻:
public void serialize(DataOutputStream dos) throws IOException
{
dos.writeUTF(userName);
dos.writeUTF(email);
dos.writeInt(age);
dos.writeBoolean(gender);
}
当我们举办反序列化的时候则凭据沟通的顺序从DataInputStream内里读取数据并赋值给成员变量。譬喻:
public static Account deserialize(DataInputStream dis) throws IOException
{
Account account = new Account();
account.userName = dis.readUTF();
account.email = dis.readUTF();
account.age = dis.readInt();
account.gender = dis.readBoolean();
return account;
}
为了便于调试我们还提供一个toString()的要领打印出工具的实际信息。这是个好的习惯。
public String toString()
{
return "UserName = " + userName + " Email = " + email + " age = " + age + " gender = " + (gender ? "male" : "female");
}
#p#副标题#e#
为了测试序列化我们编写下面的措施举办测试,代码较量简朴。
package com.j2medev.mingjava;
import java.io.*;
public class TestDataIO
{
public static void main(String[] args) throws IOException
{
Account account = new Account("mingjava","[email protected]",25,true);
System.out.println("before serialization.........");
System.out.println(account.toString());
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(baos);
account.serialize(dos);
DataInputStream dis = new DataInputStream(new ByteArrayInputStream(baos.toByteArray()));
Account sAccount = Account.deserialize(dis);
System.out.println("after serialization..........");
System.out.println(sAccount.toString());
dos.close();
dis.close();
}
}
package com.j2medev.mingjava;
import java.io.*;
public class Account
{
private String userName = "";
private String email = "";
private int age = 0;
private boolean gender = false;
public Account()
{}
public Account(String userName, String email, int age, boolean gender)
{
this.userName = userName;
this.email = email;
this.age = age;
this.gender = gender;
}
public void serialize(DataOutputStream dos) throws IOException
{
dos.writeUTF(userName);
dos.writeUTF(email);
dos.writeInt(age);
dos.writeBoolean(gender);
}
public static Account deserialize(DataInputStream dis) throws IOException
{
Account account = new Account();
account.userName = dis.readUTF();
account.email = dis.readUTF();
account.age = dis.readInt();
account.gender = dis.readBoolean();
return account;
}
public String toString()
{
return "UserName = " + userName + " Email = " + email + " age = " + age + " gender = " + (gender ? "male" : "female");
}
}
编译运行措施在节制台输出:
#p#分页标题#e#
before serialization.........
UserName = mingjava Email = [email protected] age = 25 gender = male
after serialization..........
UserName = mingjava Email = [email protected] age = 25 gender = male
序列化乐成,后头我将报告如安在J2ME联网中利用序列化机制。