国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Java > 正文

Java中基本數(shù)據(jù)類型與流

2020-03-24 19:22:23
字體:
供稿:網(wǎng)友
Java中除了二進(jìn)制文件和使用文本文件外還有基于Data的數(shù)據(jù)操作,這里的Data指的是Java的基本數(shù)據(jù)類型和String。基本數(shù)據(jù)類型包括byte、int、char、long、float、double、boolean和short。

說到Java的基本數(shù)據(jù)類型必須談到的兩個(gè)類是DataInputStream和DataOutputStream。它們提供了對Java基本數(shù)據(jù)類型的操作,但是這些方法事實(shí)上是在兩個(gè)重要的接口中定義的DataInput和DataOutput,它們的功能就是把二進(jìn)制的字節(jié)流轉(zhuǎn)換成Java的基本數(shù)據(jù)類型,同時(shí)還提供了從數(shù)據(jù)中使用UTF-8編碼構(gòu)建String的功能。有一個(gè)重要的類RandomAccessFile實(shí)現(xiàn)了DataInput和DataOutput兩個(gè)接口使得他能夠?qū)ξ募瑫r(shí)進(jìn)行寫和讀的操作。

在DataInputStream和DataOutputStream兩個(gè)類中的方法都很簡單,基本結(jié)構(gòu)為readXXXX()和writeXXXX()其中XXXX代表基本數(shù)據(jù)類型或者String。在這里不多講述,不過值得一提的是我們有必要讀讀java中unicode的編碼規(guī)則,在API doc中有比較詳細(xì)的介紹。通常我們的對象有很多都是由java的基本數(shù)據(jù)類型構(gòu)成的,比如一個(gè)人的信息包括姓名,電子信箱,電話號碼和性別等。其實(shí)我們可以用DataInputStream中的方法和DataOutputStream中的方法按照一定的序列把數(shù)據(jù)寫入流中再按照相同的序列把他們讀取出來,這就是我們自己實(shí)現(xiàn)的序列化,這可以用在數(shù)據(jù)傳輸中,比如在J2ME聯(lián)網(wǎng)程序中使用序列化機(jī)制傳輸數(shù)據(jù)。下面我們看看如何自己實(shí)現(xiàn)序列化,首先我們要有兩個(gè)html' target='_blank'>構(gòu)造函數(shù)其中一個(gè)參數(shù)為空。

public Account()
{

}

public Account(String userName, String email, int age, boolean gender)
{
this.userName = userName;
this.email = email;
this.age = age;
this.gender = gender;
}

當(dāng)我們進(jìn)行序列化的時(shí)候也很簡單,我們只是往DataOutputStream中按照順序?qū)懭雽ο蟮某蓡T變量。例如:

public void serialize(DataOutputStream dos) throws IOException
{
dos.writeUTF(userName);
dos.writeUTF(email);
dos.writeInt(age);
dos.writeBoolean(gender);
}

當(dāng)我們進(jìn)行反序列化的時(shí)候則按照相同的順序從DataInputStream里面讀取數(shù)據(jù)并賦值給成員變量。例如:

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;
}

為了便于調(diào)試我們還提供一個(gè)toString()的方法打印出對象的實(shí)際信息。這是個(gè)好的習(xí)慣。

public String toString()
{
return "UserName = " + userName + " Email = " + email + " age = " + age + " gender = " + (gender ? "male" : "female");
}

為了測試序列化我們編寫下面的程序進(jìn)行測試,代碼比較簡單。

package com.j2medev.mingjava;
import java.io.*;

public class TestDataIO
{
public static void main(String[] args) throws IOException
{
Account account = new Account("mingjava","eric.zhan@263.net",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");
}
}

編譯運(yùn)行程序在控制臺輸出:

before serialization.........
UserName = mingjava Email = eric.zhan@263.net age = 25 gender = male
after serialization..........
UserName = mingjava Email = eric.zhan@263.net age = 25 gender = male

序列化成功,后面我將講述如何在J2ME聯(lián)網(wǎng)中使用序列化機(jī)制。html教程

鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請第一時(shí)間聯(lián)系我們修改或刪除,多謝。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 天峨县| 鄱阳县| 财经| 和平区| 元江| 无棣县| 利川市| 湖州市| 香港| 县级市| 中方县| 门源| 绥江县| 定西市| 油尖旺区| 巴彦县| 田东县| 沐川县| 弥勒县| 鹤山市| 无锡市| 乐亭县| 普宁市| 泰来县| 安庆市| 垫江县| 静安区| 龙海市| 松滋市| 玉林市| 光泽县| 满城县| 房山区| 镇雄县| 南木林县| 巴中市| 伊吾县| 神池县| 登封市| 秦安县| 故城县|