该文主要记录通过properties来读取配置文件。简单介绍了两种方法。源代码下载地址
方法一:使用p.load(ClassGetResourceAsStream.class.getResourceAsStream(“conf/sd.properties”));方法加载配置文件
代码如下:
package test.com.sd.properties;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.Properties;
public class ClassGetResourceAsStream {
public static void main(String[] args) {
// TODO Auto-generated method stub
UseProperties up=new UseProperties();
up.useProperties1();
}
public void useProperties1(){
//File的根路径是项目名File f=new File("src/conf/sd.properties");
//而/conf/crawler.properties的跟人路径是src:class.getResourceAsStream("/conf/sd.properties");
Properties p=new Properties();
try {
p.load(ClassGetResourceAsStream.class.getResourceAsStream("conf/sd.properties"));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//File f=new File(src/conf/sd.properties);
if(p.containsKey("sd")){
System.out.println(p.getProperty("sd"));
/*String s=null;
try {
s = new String(p.getProperty("sd").getBytes("ISO-8859-1"),"UTF-8");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(s);*/
}
}
}
输出结果:
中文需要转码
方法二:使用
isr=new InputStreamReader(new FileInputStream(f));
p.load(isr);
加载配置文件
package test.com.sd.properties;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.Map.Entry;
import java.util.Properties;
public class UseProperties {
public static void main(String[] args) {
// TODO Auto-generated method stub
UseProperties up=new UseProperties();
up.useProperties1();
}
private void useProperties1(){
//File的根路径是项目名File f=new File("src/conf/sd.properties");
//而/conf/crawler.properties的跟人路径是src:class.getResourceAsStream("/conf/sd.properties");
//或File f=new File("conf1/sd.properties");
File f=new File("src/conf/sd.properties");
Properties p=new Properties();
//FileInputStream fi;
InputStreamReader isr;
try {
//无需转码
isr=new InputStreamReader(new FileInputStream(f));
p.load(isr);
//也可用下面的方法但要转码
/*fi=new FileInputStream(f);
p.load(fi);*/
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//File f=new File(src/conf/sd.properties);
if(p.containsKey("sd")){
System.err.println(p.getProperty("sd"));
//可以通过下面转码
/*
String s=null;
try {
s = new String(p.getProperty("sd").getBytes("ISO-8859-1"),"UTF-8");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(s);*/
}
//下面是几种遍历配置文件的方法,不刚兴趣的不用看仅自己练习才写的
Enumeration enu=p.propertyNames();
while(enu.hasMoreElements()){
String key=(String) enu.nextElement();
String value=p.getProperty(key);
System.out.println("enumeration遍历方法:"+key+"---"+value);
//不能像下面这么输出不然输出内容会有误:enu.nextElement()了两回然从而会使得key对应到下一个key值的value。
//System.out.println(enu.nextElement()+":"+properties.getProperty((String)enu.nextElement()));
}
Iterator<Entry<Object,Object>> ite=p.entrySet().iterator();
while(ite.hasNext()){
String key=(String) ite.next().getKey();
//不能想下面这样不然犯上面同样的错
//String value=(String) ite.next().getValue();
String value=p.getProperty(key);
System.err.println("Iterator遍历方法:"+key+"---"+value);
}
for(Entry<Object,Object> entry:p.entrySet()){
System.out.println("entry遍历方法:"+entry.getKey()+"---"+entry.getValue());
}
}
}
输出结果:
中文需要转码
enumeration遍历方法:ss—231lkj
enumeration遍历方法:sd—中文需要转码
enumeration遍历方法:sm—sdfadf
Iterator遍历方法:ss—231lkj
Iterator遍历方法:sd—中文需要转码
Iterator遍历方法:sm—sdfadf
entry遍历方法:ss—231lkj
entry遍历方法:sd—中文需要转码
entry遍历方法:sm—sdfadf