Properties属性文件学习

一、读取Properties属性文件信息

test.txt

 

name=bitan
password=bitan
time=2004-11-7

 

GetPropertiesDemo.java:
package test.properties;

GetPropertiesDemo.java:
package test.properties;

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


public class GetPropertiesDemo {

 public static void main(String[] args) throws IOException {
        Class c = new GetPropertiesDemo().getClass();
        InputStream is = c.getResourceAsStream("test.txt");
        Properties prop = new Properties();
        prop.load(is);
        String name = prop.getProperty("name");
        String password = prop.getProperty("password");
        String time = prop.getProperty("time");
        System.out.print(name + " " + password + " " + time);

        is.close();
 }
}

结果:

bitan bitan 2004-11-7

二、利用Properties读取和写入XML文件信息

bitan.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE properties SYSTEM 'http://java.sun.com/dtd/properties.dtd'>
<properties>
    <entry key="name">bitan</entry>
    <entry key="age">20</entry>
    <entry key="sex">male</entry>
    <entry key="address">Changde Hunan China</entry>
</properties>

 

 

Tiger16.java


package tigers;

import java.io.*;
import java.util.*;
public class Tiger16 {
 public static void main(String[] args) {
  try {
   Properties prop = new Properties();
   prop.loadFromXML(new FileInputStream("bitan.xml"));
   String name = prop.getProperty("name");
   String age = prop.getProperty("age");
   String sex = prop.getProperty("sex");
   String address = prop.getProperty("address");
   System.out.println(name + ", " + age + ", " + sex + ", " + address);
   prop.list(System.out);
   prop.storeToXML(new FileOutputStream("bitan2.xml"), "a simple test", "utf-8");
   prop = new Properties();
   prop.setProperty("name", "helen");
   prop.setProperty("age", "22");
   prop.setProperty("sex", "female");
   prop.setProperty("address", "Hangzhou Zhejiang China");
   prop.storeToXML(new FileOutputStream("helen.xml"), "helen's information.");
  } catch (Exception e) {
   e.printStackTrace();
  }
 }
}


package tigers;

import java.io.*;
import java.util.*;
public class Tiger16 {
 public static void main(String[] args) {
  try {
   Properties prop = new Properties();
   prop.loadFromXML(new FileInputStream("bitan.xml"));
   String name = prop.getProperty("name");
   String age = prop.getProperty("age");
   String sex = prop.getProperty("sex");
   String address = prop.getProperty("address");
   System.out.println(name + ", " + age + ", " + sex + ", " + address);
   prop.list(System.out);
   prop.storeToXML(new FileOutputStream("bitan2.xml"), "a simple test", "utf-8");
   prop = new Properties();
   prop.setProperty("name", "helen");
   prop.setProperty("age", "22");
   prop.setProperty("sex", "female");
   prop.setProperty("address", "Hangzhou Zhejiang China");
   prop.storeToXML(new FileOutputStream("helen.xml"), "helen's information.");
  } catch (Exception e) {
   e.printStackTrace();
  }
 }
}

 

结果:

 

bitan, 20, male, Changde Hunan China
-- listing properties --
address=Changde Hunan China
age=20
name=bitan
sex=male

bitan2.xml

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<comment>a simple test</comment>
<entry key="address">Changde Hunan China</entry>
<entry key="age">20</entry>
<entry key="name">bitan</entry>
<entry key="sex">male</entry>
</properties>

 

helen.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<comment>helen's information.</comment>
<entry key="address">Hangzhou Zhejiang China</entry>
<entry key="age">22</entry>
<entry key="name">helen</entry>
<entry key="sex">female</entry>
</properties>


结论:

可以利用这个途径快速存取一些比较简短而又重要的信息,替代或补充数据库。


结论:

可以利用这个途径快速存取一些比较简短而又重要的信息,替代或补充数据库。

三、利用Properties读取和写入普通文件信息


package tigers;

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


public class Tiger17 {

 public static void main(String[] args) {
  try {
   Properties prop = new Properties();
   prop.setProperty("name", "bitan");
   prop.setProperty("age", "24");
   prop.setProperty("sex", "male");
   prop.store(new FileOutputStream("bitan.txt"), "bitan's information");
   prop.load(new FileInputStream("bitan.txt"));
   prop.list(System.out);
  } catch (Exception e) {
   e.printStackTrace();
  }
 }
}

结果:

 

-- listing properties --
age=24
name=bitan
sex=male

 

bitan.txt

#bitan's information
#Wed Feb 09 13:49:34 CST 2005
age=24
name=bitan
sex=male


 

四、利用Properties获取操作系统信息

package tigers;

import java.util.*;


public class Tiger15 {

 public static void main(String[] args) {
  Properties prop = new Properties(System.getProperties());
  prop.list(System.out);
 }
}

结果:

 

-- listing properties --
java.runtime.name=Java(TM) 2 Runtime Environment, Stand...
sun.boot.library.path=K:/tiger/jre/bin
java.vm.version=1.5.0_01-b08
java.vm.vendor=Sun Microsystems Inc.
java.vendor.url=http://java.sun.com/
path.separator=;
java.vm.name=Java HotSpot(TM) Client VM
file.encoding.pkg=sun.io
user.country=CN
sun.os.patch.level=Service Pack 1
java.vm.specification.name=Java Virtual Machine Specification
user.dir=G:/workspace/tigetest
java.runtime.version=1.5.0_01-b08
java.awt.graphicsenv=sun.awt.Win32GraphicsEnvironment
java.endorsed.dirs=K:/tiger/jre/lib/endorsed
os.arch=x86
java.io.tmpdir=C:/DOCUME~1/bitan/LOCALS~1/Temp/
line.separator=

java.vm.specification.vendor=Sun Microsystems Inc.
user.variant=
os.name=Windows XP
sun.jnu.encoding=GBK
java.library.path=K:/tiger/jre/bin;.;C:/WINDOWS/System3...
java.specification.name=Java Platform API Specification
java.class.version=49.0
sun.management.compiler=HotSpot Client Compiler
os.version=5.1
user.home=C:/Documents and Settings/bitan
user.timezone=
java.awt.printerjob=sun.awt.windows.WPrinterJob
file.encoding=GBK
java.specification.version=1.5
user.name=bitan
java.class.path=G:/workspace/tigetest/bin
java.vm.specification.version=1.0
sun.arch.data.model=32
java.home=K:/tiger/jre
java.specification.vendor=Sun Microsystems Inc.
user.language=zh
awt.toolkit=sun.awt.windows.WToolkit
java.vm.info=mixed mode, sharing
java.version=1.5.0_01
java.ext.dirs=K:/tiger/jre/lib/ext
sun.boot.class.path=K:/tiger/jre/lib/rt.jar;K:/tiger/jre/...
java.vendor=Sun Microsystems Inc.
file.separator=/
java.vendor.url.bug=http://java.sun.com/cgi-bin/bugreport...
sun.cpu.endian=little
sun.io.unicode.encoding=UnicodeLittle
sun.desktop=windows
sun.cpu.isalist=

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值