一个成功执行的事例:
package com.test.properties;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
public class TestProperties {
public static void main(String[] args) {
Properties configProps = new Properties();
String configFile = "com/test/properties/info.properties";
try {
// Config File must be in classpath
ClassLoader cl = Thread.currentThread().getContextClassLoader();
InputStream in = cl.getResourceAsStream(configFile);
configProps.load(in);
in.close();
} catch (IOException e) {
e.printStackTrace();
}
/**
* 获取绝对URI路径
* Thread.currentThread().getContextClassLoader().getResource("")
* 结果为:file:/C:/Documents%20and%20Settings/dashan.yin/workspace/createpdf/WebRoot/WEB-INF/classes/
*/
System.out.println(Thread.currentThread().getContextClassLoader().getResource(""));
System.out.println(configProps.getProperty("url"));
}
}
info.properties文件:
url=http://localhost:8081/webmonitor
读取Properties文件读取,路径,空格,中文问题读取Properties文件和路径问题
如:读取jdbc.properties文件
路径
读取的类位于:package com.query.util;
getClass().getResourceAsStream(jdbc.properties)
则jdbc.properties描述的路径是相对于这个类所在包的根路径而言的,
即为相对于文件夹util所在目录开始,
getClass().getResourceAsStream(/jdbc.properties) 以/开始
则/jdbc.properties描述的路径是相对于这各类当前的文件夹而言的,
即为相对于文件夹com所在目录开始,
读取的方法
1。使用java.util.Properties类的load()方法
示例: InputStream in = lnew BufferedInputStream(new FileInputStream(name));
Properties p = new Properties();
p.load(in);
2。使用java.util.ResourceBundle类的getBundle()方法
示例: ResourceBundle rb = ResourceBundle.getBundle(name, Locale.getDefault());
3。使用java.util.PropertyResourceBundle类的构造函数
示例: InputStream in = new BufferedInputStream(new FileInputStream(name));
ResourceBundle rb = new PropertyResourceBundle(in);
4。使用class变量的getResourceAsStream()方法
示例: InputStream in = JProperties.class.getResourceAsStream(name);
Properties p = new Properties();
p.load(in);
5。使用class.getClassLoader()所得到的java.lang.ClassLoader的getResourceAsStream()方法
示例: InputStream in = JProperties.class.getClassLoader().getResourceAsStream(name);
Properties p = new Properties();
p.load(in);
6。使用java.lang.ClassLoader类的getSystemResourceAsStream()静态方法
示例: InputStream in = ClassLoader.getSystemResourceAsStream(name);
Properties p = new Properties();
p.load(in);
补充
Servlet中可以使用javax.servlet.ServletContext的getResourceAsStream()方法
示例:InputStream in = context.getResourceAsStream(path);
Properties p = new Properties();
p.load(in);
一般常使用第五种
空格
//获得文件路径,并对路径进行处理
private static String getUrl()
{
String path = configLoad.class.getResource("config.properties").toString();
path = path.replace("%20", " "); //引号中有一个半角的空格
path = path.substring(6);
return path;
}
}
那么这里返回了一个Properties类型的值,在这里就可以使用getProperty()来获得值
如:Properties pro = configLoad.getConfig();
String http = pro.getProperty("url").toString();
另一种实现方式:
1. 资源文件所存放的位置
资源文件妨碍classpath下,即工程项目的class包下
2. 获取系统资源文件的方式有2中
a. 通过 InputStream inputstream = ClassLoader.getSystemResourceAsStream("info.properties");
b. 通过 InputStream inputstream = this.getClass().getResourceAsStream("/info.properties");
采用第一种方式获取资源文件时,文件不以"/" 开头,而采用方法b的话,文件必须"/"开头
3. 提取加载资源文件的信息
Properties properties = new Properties();
InputStream inputstream = ClassLoader.getSystemResourceAsStream("info.properties");
// InputStream inputstream = this.getClass().getResourceAsStream("/info.properties");
properties.load(inputstream);
4. 操作资源文件
a. 根据key值在资源文件中查询value值
1. getProperty(String key) 用指定的键在此属性列表中搜索属性。
2. getProperty(String key, String defaultValue) 用指定的键在属性列表中搜索属性。
b. 获取所有的键值对的信息
Enumeration<String> enumvalue = (Enumeration<String>) properties.propertyNames();// 返回属性列表中所有键的枚举,如果在主属性列表中未找到同名的键,则包括默认属性列表中不同的键
while (enumvalue.hasMoreElements())
{
String key = enumvalue.nextElement();
System.out.println(key + " : " + properties.getProperty(key));
}
c. 向资源文件中添加键值信息,如果key值相同就会将原有的信息覆盖
URL url = ClassLoader.getSystemResource("info.properties");
File file = new File(url.toURI());
InputStream is = new FileInputStream(file);
properties.load(is);
properties.setProperty("key", "value");
OutputStream fos = new FileOutputStream(file);
properties.store(fos, null);
fos.flush();
is.close();
d. 删除相关的键值对
File file = new File(ClassLoader.getSystemResource("info.properties").toURI());
InputStream is = new FileInputStream(file);
properties.load(is);
properties.remove("key");
OutputStream fos = new FileOutputStream(file);
properties.store(fos, null);
is.close();
fos.flush();
fos.close();