1.ตัวอย่างการอ่านค่า properties
2.ตัวอย่างการเขียนไฟล์ properties
3.ตัวอย่างไฟล์ properties
package com.test.test;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
public class AppGetProp
{
public static void main( String[] args )
{
Properties prop = new Properties();
try {
//load a properties file
prop.load(new FileInputStream("config.properties"));
//get the property value and print it out
System.out.println(prop.getProperty("database"));
System.out.println(prop.getProperty("dbuser"));
System.out.println(prop.getProperty("dbpassword"));
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
************************************
2.ตัวอย่างการเขียนไฟล์ properties
package com.test.test;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;
public class AppSetProp {
public static void main( String[] args )
{
Properties prop = new Properties();
try {
//set the properties value
prop.setProperty("database", "localhost");
prop.setProperty("dbuser", "mkyong");
prop.setProperty("dbpassword", "password");
//save properties to project root folder
prop.store(new FileOutputStream("config.properties"), null);
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
************************************
#Wed Jul 21 00:20:09 ICT 2010
dbpassword=password_xxx
database=localhostx
dbuser=user_xxx
*************************************
ไม่มีความคิดเห็น:
แสดงความคิดเห็น