return pops.getProperty(key); } 调用文件比如:String s = Property.getProperty("username");
Class GlobalFactory{ /**获取属性property*/ public String getProperties(String property){ Properties pro = new Properties(); InputStream in = GlobalFactory.class .getResourceAsStream("/factory.properties"); try { pro.load(in);
import java.io.*;public class Main2 {
public static void main(String[] args) throws Exception {
Properties p = new Properties();
p.put("name", "value");
p.put("name1", "value1");
p.put("名称", "数值");
//
OutputStream os = new FileOutputStream("main2.properties");
p.store(os, "comments for main2");
InputStream is = new FileInputStream("main2.properties");
p.load(is);
System.out.println(p);
//
is = Main2.class.getResourceAsStream("/main2.properties");
p.load(is);
System.out.println(p);
//
is = Main2.class.getClassLoader().getResourceAsStream("main2.properties");
p.load(is);
System.out.println(p);
}
}
使用store()方法进行保存,使用load()方法加载对应的properties文件,读取和保存的时候汉字都会自动进行转码。main2.properties文件内容如下:#comments for main2
#Sun Dec 14 23:48:27 CST 2008
\u540D\u79F0=\u6570\u503C
name=value
name1=value1
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Properties;/**
* @author <a href="mailto:">imasmallbird </a>
* @version $Revision 1.1 $ Jan 13, 2009 3:45:15 PM
*/
public class Test { private static Properties config; /**
* 加载配置文件
*/
private void initConfig() {
InputStream in;
try {
in = new BufferedInputStream(new FileInputStream("E:/conf/cs.properties")); // 你的配置文件
config = new Properties();
config.load(in);
in.close();
} catch (Exception e) {
// ;
}
} /**
* @return 获得配置文件信息.
*/
public static Properties getConfig() {
return config;
} public static void main(String[] args) {
Test test = new Test();
test.initConfig(); // 在你程序启动的地方进行初始化,可以改动这个initConfig()为公有的
// Test.getConfig()返回你的资源文件,然后进行操作
}
}
public static Properties loadAppProps() { String proPath = System.getProperty("user.dir")
+ "\\config\\app.properties";//文件地址
proPath = replace(proPath, "\\", File.separator);
proPath = replace(proPath, "/", File.separator);
try {
System.out.print("属性文件读取配置!");
pros.load(new FileInputStream(proPath));
System.out.println("ip:" + pros.getProperty("ip"));
System.out.println("userName :" + pros.getProperty("userName"));
System.out.println("password:" + pros.getProperty("password"));
System.out.println("filename:" + pros.getProperty("filename"));
} catch (IOException e) {
System.out.println("config\\app.properties 加载出错!");
}
return pros;
}
ip=
username=
password=
filename=
import java.io.FileInputStream;
import java.util.Properties;public class LoadPropertiesTest {
private Properties prop = null; LoadPropertiesTest() {
prop = new Properties();
} public void load(String path) {
FileInputStream stream = null; try {
stream = new FileInputStream(path);
prop.load(stream); stream.close();
stream = null;
} catch (Exception e) {
e.printStackTrace();
}
} public String getProperty(String key) {
return prop.getProperty(key);
}
public static void main(String args[]){
LoadPropertiesTest imp = new LoadPropertiesTest();
imp.load("100.properties");
System.out.println(imp.getProperty("ip"));
System.out.println(imp.getProperty("username"));
System.out.println(imp.getProperty("password"));
System.out.println(imp.getProperty("filename"));
}
}
username=aaa
password=aaa
filename=bbb
Proterty.java文件import java.io.IOException;
import java.util.Properties;public class Property {
static Properties pops = new Properties();
static {
try {
pops.load(Property.class.getClassLoader().getResourceAsStream("config/test.properties")); } catch (IOException e) {
e.printStackTrace();
}
}
private Property() {}; // 防止别人调用时更改方法.
public static String getProperty(String key) {
return pops.getProperty(key);
}
调用文件比如:String s = Property.getProperty("username");
/**获取属性property*/
public String getProperties(String property){
Properties pro = new Properties();
InputStream in = GlobalFactory.class
.getResourceAsStream("/factory.properties");
try {
pro.load(in);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return pro.getProperty(property);
}
}
String driver, url, user, pwd;
public DBInf()
{
}
public void load() throws Exception
{
InputStream ins = getClass().getResourceAsStream("/DBCfg/DBCfg.txt");
Properties p = new Properties();
p.load(ins);
driver = p.getProperty("dbdriver");
url = p.getProperty("dburl");
user = p.getProperty("dbuser");
pwd = p.getProperty("dbpwd");
ins.close();
}
dbdriver=oracle.jdbc.driver.OracleDriver
dburl=jdbc:oracle:thin:@218.198.118.126:1521:IRSS
dbuser=U_MBA
dbpwd=U_MBA
import java.io.InputStream;
import java.util.Properties;public class Q14 {
public void readProperties() {
InputStream inStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("FileName");
Properties properties = new Properties();
try {
properties.load(inStream);
// TODO
properties.getProperty("KeyName");
//TODO
} catch (IOException e) {
// TODO
e.printStackTrace();
}
}
@Deprecated
public static void main(String[] args) {
Q14 q14 = new Q14();
q14.readProperties();
}
}
import java.io.FileInputStream;
import java.util.Properties;public class LoadPropertiesTest {
private Properties prop = null; LoadPropertiesTest() {
prop = new Properties();
} public void load(String path) {
FileInputStream stream = null; try {
stream = new FileInputStream(path);
prop.load(stream); stream.close();
stream = null;
} catch (Exception e) {
e.printStackTrace();
}
} public String getProperty(String key) {
return prop.getProperty(key);
}
public static void main(String args[]){
LoadPropertiesTest imp = new LoadPropertiesTest();
imp.load("100.properties");
System.out.println(imp.getProperty("ip"));
System.out.println(imp.getProperty("username"));
System.out.println(imp.getProperty("password"));
System.out.println(imp.getProperty("filename"));
}
}