久久久久无码精品,四川省少妇一级毛片,老老熟妇xxxxhd,人妻无码少妇一区二区

Java中基本數(shù)據(jù)類型與應(yīng)用

時(shí)間:2024-07-27 10:43:16 JAVA認(rèn)證 我要投稿
  • 相關(guān)推薦

Java中基本數(shù)據(jù)類型與應(yīng)用

  Java中除了二進(jìn)制文件和使用文本文件外還有基于Data的數(shù)據(jù)操作,這里的Data指的是Java的基本數(shù)據(jù)類型和String.基本數(shù)據(jù)類型包括byte、int、char、long、float、double、boolean和short.下面一起來(lái)看看Java中的基本數(shù)據(jù)類型與應(yīng)用,僅供參考!

  說(shuō)到Java的基本數(shù)據(jù)類型必須談到的兩個(gè)類是DataInputStream和DataOutputStream.它們提供了對(duì)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ǎn)單,基本結(jié)構(gòu)為readXXXX()和 writeXXXX()其中XXXX代表基本數(shù)據(jù)類型或者String.在這里不多講述,不過(guò)值得一提的是我們有必要讀讀java中unicode的編碼規(guī)則,在API doc中有比較詳細(xì)的介紹。通常我們的對(duì)象有很多都是由java的基本數(shù)據(jù)類型構(gòu)成的,比如一個(gè)人的信息包括姓名,電子信箱,電話號(hào)碼和性別等。其實(shí)我們可以用DataInputStream中的方法和DataOutputStream中的方法按照一定的序列把數(shù)據(jù)寫入流中再按照相同的序列把他們讀取出來(lái),這就是我們自己實(shí)現(xiàn)的序列化,這可以用在數(shù)據(jù)傳輸中,比如在J2ME聯(lián)網(wǎng)程序中使用序列化機(jī)制傳輸數(shù)據(jù)。下面我們看看如何自己實(shí)現(xiàn)序列化,首先我們要有兩個(gè)構(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í)候也很簡(jiǎn)單,我們只是往DataOutputStream中按照順序?qū)懭雽?duì)象的成員變量。例如:

  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()的方法打印出對(duì)象的實(shí)際信息。這是個(gè)好的習(xí)慣。

  public String toString()

  {

  return "UserName = " + userName + " Email = " + email + " age = " + age + " gender = " + (gender ? "male" : "female");

  }

  為了測(cè)試序列化我們編寫下面的程序進(jìn)行測(cè)試,代碼比較簡(jiǎ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)行程序在控制臺(tái)輸出:

  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

  序列化成功!

【Java中基本數(shù)據(jù)類型與應(yīng)用】相關(guān)文章:

java數(shù)據(jù)類型和運(yùn)算符10-24

Java基本編程技巧07-13

Java代碼的基本知識(shí)10-26

在Java中執(zhí)行JavaScript代碼07-14

Java中的動(dòng)態(tài)代碼編程06-27

java基本語(yǔ)法復(fù)習(xí)知識(shí)點(diǎn)大全01-23

商務(wù)交往中的基本禮儀10-05

SQL Server常用數(shù)據(jù)類型06-18

java程序中如何調(diào)用linux命令08-27

刺繡在服裝中的應(yīng)用05-18