Java配置文件读取
Java配置文件读取有各种不同的文件,但是由于打包Jar后的路径改变,往往在项目中能正确读取的配置文件在Jar后变成文件不存在的杯具,下在提出几各不同的配置文件读取方式,仅供参考
一、直接文件读取
File f = new File("you config file path");
FileReader fr = new FileReader(f);
BufferReader br = new BufferReader(fr);
String str = br.readLine();
String para = null;
while(str != null){
//here to get the parameter in the line by key
String para = getPara(key);
return para ;
}
这种方式的好处是配置文件可以自己想要的方式存在,如ini文件,如key = value,或key:value等,只要自己能解释。
但,由于路径的问题,和读取权限,往往往往在项目中能正确读取的配置文件在Jar后变成文件不存在的杯具
二、采用Properties
DataInputStream dis = ClassLoader.getResourceAsStream("your config file path");
Properties p = new Properties() ;
p.load(dis );
//here get the para value by key
getResource(key) ;
这种方法减少了一中取每个属性带来和繁杂过程,但依然无法解决路径变化的问题
三、自动寻找,自动读取
ResourceBundle rb = ResourceBundle.getBundle("your config file name");
//get value of key
rb.getString(key);
这种方式自动寻找配置文件,自动读取属性
三、bean模式
1.利用ClassPathXmlApplicationContext
ApplicationContext context = new ClassPathXmlApplicationContext("beanConfig.xml");HelloBean helloBean = (HelloBean)context.getBean("helloBean");System.out.println(helloBean.getHelloWorld());
2.利用FileSystemResource读取
Resource rs = new FileSystemResource("D:/software/tomcat/webapps/springWebDemo/WEB-INF/classes/beanConfig.xml"); BeanFactory factory = new XmlBeanFactory(rs); HelloBean helloBean = (HelloBean)factory.getBean("helloBean"); System.out.println(helloBean.getHelloWorld());
相关文档:
1.对collections的支持
Java代码
List<String> list = new ArrayList<String>();
list.add("item");
String item = list.get(0);
Set<String> set = new HashSet<String>(); &nb ......
(一)过滤器类编写。
1、设置字符集编码方式:
编写过滤器类:实现接口javax.servlet.Filter
public class CharacterEncodingFilter implements Filter {
private String charset;
public void destroy() {
// TODO Auto-generated method stub
}
public void ......
中小
Java中共有三个移位操作符,分别是:
<<:左移操作,所有操作数向左移动,每移一位最右边用0补充
>>:带符号位右移:连同符号位一起右移,每移一位最左边用符号位补充
>>>:无符号右移:连同符号位一起右移,每移一位最左边用0补充
移位操作符只能作用于整数类型,即byte,short,char,i ......