请大神帮我看看这段用java写的读取配置文件properties代码写的有没有错误,能不能优化,谢谢!
importjava.io.BufferedInputStream;importjava.io.FileInputStream;importjava.io.FileNot...
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
public class DataConnProperties {
private DataConnProperties(){}
private static DataConnProperties instance=new DataConnProperties();
public static DataConnProperties getInstance(){
return instance;
}
/**
* 根据路径以及参数列表读取参数
* 可读取的参数值将放入HashMap
* @param path 路径
* @param props 需要读取的参数
* @return HashMap
*/
public HashMap<String, String> getproperties(String path,ArrayList<String> props){
HashMap<String, String> bprops=new HashMap<String, String>();
String prop;
Properties properties=new Properties();
try {
InputStream in=new BufferedInputStream(new FileInputStream(path));
properties.load(in);
for(String s:props){
prop=properties.getProperty(s);
if (prop!=null){
bprops.put(s, prop);
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return bprops;
}
}
需不需要分装一个类 属性包含hashmap和数组errers 把不能读取的字段加入到errers中
参数优化成了数组 展开
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
public class DataConnProperties {
private DataConnProperties(){}
private static DataConnProperties instance=new DataConnProperties();
public static DataConnProperties getInstance(){
return instance;
}
/**
* 根据路径以及参数列表读取参数
* 可读取的参数值将放入HashMap
* @param path 路径
* @param props 需要读取的参数
* @return HashMap
*/
public HashMap<String, String> getproperties(String path,ArrayList<String> props){
HashMap<String, String> bprops=new HashMap<String, String>();
String prop;
Properties properties=new Properties();
try {
InputStream in=new BufferedInputStream(new FileInputStream(path));
properties.load(in);
for(String s:props){
prop=properties.getProperty(s);
if (prop!=null){
bprops.put(s, prop);
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return bprops;
}
}
需不需要分装一个类 属性包含hashmap和数组errers 把不能读取的字段加入到errers中
参数优化成了数组 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询