首页> Java> java教程> 正文

Java 中的属性类

王林
发布: 2024-08-30 15:42:36
原创
924 人浏览过

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); } }
登录后复制

输出:

Java 中的属性类

示例#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); } }
登录后复制

输出:

Java 中的属性类

示例 #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(); } }
登录后复制

输出:

Java 中的属性类

示例#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()); } }
登录后复制

输出:

Java 中的属性类

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:

Java 中的属性类

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:

Java 中的属性类

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:

Java 中的属性类

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:

Java 中的属性类

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中文网其他相关文章!

相关标签:
来源:php
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!