Java 中的屬性類別是屬性類別中存在特殊物件的類,該類別保存與串流傳輸物件相關的持久性資料集。 Properties類是Hashtable的子類,用於以字串形式維護整個系列的數據,鍵為字串,值也為字串形式。這個類別的一個非常好的特性是它會傳回不滿足鍵提供的值的值。多執行緒概念也可以透過屬性類別輕鬆滿足。
開始您的免費軟體開發課程
網頁開發、程式語言、軟體測試及其他
建構子
Properties 類別中有兩種類型的建構函數,如下:
# Properties() 作為建構子
Properties() 建構子是用來建立沒有預設值的屬性物件的建構子。
# Properties(Properties Default) 作為建構子
Properties(Properties Default) 建立一個使用其預設值作為 propDefault 的物件。但無論如何,無論是 Properties() 作為建構子或 Properties() 作為建構函數,屬性列表都只會是空的。
注意:所描述的實例變數保存與屬性類別中的屬性物件連結的預設屬性清單。方法
Java Properties 是一種類,其中包含以下方法:
- String getProperty(String Key)
- String getProperty(String key, String defaultProperty)
- voidList(PrintStream streamOut)
- voidList(PrintWriter streamOut)
- voidload(InputStream streamIn) 拋出 IO 異常
- 枚舉propertyNames()
- ObjectSetProerty(String key, StringValue)
- void store(輸出流streamOut,字串描述)
# 物件 setProperty(String key, String value)
一個值與鍵關聯,它傳回與該鍵關聯的前一個值,如果鍵和值對之間沒有關聯,則傳回 null。
# void load (InputStream streamIn) 拋出 IO 異常
Streamln 作為傳遞的參數,與輸入流連結並取得具有屬性清單清單的輸入。
# 枚舉 propertyNames()
傳回已定義鍵的列舉和描述,包括那些也包含屬性清單及其內容和解釋的鍵。
# String getProperty(Key)
傳遞的建構函式應該是一個鍵,其關聯值為字串,如果傳回空對象,則這些鍵要麼不存在於列表中,要麼預設不存在於任何屬性列表中。
# String getProperty(StringKey, String defaultProperty)
行為與 String getProperty 相同,除了它還傳回預設屬性之外。它不關心鍵是否存在於屬性或預設清單中。
# void list(PrintStream, streamOut)
參數streamOut與輸出流鏈接,並在發送屬性列表後立即返回值。
# void list(PrintWriter streamOut)
與輸出流連結的參數streamOut在傳遞屬性清單後立即返回,這表示它的行為與列印流相同,但權限稍多
# void store(OutputStream streamOut, 描述)
與outputStream連結的streamOut寫入屬性清單;一旦寫入,該字串將使用指定的描述進行寫入。
注意:properties 類別中定義的方法繼承自 hashTable;所有這些方法都支援屬性類別的屬性對象,並定義了一些遺留類別方法。Java 中屬性類別的範例
下面提到了不同的例子:
範例#1
示範與 Properties 類別相關的方法和建構子的程式。
import java.util.*; public class PropertiesExmpl { public static void main(String arg[]) { Properties ex = new Properties(); Set url; String str; ex.put("ide", "ide.educba.org"); ex.put("contribution", "contribution.educba.org"); ex.put("articles", "articles.contribution.educba.org"); url = ex.keySet(); Iterator itr = url.iterator(); while(itr.hasNext()) { str = (String)itr.next(); System.out.println("The url for " + str + " is " + ex.getProperty(str)); } System.out.println(); str = ex.getProperty("article", "not found"); System.out.println("This is the respective url for the article " + str); } }
輸出:
範例#2
示範與屬性清單關聯的清單方法的程式。
import java.util.*; public class PropertiesList { public static void main(String arg[]) { Properties ex = new Properties(); Set url; String str; ex.put("ide", "ide.educba.org"); ex.put("contribution", "contribution.educba.org"); ex.put("article", "article.educba.org"); ex.list(System.out); } }
輸出:
範例#3
使用屬性類別的屬性清單方法中存在的 PrintWriter 方法的程式。
import java.io.PrintWriter; import java.util.*; public class PropertiesDem1 { public static void main(String arg[]) { Properties ex = new Properties(); PrintWriter writer = new PrintWriter(System.out); ex.put("ide", "ide.educba.org"); ex.put("contribution", "contribution.educba.org"); ex.put("article", "article.educba.org"); ex.list(writer); writer.flush(); } }
輸出:
範例#4
用於枚舉和演示屬性清單中存在的值的程式。
import java.util.*; public class PropertiesDem2 { public static void main(String arg[]) { Properties exmpl = new Properties(); Set str; String s; exmpl.put("ide", "ide.educba.org"); exmpl.put("contribution", "contribution.educba.org"); exmpl.put("artcle", "article.educba.org"); Enumeration name = exmpl.propertyNames(); System.out.println(name.nextElement()); System.out.println(name.nextElement()); System.out.println(name.nextElement()); } }
輸出:
Example #5
Program to set the value with the properties object of properties class.
import java.util.*; public class PropertiesDem4 { public static void main(String arg[]) { Properties exmpl3 = new Properties(); exmpl3.put("ide", "ide.educba.org"); exmpl3.put("contribute", "contribute.educba.org"); exmpl3.setProperty("article", "article.educba.org"); System.out.println(exmpl3); } }
Output:
Example #6
Program to demonstrate the properties class method of loading the data set.
import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.util.*; public class PropertiesDem8 { public static void main(String arg[]) throws IOException { Properties exmpl4 = new Properties(); String s = "ide = ide.educba.org"; FileOutputStream out = new FileOutputStream("properties.txt"); FileInputStream in = new FileInputStream("properties.txt"); out.write(s.getBytes()); exmpl4.load(in); exmpl4.list(System.out); } }
Output:
Example #7
Program to demonstrate the Properties class associated with the object of the properties class and then load the values within the properties class.
import java.io.IOException; import java.io.StringReader; import java.util.*; public class PropertiesDem9 { public static void main(String arg[]) throws IOException { Properties ex = new Properties(); String s = "ide = ide.educba.org"; StringReader reader = new StringReader(s); ex.load(reader); ex.list(System.out); } }
Output:
Example #8
The program stores the properties’ objects and key values in the properties class.
import java.io.IOException; import java.io.StringReader; import java.util.*; public class PropertiesDem5 { public static void main(String arg[]) throws IOException { Properties ex = new Properties(); ex.put("ide", "ide.educba.org"); ex.put("contribution", "contribution.educba.org"); ex.put("article", "article.edu.cba.org"); ex.store(System.out, "Demo for all Properties class"); } }
Output:
Advantages of Properties class in Java:
- It is easier for end-users to get the maintained form of a list specifying the required value and associated key.
- Return of default value is a very good property for specification and kind of notification if the key and value pair don’t get matched, it will return the null or default value associated.
- There is no need for external synchronization in multi-threading, which shares object properties within the property class.
Conclusion – Properties Class in Java
The properties class has an instance variable within the object of the properties class, which is used to get the return value and to get the associated default value with the streaming also associated with it. Therefore, the properties class in Java is pivotal in the methods and constructors present.
以上是Java 中的屬性類的詳細內容。更多資訊請關注PHP中文網其他相關文章!

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于结构化数据处理开源库SPL的相关问题,下面就一起来看一下java下理想的结构化数据处理类库,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于PriorityQueue优先级队列的相关知识,Java集合框架中提供了PriorityQueue和PriorityBlockingQueue两种类型的优先级队列,PriorityQueue是线程不安全的,PriorityBlockingQueue是线程安全的,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于java锁的相关问题,包括了独占锁、悲观锁、乐观锁、共享锁等等内容,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于多线程的相关问题,包括了线程安装、线程加锁与线程不安全的原因、线程安全的标准类等等内容,希望对大家有帮助。

本篇文章给大家带来了关于Java的相关知识,其中主要介绍了关于关键字中this和super的相关问题,以及他们的一些区别,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于枚举的相关问题,包括了枚举的基本操作、集合类对枚举的支持等等内容,下面一起来看一下,希望对大家有帮助。

封装是一种信息隐藏技术,是指一种将抽象性函式接口的实现细节部分包装、隐藏起来的方法;封装可以被认为是一个保护屏障,防止指定类的代码和数据被外部类定义的代码随机访问。封装可以通过关键字private,protected和public实现。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于设计模式的相关问题,主要将装饰器模式的相关内容,指在不改变现有对象结构的情况下,动态地给该对象增加一些职责的模式,希望对大家有帮助。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

DVWA
Damn Vulnerable Web App (DVWA) 是一個PHP/MySQL的Web應用程序,非常容易受到攻擊。它的主要目標是成為安全專業人員在合法環境中測試自己的技能和工具的輔助工具,幫助Web開發人員更好地理解保護網路應用程式的過程,並幫助教師/學生在課堂環境中教授/學習Web應用程式安全性。 DVWA的目標是透過簡單直接的介面練習一些最常見的Web漏洞,難度各不相同。請注意,該軟體中

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

PhpStorm Mac 版本
最新(2018.2.1 )專業的PHP整合開發工具

Safe Exam Browser
Safe Exam Browser是一個安全的瀏覽器環境,安全地進行線上考試。該軟體將任何電腦變成一個安全的工作站。它控制對任何實用工具的訪問,並防止學生使用未經授權的資源。

禪工作室 13.0.1
強大的PHP整合開發環境