비교적 중요한 클래스 속성 (java.util.properties)이 있으며 주로 Java 구성 파일을 읽는 데 사용됩니다. 다양한 언어에는 자체 지원 구성 파일이 있습니다. 구성 파일의 많은 변수가 종종 변경됩니다. 또한 사용자를 용이하게하고 사용자가 프로그램 자체없이 관련 변수 설정을 수정할 수 있습니다. 오늘 우리는 속성을 사용하기 시작할 것입니다.
Java의 속성 사용
속성 문서 설명 :
속성 클래스는 지속적인 속성 세트를 나타냅니다. 속성은 스트림에 저장되거나 스트림에서로드 될 수 있습니다. 속성 목록의 각 키와 해당 값은 문자열입니다.
속성 클래스 설명 :
공개 클래스 속성은 해시 가능 <객체, 개체>를 확장합니다
테스트의 프로젝트 구조는 다음과 같습니다.
1. Huhx.Properties 파일에서 편의를 위해 데이터 조각을 추가합니다.
이름 = huhx
2. 해당 속성을 얻으려면 Huhx.Properties 파일을로드하고 읽으십시오.
속성 속성 = new Properties (); fileInputStream fis = new FileInputStream ( "huhx.properties"); properties.load (fis); system.out.println (properties.get ( "name"));
3. 속성 목록 방법 사용
printstream printstream = system.out; properties.list (printstream);
특정 목록 코드 방법 :
public void list (printstream out) {out.println ( "-나열 속성-"); hashtable h = new hashtable (); h); {val = val.substring (0, 37) + "...";} out.println (key + "=" + val);}} 4. 속성 저장소 사용
outputStream outputStream = 새 FileOutputStream ( "huhx.txt"); properties.store (outputStream, "comment");
5. Properties 'storetoxml 방법 사용
outputStream outputStream2 = 새 파일 아웃 PutStream ( "huhx.xml"); properties.storetoxml (outputStream2, "comment");
6. 최종 생성 파일은 다음과 같습니다.
huhx.txt :
#의견#THU 5 월 19 일 19:19:36 CST 2016Name = Huhx
huhx.xml :
<? xml version = "1.0"encoding = "utf-8"stockalone = "no"?> <! doctype properties 시스템 "http://java.sun.com/dtd/properties.dtd"> properties> << huhx </retort> </reportife> huhx </lenetter> huhx <same"> huhx <same "
친절한 링크, propertiestest.java :
패키지 com.huhx.linux; import java.io.fileInputStream; import java.io.fileoutputStream; import java.io.outputStream; import java.io.printstream; import java.util.properties; public static void main (string [] args) {// 일반적인 제도 {// 일반적인 제도 {// properties (); fileInputStream fis = new FileInputStream ( "huhx.properties"); properties.load (fis); system.out.println (properties.get ( "name")); // 다음은 printstream printstream = system.out; listrestrem = orputstream = list의 부분입니다. fileoutputStream ( "huhx.txt"); properties.store (outputStream, "comment"); outputStream outputStream2 = new FileOutputStream ( "huhx.xml"); Properties.StoretOxml (outputStream2, "commiss")} 위는 편집자가 귀하에게 소개 한 Java에서 속성 사용에 대한 자세한 설명입니다. 나는 그것이 당신에게 도움이되기를 바랍니다. 궁금한 점이 있으면 메시지를 남겨 주시면 편집자가 제 시간에 답장을 드리겠습니다. Wulin.com 웹 사이트를 지원해 주셔서 대단히 감사합니다!