Javaのプロパティクラス

王林
リリース: 2024-08-30 15:42:36
オリジナル
951 人が閲覧しました

Java のプロパティ クラスは、プロパティ クラス内に存在する特別なオブジェクトを持つクラスであり、ストリーミング用のオブジェクトに関連する永続データ セットを保持します。 Properties クラスは Hashtable のサブクラスであり、一連のデータ全体を文字列としてのキーの形式で維持し、その値を文字列の形式で維持するために使用されます。このクラスの非常に優れた特性は、キーで指定された値を満たさない値を返すことです。複数のスレッドの概念もプロパティ クラスで簡単に実現できます。

無料ソフトウェア開発コースを始めましょう

Web 開発、プログラミング言語、ソフトウェア テスト、その他

コンストラクター

Properties クラスには次の 2 種類のコンストラクターがあります。

# コンストラクターとしての Properties()

Properties() コンストラクターは、デフォルト値のないプロパティ オブジェクトを作成するために使用されるコンストラクターです。

# コンストラクターとしての Properties(Properties Default)

Properties(Properties Default) は、デフォルト値を propDefault として使用するオブジェクトを作成します。ただし、いずれの場合も、コンストラクターとしての Properties() であっても、コンストラクターとしての Properties() であっても、プロパティ リストは空のみになります。

注: 説明されているインスタンス変数は、プロパティ クラス内のプロパティ オブジェクトにリンクされたデフォルトのプロパティ リストを保持します。

メソッド

Java プロパティはクラスの一種で、その中に次のメソッドが含まれます:

  • 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(文字列キー, 文字列値)

値はキーに関連付けられており、キーに関連付けられている前の値を返すか、キーと値のペアの間に関連付けがない場合は null を返します。

# voidload (InputStream streamIn) は IO 例外をスローします

Streamln は、渡されるパラメーターとして入力ストリームにリンクされ、プロパティ リストのリストを含む入力を取得します。

# 列挙 propertyNames()

定義されたキーの列挙と説明が返されます。これには、その内容と説明を含むプロパティ リストを構成するキーも含まれます。

# String getProperty(Key)

渡されるコンストラクターは、文字列として関連付けられた値を持つキーである必要があり、null オブジェクトが返された場合、キーはリストに存在しないか、デフォルトとしてどのプロパティ リストにも存在しません。

# String getProperty(StringKey, String defaultProperty)

デフォルトの Property も返すという点を除けば、動作は String getProperty と同じです。キーがプロパティまたはデフォルトのリストに存在するかどうかは関係ありません。

# void list(PrintStream, streamOut)

パラメータ streamOut は出力ストリームにリンクされ、プロパティ リストが送信されるとすぐに値を返します。

# void list(PrintWriter streamOut)

出力ストリームにリンクされたパラメータ streamOut は、プロパティ リストが渡されるとすぐに返されます。これは、その動作が少しだけ多くの権限を備えた印刷ストリームと同じであることを意味します

# void store(OutputStream streamOut, 説明)

outputStream にリンクされた streamOut はプロパティ リストを書き込みます。その文字列は、一度書き込まれると、指定された説明とともに書き込まれます。

注: プロパティ クラス内で定義されたメソッドは 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 中国語 Web サイトの他の関連記事を参照してください。

関連ラベル:
ソース:php
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!