自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

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

開發(fā) 后端
Java中除了二進(jìn)制文件和使用文本文件外還有基于Data的數(shù)據(jù)操作,這里的Data指的是Java的基本數(shù)據(jù)類型和String?;緮?shù)據(jù)類型包括byte、int、char、long、float、double、boolean和short。

Java中除了二進(jìn)制文件和使用文本文件外還有基于Data的數(shù)據(jù)操作,這里的Data指的是Java的基本數(shù)據(jù)類型和String。基本數(shù)據(jù)類型包括byte、int、char、long、float、double、boolean和short。

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

有一個重要的類RandomAccessFile實現(xiàn)了 DataInput和DataOutput兩個接口使得他能夠?qū)ξ募瑫r進(jìn)行寫和讀的操作。

在DataInputStream和DataOutputStream兩個類中的方法都很簡單,基本結(jié)構(gòu)為readXXXX()和 writeXXXX()其中XXXX代表基本數(shù)據(jù)類型或者String。在這里不多講述,不過值得一提的是我們有必要讀讀java中unicode的編碼規(guī)則,在API doc中有比較詳細(xì)的介紹。

通常我們的對象有很多都是由java的基本數(shù)據(jù)類型構(gòu)成的,比如一個人的信息包括姓名,電子信箱,電話號碼和性別等。其實我們可以用DataInputStream中的方法和DataOutputStream中的方法按照一定的序列把數(shù)據(jù)寫入流中再按照相同的序列把他們讀取出來,這就是我們自己實現(xiàn)的序列化,這可以用在數(shù)據(jù)傳輸中,比如在J2ME聯(lián)網(wǎng)程序中使用序列化機(jī)制傳輸數(shù)據(jù)。下面我們看看如何自己實現(xiàn)序列化,首先我們要有兩個構(gòu)造函數(shù)其中一個參數(shù)為空。

  1. public Account()  
  2. {  
  3. }  
  4. public Account(String userName, String email, int age, boolean gender)  
  5. {  
  6. this.userName = userName;  
  7. this.email = email;  
  8. this.age = age;  
  9. this.gender = gender;  

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

  1. public void serialize(DataOutputStream dos) throws IOException  
  2. {  
  3. dos.writeUTF(userName);  
  4. dos.writeUTF(email);  
  5. dos.writeInt(age);  
  6. dos.writeBoolean(gender);  

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

  1. public static Account deserialize(DataInputStream dis) throws IOException  
  2. {  
  3. Account account = new Account();  
  4. account.userName = dis.readUTF();  
  5. account.email = dis.readUTF();  
  6. account.age = dis.readInt();  
  7. account.gender = dis.readBoolean();  
  8. return account;  

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

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

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

  1. package com.j2medev.mingjava;  
  2. import java.io.*;  
  3. public class TestDataIO  
  4. {  
  5. public static void main(String[] args) throws IOException  
  6. {  
  7. Account account = new Account("mingjava","eric.zhan@263.net",25,true);  
  8. System.out.println("before serialization.........");  
  9. System.out.println(account.toString());  
  10. ByteArrayOutputStream baos = new ByteArrayOutputStream();  
  11. DataOutputStream dos = new DataOutputStream(baos);  
  12. account.serialize(dos);  
  13. DataInputStream dis = new DataInputStream(new ByteArrayInputStream(baos.toByteArray()));  
  14. Account sAccount = Account.deserialize(dis);  
  15. System.out.println("after serialization..........");  
  16. System.out.println(sAccount.toString());  
  17. dos.close();  
  18. dis.close();  
  19. }  
  20. }  
  21. package com.j2medev.mingjava;  
  22. import java.io.*;  
  23. public class Account  
  24. {  
  25. private String userName = "";  
  26. private String email = "";  
  27. private int age = 0;  
  28. private boolean gender = false;  
  29. public Account()  
  30. {}  
  31. public Account(String userName, String email, int age, boolean gender)  
  32. {  
  33. this.userName = userName;  
  34. this.email = email;  
  35. this.age = age;  
  36. this.gender = gender;  
  37. }  
  38. public void serialize(DataOutputStream dos) throws IOException  
  39. {  
  40. dos.writeUTF(userName);  
  41. dos.writeUTF(email);  
  42. dos.writeInt(age);  
  43. dos.writeBoolean(gender);  
  44. }  
  45. public static Account deserialize(DataInputStream dis) throws IOException  
  46. {  
  47. Account account = new Account();  
  48. account.userName = dis.readUTF();  
  49. account.email = dis.readUTF();  
  50. account.age = dis.readInt();  
  51. account.gender = dis.readBoolean();  
  52. return account;  
  53. }  
  54. public String toString()  
  55. {  
  56. return "UserName = " + userName + " Email = " + email + " age = " + age + " gender = " + (gender ? "male" : "female");  
  57. }  

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

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

序列化成功!

【編輯推薦】

  1. Java中的Collection類
  2. 學(xué)習(xí)java大概步驟
  3. 在Java中>、>>、>>>三者的區(qū)別
  4. Java新手入門的30個基本概念
  5. Java中的四個核心技術(shù)思想
責(zé)任編輯:于鐵 來源: 互聯(lián)網(wǎng)
相關(guān)推薦

2016-08-18 14:13:55

JavaScript基本數(shù)據(jù)引用數(shù)據(jù)

2010-10-08 09:02:03

JavaScript基

2018-05-25 09:50:30

Java數(shù)據(jù)類型類型轉(zhuǎn)換

2023-03-27 10:04:27

數(shù)據(jù)類型浮點型布爾型

2020-10-26 13:46:07

Java基礎(chǔ)入門篇

2021-04-28 18:16:24

Rust數(shù)據(jù)類型

2010-04-27 11:03:39

Oracle Java

2019-11-11 14:55:25

Redis數(shù)據(jù)類型命令

2017-02-27 08:34:09

JavaScript數(shù)據(jù)引用

2009-08-14 11:15:45

C#基本數(shù)據(jù)類型

2023-07-04 08:41:08

Redis數(shù)據(jù)類型

2010-01-18 16:49:36

VB.NET基本數(shù)據(jù)類

2024-11-04 06:20:00

Redis單線程

2024-11-12 13:01:46

2022-03-14 09:41:10

POJO類型系統(tǒng)

2024-06-11 08:30:31

2019-08-12 15:40:26

Redis數(shù)據(jù)庫數(shù)據(jù)類型

2010-08-10 17:17:59

2023-04-06 07:49:23

Python數(shù)據(jù)類型

2010-10-08 15:11:28

JavaScript數(shù)
點贊
收藏

51CTO技術(shù)棧公眾號