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());
相关文档:
Cavaj Java Decompiler is a graphical freeware utility that reconstructs Java source code from CLASS files. You can decompile Java applets, JAR and ZIP files, producing accurate Java source code. Browse the reconstructed source code with the Class View for instant access to methods and fields. The pr ......
MD5即Message-Digest Algorithm 5(信息-摘要算法5),是一种用于产生数字签名的单项散列算法,在1991年由MIT Laboratory for Computer Science(IT计算机科学实验室)和RSA Data Security Inc(RSA数据安全公司)的Ronald L. Rivest教授开发出来,经由MD2、MD3和MD4发展而来。MD5算法的使用不需要支付任何版权费用。它的作 ......
前些天和一个C++程序员聊天时,聊到一些对象、数组存储空间时,由于两人都不是大鸟,之前也看过一些java存储数据的资料,但都是过目便忘。也没有进行总结过。下面是转载过来,感觉总结的很全面。
java对象内存机制:http://java.chinait ......
线程
1---锁对象的方法----obj.wait()----obj.notify()----针对当前线程
& ......
jar -cvf name.jar *.*(打包此目录下所有文件)
jar -cvf name.jar filename(打包此目录下单个文件helloWorld.java或文件夹)
jar -cvf name.jar filename1 filename2....(打包此目录下多个文件或文件夹)
参考: jar ......