Java ObjectOutputStream writeFields()方法與示例

ObjectOutputStream類writeFields()方法 (ObjectOutputStream Class writeFields() method)

  • writeFields() method is available in java.io package.

    在java.io包中提供了writeFields()方法

  • writeFields() method is used to write the buffered fields to the ObjectOutputStream.

    writeFields()方法用于將緩沖的字段寫入ObjectOutputStream。

  • writeFields() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.

    writeFields()方法是一個非靜態方法,只能通過類對象訪問,如果嘗試使用類名稱訪問該方法,則會收到錯誤消息。

  • writeFields() method may throw an exception at the time of writing fields.

    writeFields()方法在寫入字段時可能會引發異常。

    • IOException: This exception may throw when getting any input/output error while writing to the output stream.IOException :在寫入輸出流時遇到任何輸入/輸出錯誤時,可能引發此異常。
    • NotActiveException: This exception may throw when writeObject() was not invoked to write the state of the object of classes.NotActiveException :如果未調用writeObject()來寫入類對象的狀態,則可能引發此異常。

Syntax:

句法:

    public void writeFields();

Parameter(s):

參數:

  • It does not accept any parameter.

    它不接受任何參數。

Return value:

返回值:

The return type of the method is void, it returns nothing.

該方法的返回類型為void ,不返回任何內容。

Example:

例:

// Java program to demonstrate the example 
// of void writeFields() method of ObjectOutputStream
import java.io.*;
public class WriteFieldsOfOOS {
public static void main(String[] args) throws Exception {
// Instantiates ObjectOutputStream , ObjectInputStream 
// FileInputStream and FileOutputStream
FileOutputStream file_out_stm = new FileOutputStream("D:\\includehelp.txt");
ObjectOutputStream obj_out_stm = new ObjectOutputStream(file_out_stm);
FileInputStream file_in_stm = new FileInputStream("D:\\includehelp.txt");
ObjectInputStream obj_in_stm = new ObjectInputStream(file_in_stm);
// By using writeInt() method is to write
// integer to the obj_out_stm stream
obj_out_stm.writeObject(new ReadFields());
obj_out_stm.flush();
// By using readObject() method is to read
//  object and display fields
ReadFields rf = (ReadFields) obj_in_stm.readObject();
System.out.println("obj_in_stm.writeFields(): " + rf.val);
}
static public class ReadFields implements Serializable {
static int val = 12685;
private static final ObjectStreamField[] serialPersistentFields = {
new ObjectStreamField("val", Integer.TYPE)
};
private void readObject(ObjectInputStream oin)
throws IOException, ClassNotFoundException {
// Invokes readFields()
ObjectInputStream.GetField fi = oin.readFields();
// By using get() is to get var
val = fi.get("val", 0);
}
private void writeObject(ObjectOutputStream oos) throws IOException {
// By using putFields() method is to
// write var into ObjectStreamField[]
ObjectOutputStream.PutField fi = oos.putFields();
fi.put("val", val);
// Invokes writeFields()
oos.writeFields();
}
}
}

Output

輸出量

obj_in_stm.writeFields(): 12685

翻譯自: https://www.includehelp.com/java/objectoutputstream-writefields-method-with-example.aspx

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/379943.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/379943.shtml
英文地址,請注明出處:http://en.pswp.cn/news/379943.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

利用帶關聯子查詢Update語句更新數據

Update是T-sql中再簡單不過的語句了,update table set columnexpression [where condition],我們都會用到。但update的用法不僅于此,真正在開發的時候,靈活恰當地使用update可以達到事半功倍的效果。 假定有表Table1(…

SQL Server 2000數據庫移植到SQL Server 2008R2數據庫服務器中碰到的”3145錯誤”及解決辦法...

辛苦忙碌了一個星期終于安裝配置好了TFS服務器,給每個團隊成員分配了賬戶和郵箱。不過,老機器中的部分數據需要備份到新機器中,其中在移植一個使用DVBBS架設的論壇的時候,出了點問題,記錄如下,以備查找&…

web安全----XSS漏洞之基本原理

0x01 概述 XSS為跨站腳本攻擊,XSS攻擊針對的是用戶層面的攻擊!類型有反射型XSS、存儲型XSS、DOM型XSS,這里的DOM可以理解為頁面,或者是所有的標簽、內容之和 0x02 反射型XSS 反射型XSS攻擊流程為: 即: …

面向對象(匿名內部類與有名字內部類的比較)

A:匿名內部類 就是內部類的簡化寫法B:前提 這里的類可以是具體類也可以是抽象類C:格式 new 類名或者接口(){ //表示繼承這個類或實現這個接口重寫方法}D:本質是什么呢? 是一個繼承了該類或者實現了該接口的子類匿名對象E:案…

如何在Python中針對一個值檢查多個變量?

Given multiple variables and they are assigned some values, we have to test a value with these variables. 給定多個變量并為其分配了一些值,我們必須使用這些變量測試一個值。 Let, there are three variables a, b and c and we have to check whether one…

品析《桃花庵歌》

桃花庵歌 【明】唐寅(YIN) 桃花塢里桃花庵,桃花庵下桃花仙; 桃花仙人種桃樹,又摘桃花賣酒錢。 酒醒只在花前坐,酒醉還來花下眠; 半醒半醉日復日,花落花開年復年。 但愿老死花酒間&#xf…

面向對象(匿名內部類重寫多個方法調用)

①匿名內部類只針對重寫一個方法的時候使用; ②若有多個方法,通過匿名內部類進行調用的時候,需要一個一個進行調用,比較麻煩,所以不建議使用。 ③匿名內部類是無法向下轉型的,向下轉型需要子類的類名,這里面沒有子類…

c++ 取兩個鏈表的交集_使用C ++程序查找兩個鏈表的交集

c 取兩個鏈表的交集Problem statement: Write a C program to find the intersection of two single linked lists. 問題陳述:編寫一個C 程序來查找兩個單個鏈表的交集。 Example: 例: Let the first linked list be:6->5->2->9->NULLLet …

只在IE中顯示

只在IE中顯示div{display:none;display:block;_display:block}轉載于:https://www.cnblogs.com/lishenglyx/archive/2008/08/21/1273089.html

web安全---XSS漏洞之標簽使用2

所有標簽已經測試完并可以使用&#xff0c;測試環境&#xff1a;DVWA的反射型XSS&#xff0c;等級low 0x01 <script>標簽 <script>alert(2)</script> <script>alert(2)</script//0x02 <img>標簽 <img src"x" onerroralert(1)…

Java——多線程(鐵路售票系統案例)

問題&#xff1a;鐵路售票&#xff0c;一共100張&#xff0c;通過四個窗口賣完。 要求&#xff1a;分別用 繼承Thread類 和 實現Runnable接口 去實現 ①用繼承Thread類去實現 package com.yy.syn;public class Demo3_Ticket { /*** 鐵路售票&#xff0c;一共100張&#xff…

Java LocalDateTime類| 帶示例的getDayOfWeek()方法

LocalDateTime類getDayOfWeek()方法 (LocalDateTime Class getDayOfWeek() method) getDayOfWeek() method is available in java.time package. getDayOfWeek()方法在java.time包中可用。 getDayOfWeek() method is used to get the field value day-of-week that is an enum …

軟件測試方法大匯總

軟件測試方法大匯總 軟件測試方法種類繁多&#xff0c;記憶起來混亂&#xff0c; 如果把軟件測試方法進行分類, 就會清晰很多。 我參考一些書籍和網上的資料&#xff0c; 把常用的軟件測試方法列出來&#xff0c; 讓大家對軟件測試行業有個總體的看法。 從測試設計方法分類 測試…

web安全----xss工具使用3

XSSer 0x01 安裝 環境&#xff1a;kali、python3&#xff08;必須是python3&#xff0c;kali默認為python2&#xff09; 安裝步驟&#xff1a; git clone https://github.com/epsylon/xsser.git cd xsser sudo python3 setup.py install 使用命令&#xff1a; xsser -h查看…

Java——多線程(死鎖)

死鎖是指&#xff1a;兩個或兩個以上的進程在執行過程中&#xff0c;由于競爭資源或者由于彼此通信而造成的一種阻塞的現象&#xff0c;若無外力作用&#xff0c;它們都將無法推進下去。此時稱系統處于死鎖狀態或系統產生了死鎖&#xff0c;這些永遠在互相等待的進程稱為死鎖進…

c# 前導0_C#| 用前導零填充整數

c# 前導0To pad an integer number with leading zero, we can use String.Format() method which is library method of String class in C#. 要用前導零填充整數&#xff0c;我們可以使用String.Format()方法&#xff0c;該方法是C&#xff03;中String類的庫方法。 using S…

走到盡頭的技術-MVC

MVC技術是一種WebApplication設計技術&#xff0c;相比于傳統Web應用程序&#xff0c;MVC可以使程序結構更加清晰&#xff0c;他采用 Model&#xff0c;View&#xff0c;Controller 來管理和架構我們的Web資源&#xff0c;將不同的請求&#xff08;request&#xff09;導向不同…

Search Engine -垂直搜索小匯總

FilesTube: 共享文件搜索引擎,文件來自:Rapidshare, MegaUpload, Megashares, YouSendIt, SaveFile, FileFront和Badongo等很多文件儲存網站,支持的文件格式包括:AVI, MP3, MPEG, MPG, RAR, WMA, WMV, EXE, ZIP等,主要為媒體格式,不支持中文 Picsearch:專業圖片搜索引擎,中文界…

Java——多線程(線程安全問題)

同步為安全&#xff0c;不同步為不安全&#xff1b;也就是有synchronized這個標識符&#xff0c;就為線程安全&#xff0c;反之&#xff0c;為線程不安全。 ①Vector是線程安全的 ②StringBuffer是線程安全的 ③Hashtable是線程安全的 Collections.synchronized(xxx)&#…

web安全---XSS利用平臺BLUE-LOTUS安裝與使用

0x01 安裝 環境&#xff1a;windows、phpstudy 下載地址&#xff1a;https://gitee.com/gid1314/BlueLotus_XSSReceiver-master 下載后將文件解壓&#xff0c;重命名為blue&#xff0c;放在www目錄下 訪問&#xff1a;http://IP/blue 點擊安裝 這里只需要修改后臺登陸密碼和…