Properties属性文件学习
2005-03-16 17:02
411 查看
一、读取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=
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=
相关文章推荐
- SpringBoot内部配置:“application.properties配置”和”使用XML配置”,读取属性文件中的内容,日志配置,Profile配置(学习:SpringBoot实战)
- Java学习笔记——JDBC读取properties属性文件
- JSP学习——Properties属性文件的使用注意
- Java学习笔记——JDBC读取properties属性文件
- Struts2 - 在Tag中使用Properties属性文件(从零开始学习Strust2_04)
- Spring学习笔记 通过PropertyPlaceholderConfigurer来使用properties文件初始化Map类型属性
- Java学习笔记——JDBC读取properties属性文件
- Java学习笔记——JDBC读取properties属性文件
- 一起来学习Mybatis(三)-配置文件之properties 属性
- Java属性Properties文件读取工具类
- Properties属性文件中文输入
- spring管理属性配置文件properties——PropertiesFactoryBean和PropertyPlaceholderConfigurer的区别
- 使用Properties读写属性文件
- linux的文件属性和权限学习——分析ls命令结果
- 获取文件属性中详细信息里的各项目(vb 学习备注)
- cocos2d-x 3.0游戏实例学习笔记《卡牌塔防》第七步---英雄要升级&属性--解析csv配置文件
- Java学习笔记(二)——Java操作properties文件
- spring+mybatis 注入properties文件中属性失败导致datasource加载失败的原因和解决
- Spring中属性文件properties的读取与使用
- java中遍历properties属性文件的方法