类为java是跨平台的,所有配置文件不可能是绑定到任何一个平台上的,其配置文件也要有自己的一套东西.
不可能是.ini等这样的格式,而是properties.
1。使用java.util.Properties类的load()方法示例:InputStreamin=lnewBufferedInputStream (newFileInputStream(name));Propertiesp=newProperties();p.load(in);
2。使用java.util.ResourceBundle类的getBundle()方法示例:ResourceBundlerb=ResourceBundle.getBundle(name,Locale.getDefault());
3。使用java.util.PropertyResourceBundle类的构造函数示例:InputStreamin= newBufferedInputStream(newFileInputStream(name));ResourceBundlerb=newPropertyResourceBundle(in);
4。使用class变量的getResourceAsStream()方法示例:InputStreamin= JProperties.class.getResourceAsStream(name);Propertiesp=newProperties();p.load(in);
5。使用class.getClassLoader()所得到的java.lang.ClassLoader的getResourceAsStream ()方法示例:InputStreamin=JProperties.class.getClassLoader(). getResourceAsStream(name);Propertiesp=newProperties();p.load(in);
6。使用java.lang.ClassLoader类的getSystemResourceAsStream()静态方法示例: InputStreamin=ClassLoader.getSystemResourceAsStream(name);Propertiesp=newProperties();p.load(in);
补充 Servlet中可以使用javax.servlet.ServletContext的getResourceAsStream()方法示例: InputStreamin=context.getResourceAsStream(path);Propertiesp=newProperties();p.load(in);
==============================================================================================================
在properties文件中储存的是key和value类似java类里的Map,key和value之间用=号或者:分开。当要进行多行输入时用\号来进行换行。如:
sina=http://www.sina.com.cn
blogjava=http://www.blogjava.net
在properties中保存最多的是jdbc中的参数如jdbcdriver,url,user和password,
jdbcdriver=com.mysql.jdbc.Driver
jdbcurl=jdbc:mysql://[host:port],[host:port].../[database]
jdbcuser=mysql
jdbcpassword=root
Java中提供了一个java.util.Properties工具类,使用Properties类您可以方便的从一个.properties属性文件中读取设置参数,示例代码如下:
Properties props = new Properties(); ==========================>Properties为一个Map集合
props.load(new FileInputStream("filename.properties")); ================>使用的是绝对路径
String value = props.getProperty("propertyname");
如果您的.properties文件打包入一个Jar或War文件,您可以使用ClassLoader的getResourceAsStream()方法得到一个InputStream对象,示例代码如下:
Properties props = new Properties();
props.load(getClass().getResourceAsStream("com/company/application/application.properties"));
================>使用的是虚拟路径,虚拟路径相对于工程来说的,一般相对于src文件
String value = props.getProperty("propertyname")
==============================================================================================================
详细用法:
//根据key读取value
public static String readValue(String filePath,String key) {
Properties props = new Properties();
try {
InputStream in = new BufferedInputStream (new FileInputStream(filePath));
props.load(in);
String value = props.getProperty (key);
System.out.println(key+value);
return value;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
//读取properties的全部信息
public static void readProperties(String filePath) {
Properties props = new Properties();
try {
InputStream in = new BufferedInputStream (new FileInputStream(filePath));
props.load(in);
Enumeration en = props.propertyNames(); //propertyNames()方法返回一枚举的集合
while (en.hasMoreElements()) {
String key = (String) en.nextElement();
String Property = props.getProperty (key);
System.out.println(key+Property);
}
} catch (Exception e) {
e.printStackTrace();
}
}
//写入properties信息
public static void writeProperties(String filePath,String parameterName,String parameterValue) {
Properties prop = new Properties();
try {
InputStream fis = new FileInputStream(filePath);
prop.load(fis);
OutputStream fos = new FileOutputStream(filePath);
prop.setProperty(parameterName, parameterValue);
prop.store(fos, "Update '" + parameterName + "' value");
} catch (IOException e) {
System.err.println("Visit "+filePath+" for updating "+parameterName+" value error");
}
}