> Java > java지도 시간 > 본문

Java의 속성 클래스

王林
풀어 주다: 2024-08-30 15:42:36
원래의
939명이 탐색했습니다.

Java의 속성 클래스는 스트리밍용 개체와 관련된 영구 데이터 세트를 보유하는 속성 클래스에 존재하는 특수 개체가 있는 클래스입니다. Properties 클래스는 Hashtable의 하위 클래스로, 일련의 전체 데이터를 키 형태로 문자열로 유지하고 해당 값을 문자열 형태로 유지하는 데 사용됩니다. 이 클래스의 매우 좋은 특징은 키와 함께 제공된 값을 만족하지 않는 값을 반환한다는 것입니다. 다중 스레딩 개념도 속성 클래스를 사용하면 쉽게 만족됩니다.

무료 소프트웨어 개발 과정 시작

웹 개발, 프로그래밍 언어, 소프트웨어 테스팅 등

건축자

Properties 클래스에는 다음과 같은 두 가지 유형의 생성자가 있습니다.

# 생성자로서의 Properties()

Properties() 생성자는 기본값이 없는 속성 객체를 생성하는 데 사용되는 생성자입니다.

# 생성자로서의 속성(Properties Default)

Properties(Properties Default)는 기본값을 propDefault로 사용하는 객체를 생성합니다. 그러나 어떤 경우에도 생성자인 Properties()이든 생성자인 Properties()이든 속성 목록은 비어 있을 뿐입니다.

참고: 설명된 인스턴스 변수는 속성 클래스 내의 속성 개체와 연결된 기본 속성 목록을 보유합니다.

방법

Java 속성은 다음 메소드를 포함하는 클래스 유형입니다.

  • 문자열 getProperty(문자열 키)
  • String getProperty(String key, String defaultProperty)
  • voidList(PrintStream streamOut)
  • voidList(PrintWriter streamOut)
  • voidload(InputStream streamIn)에서 IO 예외 발생
  • 열거 속성 이름()
  • ObjectSetProerty(문자열 키, StringValue)
  • void store(출력 스트림 streamOut, 문자열 설명)

# 객체 setProperty(문자열 키, 문자열 값)

값은 키와 연결되어 있으며, 키와 연결된 이전 값을 반환하거나 키와 값 쌍 사이에 연결이 없는 경우 null을 반환합니다.

# void 로드(InputStream streamIn)에서 IO 예외 발생

Streamln은 매개변수로 전달되어 입력 스트림과 연결되어 속성 목록이 포함된 입력을 받습니다.

# 열거형 propertyNames()

내용 및 설명이 포함된 속성 목록을 구성하는 키를 포함하여 정의된 키의 열거 및 설명이 반환됩니다.

# 문자열 getProperty(키)

전달된 생성자는 연결된 값이 문자열로 포함된 키여야 하며, null 개체가 반환되는 경우 키가 목록에 없거나 기본적으로 어떤 속성 목록에도 존재하지 않습니다.

# String getProperty(StringKey, String defaultProperty)

기본 속성도 반환한다는 점 외에 동작은 String getProperty와 동일합니다. 해당 키가 속성에 있는지, 기본 목록에 있는지는 상관하지 않습니다.

# 무효 목록(PrintStream, streamOut)

streamOut 매개변수는 출력 스트림과 연결되어 속성 목록이 전송되는 즉시 값을 반환합니다.

# 무효 목록(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

Properties 클래스의 속성 목록 메소드에 있는 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 학습자의 빠른 성장을 도와주세요!