First, read the Properties property file information
Test.txt
Name = Bitanpassword = Bitantime = 2004-11-7
GetPropertiesDemo.java: postage test.properties;
GetPropertiesDemo.java: postage 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; string password = prop.getProperty ("password"); string time = prop.getProperty ("time"); system.out.print (name "" Password " Time);
Is.close ();}}
result:
Bitan Bitan 2004-11-7
Second, use Properties to read and write XML file information
Bitan.xml
XML Version = "1.0" encoding = "UTF-8"?>
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 = 20name = Bitansex = MaleBitan2.xml
XML Version = "1.0" Encoding = "UTF-8"?>
Helen.xml
XML Version = "1.0" encoding = "UTF-8"?>
You can quickly access some relatively short and important information, replace or supplement the database.
in conclusion:
You can quickly access some relatively short and important information, replace or supplement the database.
Third, use Properties to read and write ordinary file information
Package tigers;
Import java.io. *; import java.util. *;
Public class tiger17 {
Public static void main (string [] args) {Try {Properties Prop = New Properties (); Prop.SetProperty; 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 ();}}}
result:
- Listing Properties --age = 24name = Bitansex = Male
Bitan.txt
# Bitan's Information # WED Feb 09 13:49:34 CST 2005AGE = 24Name = Bitansex = MALE
Fourth, use Properties to get operating system information
Package tigers;
Import java.util. *;
Public class tiger15 {
Public static void main (String [] args) {Properties Prop = New Properties (system.getproperties ()); prop.list (system.out);}}
result:
- Listing Properties --java.Runtime.name = Java (TM) 2 Runtime Environment, Stand ... Sun.boot.library.path = k: /tiger/jre/binjava.vm.version=1.5.0_01-b08java .vm.vendor = sun microsystems Inc.java.vendor.url = http: //java.sun.com/path.separator=; java.vm.name = java hotspot (tm) Client Vmfile.Encoding.pkg = sun. iouser.country = CNsun.os.patch.level = Service Pack 1java.vm.specification.name = Java Virtual Machine Specificationuser.dir = G: /workspace/tigetestjava.runtime.version=1.5.0_01-b08java.awt.graphicsenv= Sun.awt.win32graphicsenvironmentjava.endorsed.dirs = k: /tiger/jre/lib/endorsedos.Arch=x86java.io.tmpdir=c: /docume ~1/bitan/locals ~ 1/temp/line.separator=