當前位置:首頁 » 編程語言 » java串列

java串列

發布時間: 2022-09-28 10:00:05

A. java:什麼是類的串列化有什麼作用舉個栗子唄。

對象的壽命通常隨著生成該對象的程序的終止而終止。有時候,可能需要將對象的狀態保存下來,在需要時再將對象恢復。我們把對象的這種能記錄自己的狀態以便將來再生的能力。叫作對象的持續性(persistence)。對象通過寫出描述自己狀態的數值來記錄自己 ,這個過程叫對象的串列化(Serialization-連續) 。串列化的主要任務是寫出對象實例變數的數值。如果變數是另一對象的引用,則引用的對象也要串列化。這個過程是遞歸的,串列化可能要涉及一個復雜樹結構的單行化,包括原有對象、對象的對象、對象的對象的對象等等。對象所有權的層次結構稱為圖表(graph)。

Java對象的單行化的目標是為Java的運行環境提供一組特性,如下所示:
1) 盡量保持對象串列化的簡單扼要 ,但要提供一種途徑使其可根據開發者的要求進行擴展或定製。
2) 串列化機制應嚴格遵守Java的對象模型 。對象的串列化狀態中應該存有所有的關於種類的安全特性的信息。
3) 對象的串列化機制應支持Java的對象持續性。
4) 對象的串列化機制應有足夠的 可擴展能力以支持對象的遠程方法調用(RMI)。
5) 對象串列化應允許對象定義自身 的格式即其自身的數據流表示形式,可外部化介面來完成這項功能。

B. JAVA 線程 串列化

transient 這個關鍵字的意思是,在屬性前面使用,那麼在序列化對象的時候將不將其序列化。
暈死,這句話意思說反了吧?

C. java什麼是串列版本 id

serialVersionUID表示:「串列化版本統一標識符」(serial version universal identifier),簡稱UID。
很久以前Java使用序列化傳輸對象,這個ID用於類的版本號,現在已經被淘汰了。

D. Java串列化怎麼理解什麼是串列化誰能通俗地給我講講

首先,這個概念的原文是 Serialization,而串列化這個翻譯並不是很好,個人傾向於序列化這個翻譯,下面我都會用序列化這個名詞。

所謂序列化是指把一個對象通過某種規則轉化為一串二進制串,字元串就是一種二進制串。但為何要把對象轉化為二進制串呢?因為我們需要保存或者在網路上傳輸它們,而存在於 JVM 內存中的對象並沒有使用者可見的二進制形式。雖然內存中的所有東西仍然是二進制的,但 JVM 向我們屏蔽了內存操作相關的信息,我們不一定能確定某個 JVM 實現是如何在內存中存儲和組織一個 Java 對象的內容的(C/C++ 就可以直接獲取內存塊來作為序列化的二進制串)。

當然,光序列化是不夠的,我們還需要反序列化,也就是如何從二進制串重新轉回對象。這樣當我們從文件中讀取或者在網路的另一頭收到某個對象的二進制串之後,我們才能重新還原回那個對象。

Java 默認實現了自己的序列化,就是使用的內存數據。然而除了 Java 自己的序列化,我們還有很多中序列化方式,例如 hessian。或者說將 Java 對象轉成 json、xml 也是一種序列化。

舉一個非常簡單的例子,例如我們有一個對象 Integer v = 1;。當我們使用 hessian 對其序列化的時候,我們可能會拿到 I1 這樣的字串(並不確定 hessian 生成的串是不是真的是這樣,但是也差不多),其中 i 表示類型是 Integer,而 1 就是這個變數的值,而 I1 就是序列化後的二進制串(一個字元串)。

E. 利用JAVA實現串列通信的優點

利用Java實現串口全雙工通訊

內容:

1. SerialBean
2. SerialBuffer
3. ReadSerial
4. SerialExample

一個嵌入式系統通常需要通過串口與其主控系統進行全雙工通訊,譬如一個流水線控制系統需要不斷的接受從主控系統發送來的查詢和控制信息,並將執行結果或查詢結果發送回主控系統。本文介紹了一個簡單的通過串口實現全雙工通訊的Java類庫,該類庫大大的簡化了對串口進行操作的過程。
本類庫主要包括:SerialBean.java (與其他應用程序的介面), SerialBuffer.java (用來保存從串口所接收數據的緩沖區), ReadSerial.java (從串口讀取數據的程序)。另外本類庫還提供了一個常式SerialExample.java 作為示範。在下面的內容中將逐一對這幾個部分進行詳細介紹。

1. SerialBean
SerialBean是本類庫與其他應用程序的介面。該類庫中定義了SerialBean的構造方法以及初始化串口,從串口讀取數據,往串口寫入數據以及關閉串口的函數。具體介紹如下:

public SerialBean(int PortID)
本函數構造一個指向特定串口的SerialBean,該串口由參數PortID所指定。PortID = 1 表示COM1,PortID = 2 表示COM2,由此類推。

public int Initialize()
本函數初始化所指定的串口並返回初始化結果。如果初始化成功返回1,否則返回-1。初始化的結果是該串口被SerialBean獨占性使用,其參數被設置為9600, N, 8, 1。如果串口被成功初始化,則打開一個進程讀取從串口傳入的數據並將其保存在緩沖區中。

public String ReadPort(int Length)
本函數從串口(緩沖區)中讀取指定長度的一個字元串。參數Length指定所返回字元串的長度。

public void WritePort(String Msg)
本函數向串口發送一個字元串。參數Msg是需要發送的字元串。

public void ClosePort()
本函數停止串口檢測進程並關閉串口。
SerialBean的源代碼如下:

package serial;
import java.io.*;
import java.util.*;
import javax.comm.*;
/**
*
* This bean provides some basic functions to implement full lplex
* information exchange through the srial port.
*
*/
public class SerialBean
{
static String PortName;
CommPortIdentifier portId;
SerialPort serialPort;
static OutputStream out;
static InputStream in;
SerialBuffer SB;
ReadSerial RT;
/**
*
* Constructor
*
* @param PortID the ID of the serial to be used. 1 for COM1,
* 2 for COM2, etc.
*
*/
public SerialBean(int PortID)
{
PortName = "COM" + PortID;
}
/**
*
* This function initialize the serial port for communication. It startss a
* thread which consistently monitors the serial port. Any signal capturred
* from the serial port is stored into a buffer area.
*
*/
public int Initialize()
{
int InitSuccess = 1;
int InitFail = -1;
try
{
portId = CommPortIdentifier.getPortIdentifier(PortName);
try
{
serialPort = (SerialPort)
portId.open("Serial_Communication", 2000);
} catch (PortInUseException e)
{
return InitFail;
}
//Use InputStream in to read from the serial port, and OutputStream
//out to write to the serial port.
try
{
in = serialPort.getInputStream();
out = serialPort.getOutputStream();
} catch (IOException e)
{
return InitFail;
}
//Initialize the communication parameters to 9600, 8, 1, none.
try
{
serialPort.setSerialPortParams(9600,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
} catch ( e)
{
return InitFail;
}
} catch (NoSuchPortException e)
{
return InitFail;
}
// when successfully open the serial port, create a new serial buffer,
// then create a thread that consistently accepts incoming signals from
// the serial port. Incoming signals are stored in the serial buffer.
SB = new SerialBuffer();
RT = new ReadSerial(SB, in);
RT.start();
// return success information
return InitSuccess;
}
/**
*
* This function returns a string with a certain length from the incomin
* messages.
*
* @param Length The length of the string to be returned.
*
*/
public String ReadPort(int Length)
{
String Msg;
Msg = SB.GetMsg(Length);
return Msg;
}
/**
*
* This function sends a message through the serial port.
*
* @param Msg The string to be sent.
*
*/
public void WritePort(String Msg)
{
int c;
try
{
for (int i = 0; i < Msg.length(); i++)
out.write(Msg.charAt(i));
} catch (IOException e) {}
}
/**
*
* This function closes the serial port in use.
*
*/
public void ClosePort()
{
RT.stop();
serialPort.close();
}
}

2. SerialBuffer

SerialBuffer是本類庫中所定義的串口緩沖區,它定義了往該緩沖區中寫入數據和從該緩沖區中讀取數據所需要的函數。

public synchronized String GetMsg(int Length)
本函數從串口(緩沖區)中讀取指定長度的一個字元串。參數Length指定所返回字元串的長度。

public synchronized void PutChar(int c)
本函數望串口緩沖區中寫入一個字元,參數c 是需要寫入的字元。

在往緩沖區寫入數據或者是從緩沖區讀取數據的時候,必須保證數據的同步,因此GetMsg和PutChar函數均被聲明為synchronized並在具體實現中採取措施實現的數據的同步。
SerialBuffer的源代碼如下:

package serial;
/**
*
* This class implements the buffer area to store incoming data from the serial
* port.
*
*/
public class SerialBuffer
{
private String Content = "";
private String CurrentMsg, TempContent;
private boolean available = false;
private int LengthNeeded = 1;
/**
*
* This function returns a string with a certain length from the incomin
* messages.
*
* @param Length The length of the string to be returned.
*
*/
public synchronized String GetMsg(int Length)
{
LengthNeeded = Length;
notifyAll();
if (LengthNeeded > Content.length())
{
available = false;
while (available == false)
{
try
{
wait();
} catch (InterruptedException e) { }
}
}
CurrentMsg = Content.substring(0, LengthNeeded);
TempContent = Content.substring(LengthNeeded);
Content = TempContent;
LengthNeeded = 1;
notifyAll();
return CurrentMsg;
}
/**
*
* This function stores a character captured from the serial port to the
* buffer area.
*
* @param t The char value of the character to be stored.
*
*/
public synchronized void PutChar(int c)
{
Character d = new Character((char) c);
Content = Content.concat(d.toString());
if (LengthNeeded < Content.length())
{
available = true;
}
notifyAll();
}
}

3. ReadSerial
ReadSerial是一個進程,它不斷的從指定的串口讀取數據並將其存放到緩沖區中。

public ReadSerial(SerialBuffer SB, InputStream Port)
本函數構造一個ReadSerial進程,參數SB指定存放傳入數據的緩沖區,參數Port指定從串口所接收的數據流。

public void run()
ReadSerial進程的主函數,它不斷的從指定的串口讀取數據並將其存放到緩沖區中。
ReadSerial的源代碼如下:

package serial;
import java.io.*;
/**
*
* This class reads message from the specific serial port and save
* the message to the serial buffer.
*
*/
public class ReadSerial extends Thread
{
private SerialBuffer ComBuffer;
private InputStream ComPort;
/**
*
* Constructor
*
* @param SB The buffer to save the incoming messages.
* @param Port The InputStream from the specific serial port.
*
*/
public ReadSerial(SerialBuffer SB, InputStream Port)
{
ComBuffer = SB;
ComPort = Port;
}
public void run()
{
int c;
try
{
while (true)
{
c = ComPort.read();
ComBuffer.PutChar(c);
}
} catch (IOException e) {}
}
}

4. SerialExample
SerialExample是本類庫所提供的一個常式。它所實現的功能是打開串口COM1,對其進行初始化,從串口讀取信息對其進行處理後將處理結果發送到串口。

import serial.*;
import java.io.*;
/**
*
* This is an example of how to use the SerialBean. It opens COM1 and reads
* six messages with different length form the serial port.
*
*/
class SerialExample
{
public static void main(String[] args)
{
//TO DO: Add your JAVA codes here
SerialBean SB = new SerialBean(1);
String Msg;
SB.Initialize();
for (int i = 5; i <= 10; i++)
{
Msg = SB.ReadPort(i);
SB.WritePort("Reply: " + Msg);
}
SB.ClosePort();
}
}

5. 編譯與調試

本類庫中使用了Java Communication API (javax.comm)。這是一個Java擴展類庫,並不包括在標準的Java SDK當中。如果你尚未安裝這個擴展類庫的話,你應該從Sun公司的Java站點下載這個類庫並將其安裝在你的系統上。在所下載的包裡麵包括一個安裝說明,如果你沒有正確安裝這個類庫及其運行環境的話,運行這個程序的時候你會找不到串口。

正確安裝Java Communication API並將上述程序編譯通過以後,你可以按如下方法測試這個程序。如果你只有一台機器,你可以利用一條RS-232電纜將COM1和COM2連接起來,在COM1上運行SerialExample,在COM2上運行Windows提供的超級終端程序。如果你有兩台機器的話,你可以利用一條RS-232電纜將兩台機器的COM1(或者是COM2)連接起來,在一端運行常式,另外一端運行Windows提供的超級終端程序。如果有必要的話,可以對SerialExample中所聲明的串口進行相應改動。

本程序在Windows 2000 + Java SDK 1.3環境下編譯通過並成功運行。

F. java的串列化

對象的壽命通常隨著生成該對象的程序的終止而終止。有時候,可能需要將對象的狀態保存下來,在需要時再將對象恢復。我們把對象的這種能記錄自己的狀態以便將來再生的能力,叫做對象的持續性(persistence)。對象通過寫出描述自己狀態的數值來記錄自己,這個過程叫對象的串列化(Serialization)。
在java.io包中,介面Serializable用來作為實現對象串列化的工具,只有實現了Serializable的類的對象才可以被串列化。

定義一個可串列化對象

public class Student implements Serializable{
int id; //學號
String name; //姓名
int age; //年齡
String department //系別
public Student(int id,String name,int age,String department){
this.id = id;
this.name = name;
this.age = age;
this.department = department;
}
}

熱點內容
火車軟卧無線密碼是多少 發布:2024-04-19 07:38:59 瀏覽:422
vb系統文件夾 發布:2024-04-19 07:29:58 瀏覽:739
qt怎麼添加文件夾 發布:2024-04-19 07:22:53 瀏覽:255
sql查詢表是否存在 發布:2024-04-19 06:11:48 瀏覽:622
T178Tccftp 發布:2024-04-19 06:11:35 瀏覽:185
電腦遠程訪問自己的伺服器 發布:2024-04-19 00:08:03 瀏覽:96
噸包演算法 發布:2024-04-19 00:02:13 瀏覽:328
win8解壓縮軟體官方下載 發布:2024-04-18 23:52:27 瀏覽:727
手機軟體在哪個文件夾 發布:2024-04-18 23:51:26 瀏覽:373
未加密魔獸地圖 發布:2024-04-18 23:39:13 瀏覽:947