property在Java中的用法 java中property的属性问题

java\u7f16\u7a0b\u4e2dProperties\u7c7b\u7684\u5177\u4f53\u4f5c\u7528\u548c\u4f7f\u7528\uff01

\u5982\u679c\u4e0d\u719f\u6089 java.util.Properties\u7c7b\uff0c\u90a3\u4e48\u73b0\u5728\u544a\u8bc9\u60a8\u5b83\u662f\u7528\u6765\u5728\u4e00\u4e2a\u6587\u4ef6\u4e2d\u5b58\u50a8\u952e-\u503c\u5bf9\u7684\uff0c\u5176\u4e2d\u952e\u548c\u503c\u662f\u7528\u7b49\u53f7\u5206\u9694\u7684\u3002\uff08\u5982\u6e05\u5355 1 \u6240\u793a\uff09\u3002\u6700\u8fd1\u66f4\u65b0\u7684java.util.Properties \u7c7b\u73b0\u5728\u63d0\u4f9b\u4e86\u4e00\u79cd\u4e3a\u7a0b\u5e8f\u88c5\u8f7d\u548c\u5b58\u50a8\u8bbe\u7f6e\u7684\u66f4\u5bb9\u6613\u7684\u65b9\u6cd5\uff1a loadFromXML(InputStreamis) \u548c storeToXML(OutputStream os, String comment) \u65b9\u6cd5\u3002

\u4e00\u4e0b\u662f\u8be6\u7ec6\u7684\u8bf4\u660e\uff0c\u5e0c\u671b\u80fd\u7ed9\u5927\u5bb6\u5e26\u6765\u5e2e\u52a9\u3002

\u6e05\u5355 1. \u4e00\u7ec4\u5c5e\u6027\u793a\u4f8b

foo=bar
fu=baz




\u5c06\u6e05\u5355 1 \u88c5\u8f7d\u5230 Properties \u5bf9\u8c61\u4e2d\u540e\uff0c\u60a8\u5c31\u53ef\u4ee5\u627e\u5230\u4e24\u4e2a\u952e\uff08 foo \u548c fu \uff09\u548c\u4e24\u4e2a\u503c\uff08 foo \u7684 bar \u548c fu \u7684baz \uff09\u4e86\u3002\u8fd9\u4e2a\u7c7b\u652f\u6301\u5e26 \u \u7684\u5d4c\u5165 Unicode \u5b57\u7b26\u4e32\uff0c\u4f46\u662f\u8fd9\u91cc\u91cd\u8981\u7684\u662f\u6bcf\u4e00\u9879\u5185\u5bb9\u90fd\u5f53\u4f5c String \u3002

\u6e05\u53552 \u663e\u793a\u4e86\u5982\u4f55\u88c5\u8f7d\u5c5e\u6027\u6587\u4ef6\u5e76\u5217\u51fa\u5b83\u5f53\u524d\u7684\u4e00\u7ec4\u952e\u548c\u503c\u3002\u53ea\u9700\u4f20\u9012\u8fd9\u4e2a\u6587\u4ef6\u7684 InputStream \u7ed9 load()\u65b9\u6cd5\uff0c\u5c31\u4f1a\u5c06\u6bcf\u4e00\u4e2a\u952e-\u503c\u5bf9\u6dfb\u52a0\u5230 Properties \u5b9e\u4f8b\u4e2d\u3002\u7136\u540e\u7528 list() \u5217\u51fa\u6240\u6709\u5c5e\u6027\u6216\u8005\u7528 getProperty()\u83b7\u53d6\u5355\u72ec\u7684\u5c5e\u6027\u3002

\u6e05\u5355 2. \u88c5\u8f7d\u5c5e\u6027

import java.util.*;
import java.io.*;

public class LoadSample {
public static void main(String args[]) throws Exception {
Properties prop = new Properties();
FileInputStream fis =
new FileInputStream("sample.properties");
prop.load(fis);
prop.list(System.out);
System.out.println("\nThe foo property: " +
prop.getProperty("foo"));
}
}




\u8fd0\u884c LoadSample \u7a0b\u5e8f\u751f\u6210\u5982\u6e05\u5355 3 \u6240\u793a\u7684\u8f93\u51fa\u3002\u6ce8\u610f list() \u65b9\u6cd5\u7684\u8f93\u51fa\u4e2d\u952e-\u503c\u5bf9\u7684\u987a\u5e8f\u4e0e\u5b83\u4eec\u5728\u8f93\u5165\u6587\u4ef6\u4e2d\u7684\u987a\u5e8f\u4e0d\u4e00\u6837\u3002Properties \u7c7b\u5728\u4e00\u4e2a\u6563\u5217\u8868\uff08hashtable\uff0c\u4e8b\u5b9e\u4e0a\u662f\u4e00\u4e2a Hashtable \u5b50\u7c7b\uff09\u4e2d\u50a8\u5b58\u4e00\u7ec4\u952e-\u503c\u5bf9\uff0c\u6240\u4ee5\u4e0d\u80fd\u4fdd\u8bc1\u987a\u5e8f\u3002

\u6e05\u5355 3. LoadSample \u7684\u8f93\u51fa

-- listing properties --
fu=baz
foo=bar

The foo property: bar




XML \u5c5e\u6027\u6587\u4ef6
\u8fd9\u91cc\u6ca1\u6709\u4ec0\u4e48\u65b0\u5185\u5bb9\u3002 Properties \u7c7b\u603b\u662f\u8fd9\u6837\u5de5\u4f5c\u7684\u3002\u4e0d\u8fc7\uff0c\u65b0\u7684\u5730\u65b9\u662f\u4ece\u4e00\u4e2a XML \u6587\u4ef6\u4e2d\u88c5\u8f7d\u4e00\u7ec4\u5c5e\u6027\u3002\u5b83\u7684 DTD \u5982\u6e05\u5355 4 \u6240\u793a\u3002

\u6e05\u5355 4. \u5c5e\u6027 DTD












\u5982\u679c\u4e0d\u60f3\u7ec6\u8bfb XML DTD\uff0c\u90a3\u4e48\u53ef\u4ee5\u544a\u8bc9\u60a8\u5b83\u5176\u5b9e\u5c31\u662f\u8bf4\u5728\u5916\u56f4 \u6807\u7b7e\u4e2d\u5305\u88c5\u7684\u662f\u4e00\u4e2a \u6807\u7b7e\uff0c\u540e\u9762\u662f\u4efb\u610f\u6570\u91cf\u7684 \u6807\u7b7e\u3002\u5bf9\u6bcf\u4e00\u4e2a \u6807\u7b7e\uff0c\u6709\u4e00\u4e2a\u952e\u5c5e\u6027\uff0c\u8f93\u5165\u7684\u5185\u5bb9\u5c31\u662f\u5b83\u7684\u503c\u3002\u6e05\u5355 5 \u663e\u793a\u4e86 \u6e05\u5355 1\u4e2d\u7684\u5c5e\u6027\u6587\u4ef6\u7684 XML \u7248\u672c\u662f\u4ec0\u4e48\u6837\u5b50\u7684\u3002

\u6e05\u5355 5. XML \u7248\u672c\u7684\u5c5e\u6027\u6587\u4ef6




Hi
bar
baz





\u5982\u679c\u6e05\u5355 6 \u6240\u793a\uff0c\u8bfb\u53d6 XML \u7248\u672c\u7684 Properties \u6587\u4ef6\u4e0e\u8bfb\u53d6\u8001\u683c\u5f0f\u7684\u6587\u4ef6\u6ca1\u4ec0\u4e48\u4e0d\u540c\u3002

\u6e05\u5355 6. \u8bfb\u53d6 XML Properties \u6587\u4ef6

import java.util.*;
import java.io.*;

public class LoadSampleXML {
public static void main(String args[]) throws Exception {
Properties prop = new Properties();
FileInputStream fis =
new FileInputStream("sampleprops.xml");
prop.loadFromXML(fis);
prop.list(System.out);
System.out.println("\nThe foo property: " +
prop.getProperty("foo"));
}
}




\u5173\u4e8e\u8d44\u6e90\u7ed1\u5b9a\u7684\u8bf4\u660e
\u867d\u7136 java.util.Properties \u7c7b\u73b0\u5728\u9664\u4e86\u652f\u6301\u952e-\u503c\u5bf9\uff0c\u8fd8\u652f\u6301\u5c5e\u6027\u6587\u4ef6\u4f5c\u4e3a XML \u6587\u4ef6\uff0c\u4e0d\u5e78\u7684\u662f\uff0c\u6ca1\u6709\u5185\u7f6e\u7684\u9009\u9879\u53ef\u4ee5\u5c06ResourceBundle \u4f5c\u4e3a\u4e00\u4e2a XML \u6587\u4ef6\u5904\u7406\u3002\u662f\u7684\uff0c PropertyResourceBundle \u4e0d\u4f7f\u7528 Properties\u5bf9\u8c61\u6765\u88c5\u8f7d\u7ed1\u5b9a\uff0c\u4e0d\u8fc7\u88c5\u8f7d\u65b9\u6cd5\u7684\u4f7f\u7528\u662f\u786c\u7f16\u7801\u5230\u7c7b\u4e2d\u7684\uff0c\u800c\u4e0d\u4f7f\u7528\u8f83\u65b0\u7684 loadFromXML() \u65b9\u6cd5\u3002


\u8fd0\u884c\u6e05\u5355 6 \u4e2d\u7684\u7a0b\u5e8f\u4ea7\u751f\u4e0e\u539f\u6765\u7684\u7a0b\u5e8f\u76f8\u540c\u7684\u8f93\u51fa\uff0c\u5982 \u6e05\u5355 2\u6240\u793a\u3002

\u4fdd\u5b58 XML \u5c5e\u6027
\u65b0\u7684 Properties \u8fd8\u6709\u4e00\u4e2a\u529f\u80fd\u662f\u5c06\u5c5e\u6027\u5b58\u50a8\u5230 XML \u683c\u5f0f\u7684\u6587\u4ef6\u4e2d\u3002\u867d\u7136 store() \u65b9\u6cd5\u4ecd\u7136\u4f1a\u521b\u5efa\u4e00\u4e2a\u7c7b\u4f3c \u6e05\u5355 1\u6240\u793a\u7684\u6587\u4ef6\uff0c\u4f46\u662f\u73b0\u5728\u53ef\u4ee5\u7528\u65b0\u7684 storeToXML() \u65b9\u6cd5\u521b\u5efa\u5982 \u6e05\u5355 5 \u6240\u793a\u7684\u6587\u4ef6\u3002\u53ea\u8981\u4f20\u9012\u4e00\u4e2a OutputStream\u548c\u4e00\u4e2a\u7528\u4e8e\u6ce8\u91ca\u7684 String \u5c31\u53ef\u4ee5\u4e86\u3002\u6e05\u5355 7 \u5c55\u793a\u4e86\u65b0\u7684 storeToXML() \u65b9\u6cd5\u3002

\u6e05\u5355 7. \u5c06 Properties \u5b58\u50a8\u4e3a XML \u6587\u4ef6

import java.util.*;
import java.io.*;

public class StoreXML {
public static void main(String args[]) throws Exception {
Properties prop = new Properties();
prop.setProperty("one-two", "buckle my shoe");
prop.setProperty("three-four", "shut the door");
prop.setProperty("five-six", "pick up sticks");
prop.setProperty("seven-eight", "lay them straight");
prop.setProperty("nine-ten", "a big, fat hen");
FileOutputStream fos =
new FileOutputStream("rhyme.xml");
prop.storeToXML(fos, "Rhyme");
fos.close();
}
}




\u8fd0\u884c\u6e05\u5355 7 \u4e2d\u7684\u7a0b\u5e8f\u4ea7\u751f\u7684\u8f93\u51fa\u5982\u6e05\u5355 8 \u6240\u793a\u3002

\u6e05\u5355 8. \u5b58\u50a8\u7684 XML \u6587\u4ef6




Rhyme
lay them straight
pick up sticks
a big, fat hen
shut the door
buckle my shoe

\u5728\u8fd9\u91cc\u6539\u4e86\u4e00\u4e2a\u4f8b\u5b50\uff1a
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
/**
* \u5b9e\u73b0properties\u6587\u4ef6\u7684\u8bfb\u53d6
* @author haoxuewu
*/
public class Test {
public static void main(String[] args) {
try {
long start = System.currentTimeMillis();
InputStream is = new FileInputStream("conf.properties");
Properties p = new Properties();
p.load(is);
is.close();
System.out.println("SIZE : " + p.size());
System.out.println("homepage : " + p.getProperty("homepage"));
System.out.println("author : " + p.getProperty("author"));
System.out.println("school : " + p.getProperty("school"));
long end = System.currentTimeMillis();
System.out.println("Cost : " + (end - start));
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
}
conf.properties
# Configuration file
homepage = http://www.blogjava.net/haoxuewu
author = bbflyerwww
school = jilinjianzhugongchengxueyuan

Result
SIZE:3
homepage : http://www.blogjava.net/haoxuewu
author : bbflyerwww
school : jilinjianzhugongchengxueyuan

\u4f60\u7528<s:textfiled \u8bbe\u7f6e\u4e0breadonly\uff0c\u53d8\u6210\u53ea\u8bfb\uff0c\u4e0d\u8ba9\u66f4\u6539\u5c31\u884c\u4e86\uff0c\u8bbe\u7f6e\u4e2aname\u673a\u5b50\u5c31\u4f20\u4e86\uff1b
\u8981\u4e0d\u5c31\u7528\u8f93\u51fa \uff0c\u5728\u7528\u4e00\u4e2a\u8bb0\u5f55\u4e0b\u5c31\u884c\u4e86\uff1b

JDK 中的 Properties 类 Properties 类存在于胞 Java.util 中,该类继承自 Hashtable ,它提供了几个主要的方法:
1. getProperty ( String key) , 用指定的键在此属性列表中搜索属性。也就是通过参数 key ,得到 key 所对应的 value。
2. load ( InputStream inStream) ,从输入流中读取属性列表(键和元素对)。通过对指定的文件(比如说上面的 test.properties 文件)进行装载来获取该文件中的所有键 - 值对。以供 getProperty ( String key) 来搜索。
3. setProperty ( String key, String value) ,调用 Hashtable 的方法 put 。他通过调用基类的put方法来设置 键 - 值对。
4. store ( OutputStream out, String comments) , 以适合使用 load 方法加载到 Properties 表中的格式,将此 Properties 表中的属性列表(键和元素对)写入输出流。与 load 方法相反,该方法将键 - 值对写入到指定的文件中去。
5. clear () ,清除所有装载的 键 - 值对。该方法在基类中提供。

 在项目中经常用到各种配置文件,有.properties的,也有.xml格式的

都可以通过java.utils.Property类进行处理。

1. 读取.properties文件

File pFile = new File("test.properties");
FileInputStream   pInStream=null;
try {
pInStream = new FileInputStream(pFile );
} catch (FileNotFoundException e) {
e.printStackTrace(); 
}
Properties p = new Properties();
try {
p.load(pInStream );  
} catch (IOException e) {
e.printStackTrace(); 
}
Enumeration enu = p.propertyNames();  //取出所有的key
p.list(System.out) ;
while(enu.hasMoreElements())
{
String thisKey = (String)enu.nextElement();
System.out.println("key="+thisKey);
System.out.println("value="+p.getProperty(thisKey));
}

.properties测试文件内容如下:

    name = tinyfun
    age = 25
    sex = man
    title = software developer

    2. 读取XML文件

               File pFile = new File("test.xml");
FileInputStream pInStream = null;
try {
pInStream = new FileInputStream(pFile);
Properties p = new Properties();
p.loadFromXML(pInStream);
p.list(System.out);

} catch (IOException e) {
e.printStackTrace();
}

xml文件如下:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<comment>test</comment>
<entry key="age">25</entry>
<entry key="name">tinyfun</entry>
<entry key="sex">man</entry>
<entry key="title">software developer</entry>
</properties>

3. 写入XML文件

                Properties p = new Properties();
p.setProperty("name","tinyfun");
p.setProperty("age","25");
p.setProperty("sex","man");
p.setProperty("title","software developer");
try{
PrintStream fW = new PrintStream(new File("test.xml"));
p.storeToXML(fW,"test");
} catch (IOException e) {
e.printStackTrace();
}

4. 写入.properties文件

                Properties p = new Properties();
p.setProperty("name","tinyfun");
p.setProperty("age","25");
p.setProperty("sex","man");
p.setProperty("title","software developer");
try{
PrintStream fW = new PrintStream(new File("test.properties"));
p.list(fW );        } 
catch (IOException e) {
e.printStackTrace();
}

http://blog.csdn.net/hopestar2/article/details/6372883



  • JAVA銆俠ean field 鍜 property 鏈変粈涔堝紓鍚屽悧?
    绛旓細field鏄垚鍛樺彉閲 property鏄睘鎬 鍚湁get銆乻et鏂规硶锛堜笉鍐檚et鏂规硶鍒欐槸鍙锛夊苟涓旀槸绉佹湁鐨勬墠鍙睘鎬с傛病鏈塯et銆乻et鏂规硶鐨勬槸鎴愬憳鍙橀噺锛屾垚鍛樺彉閲忓彲浠ョ敤public淇グ銆傚紩鐢ㄨbean鐨勫睘鎬х殑璇濋渶瑕侀氳繃get鏂规硶锛屽彲浠ョ悊瑙d负澶氫簡涓灞傛帴鍙o紝澶栫晫涓嶇敤鑾风煡閲岄潰鐨勫疄鐜版柟寮忋
  • 鍦╦ava涓濡備綍璇诲彇properties鏂囦欢?
    绛旓細浣跨敤java.util.Properties\x0d\x0a\x0d\x0a1銆佸垱寤轰竴涓狿roperties瀵硅薄銆俓x0d\x0a2銆佷娇鐢ㄥ璞$殑load鏂规硶鍔犺浇浣犵殑property鏂囦欢銆俓x0d\x0a3銆佷娇鐢╣etProperty鏂规硶鍙栧笺俓x0d\x0a渚嬪瓙:\x0d\x0apackage com.bill.test;\x0d\x0a\x0d\x0aimport java.io.FileInputStream;\x0d\x0aimport java.util...
  • java缂栫▼涓Properties绫荤殑鍏蜂綋浣滅敤鍜屼娇鐢!
    绛旓細铏界劧 java.util.Properties 绫荤幇鍦ㄩ櫎浜嗘敮鎸侀敭-鍊煎锛岃繕鏀寔灞炴ф枃浠朵綔涓 XML 鏂囦欢锛屼笉骞哥殑鏄紝娌℃湁鍐呯疆鐨勯夐」鍙互灏哛esourceBundle 浣滀负涓涓 XML 鏂囦欢澶勭悊銆傛槸鐨勶紝 PropertyResourceBundle 涓嶄娇鐢 Properties瀵硅薄鏉ヨ杞界粦瀹氾紝涓嶈繃瑁呰浇鏂规硶鐨勪娇鐢ㄦ槸纭紪鐮佸埌绫涓殑锛岃屼笉浣跨敤杈冩柊鐨 loadFromXML() 鏂规硶銆傝繍琛屾竻鍗...
  • Java涓 @Property浠涔堟剰鎬?
    绛旓細灞炴х殑鎰忔濓紝浣滀负鏍囪锛屽氨鍍廆Override 涓鏍
  • java璋冪敤properties鏂囦欢鐨勯棶棰
    绛旓細import java.util.Iterator;import java.util.Properties;public class PropertyTest { public static void main(String[] args) { try { // 璇诲彇灞炴ф枃浠禷.properties InputStream in = new BufferedInputStream(new FileInputStream("a.properties"));// /鍔犺浇灞炴у垪琛 Properties prop = new ...
  • java宸ョ▼涓properties鏂囦欢
    绛旓細import java.util.Properties;public class PropertiesTest { /*瀹氫箟闈欐佹柟娉,绫诲悕鍙互鐩存帴璋冪敤,鐢ㄤ簬璇诲彇properties灞炴ф枃浠涓殑鍐呭*/ public static void initFile(){ /*D://a.properties婧愬睘鎬ф枃浠剁殑璺緞鍜屽悕瀛*/ File file = new File("D://a.properties");FileInputStream fis = null;try { ...
  • java涓璸roperty鐨灞炴ч棶棰
    绛旓細浣犵敤<s:textfiled 璁剧疆涓媟eadonly锛屽彉鎴愬彧璇伙紝涓嶈鏇存敼灏辫浜嗭紝璁剧疆涓猲ame鏈哄瓙灏变紶浜嗭紱瑕佷笉灏辩敤杈撳嚭 锛屽湪鐢ㄤ竴涓褰曚笅灏辫浜嗭紱
  • JAVABEAN涓璸rorerty="*"鐨勭敤娉
    绛旓細servlet涓巎avabean鐨勫疄鐜版槸鏈変竴涓繃绋嬬殑,鍙互浜掑姩,鍦ㄤ簰鍔ㄧ殑杩囩▼ 涓篃鏄渶瑕佷竴姝ヤ竴姝ヨ繘琛屽鐞,MVC妯″紡,浠巎avabea鍒皊ervlet,涔熷彲浠巎avabean鐩存帴鍒癑SP椤甸潰,propenrty灞炴у彧鑳鍦╦avabean閲浣跨敤,鍐嶈浜,鍙湁javabean鎵嶈兘鏈塸ropenrty鐨勮缃笌寰楀埌鐨勬柟娉,濡傛灉浣犻潪瑕佺敤servlet浣跨敤,閭d箞浣犺繕鏄閫氳繃javabean璋冪敤鏉ヨ幏鍙...
  • 鍦╦ava涓濡備綍璇诲彇properties鏂囦欢
    绛旓細Java璇诲彇properties鏂囦欢鐨勬柟娉曟瘮杈冨锛岀綉涓婃渶澶氱殑鏂囩珷鏄"Java璇诲彇properties鏂囦欢鐨勫叚绉嶆柟娉"锛屼絾鍦↗ava搴旂敤涓紝鏈甯哥敤杩樻槸閫氳繃java.lang.Class绫荤殑getResourceAsStream(String name) 鏂规硶鏉ュ疄鐜帮紝浣嗘垜瑙佸埌浼楀璇诲彇properties鏂囦欢鐨勪唬鐮佷腑锛岄兘浼氳繖涔堝共锛欼nputStream in = getClass().getResourceAsStream("璧勬簮...
  • java涓param浠涔堟剰鎬?
    绛旓細param鏄寚鏄庝竴涓嚱鏁扮殑鍙傛暟鐨勬剰鎬 java閲岀殑@param鐨勪綔鐢 param鏍囩鍙互褰掓。鏂规硶鎴栨瀯閫犲櫒鐨勬煇涓崟涓鍙傛暟锛屾垨鑰呭綊妗g被銆佹帴鍙d互鍙婃硾鍨嬫柟娉曠殑绫诲瀷鍙傛暟銆傚湪浣跨敤@ param鏍囩鏃讹紝鎴戜滑搴旇閽堝鏂规硶鐨勬瘡涓涓弬鏁伴兘浣跨敤涓涓鏍囩銆傛瘡涓钀界殑绗竴涓瘝浼氳褰撲綔鍙傛暟鍚嶏紝鑰屼綑涓嬬殑閮ㄥ垎鍒欎細琚綋浣滄槸瀵瑰畠鐨勬弿杩般
  • 扩展阅读:黑马java视频 ... java入门网站 ... pubmed官方官网入口 ... 九一pro ... x86国产 ... java windowbuilder ... java properties ... javascript 在线 ... java blockingqueue ...

    本站交流只代表网友个人观点,与本站立场无关
    欢迎反馈与建议,请联系电邮
    2024© 车视网