Java工具类之配置文件读取
2016-02-20 15:18
721 查看
在项目中,经常会用到很多的工具类,比如读取配置文件,上传下载,导入导出,json解析,排序等一些工具类。以前用到了很多,但是没有记录,等到下次工作的时候,要么就是在网上找,要么就是问同事要。现在准备总结一下这些工具类,以后好用到。
Java读取配置文件一使用properties类读取:
使用Spring去读取配置文件
Java读取配置文件一使用properties类读取:
public static Properties getProperty(String URL){ Properties properties = new Properties(); String path = System.getProperty("global.config.path") + URL; FileInputStream fileInputStream=null; try { fileInputStream=new FileInputStream(path); InputStreamReader is = new InputStreamReader(fileInputStream, "UTF-8"); properties.load(is); return properties; } catch (Exception e) { throw new RuntimeException( path + " is not exist."); }finally{ try { fileInputStream.close(); } catch (IOException e) { e.printStackTrace(); } } }使用InputStreamReader流,主要是为了可以转换字符编码,以防乱码;
使用Spring去读取配置文件
<bean id="dcenterCommonPropertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="ignoreUnresolvablePlaceholders" value="true"/> <property name="fileEncoding" value="UTF-8" /> <property name="locations"> <list> <value>file:${global.config.path}/back-order/back-order-service/service.properties</value> <value>file:${global.config.path}/back-order/back-order-service/jobconfig.properties</value> </list> </property> </bean>在我们的VO层只需要通过注解就可以注入了;
@Component("BackOrderLogisticsConfig") public class BackOrderLogisticsConfig { @Value("${kuaidi100.id}") private String id;// 注册的key值 @Value("${kuaidi100.url}") private String url;// 注册的url地址 @Value("${kuaidi100.show}") private String show;// 返回值类型 0:json,1:返回xml对象,2:返回html对象,3:返回text文本。 @Value("${kuaidi100.muti}") private String muti;// 返回的行数 @Value("${kuaidi100.order}")
相关文章推荐
- Eclipse如何使用多个console?
- java ThreadPoolExecutor 关键代码分析
- hbase-0.94 Java API
- JAVA EE(8)
- eclipse下的spring环境配置
- Spring学习二、IOC学习
- java核心技术学习笔记(三)—GregorianCalendar
- struts2中从数据库中读取数据,并在JSP页面中遍历保存有JavaBean对象的List对象
- 转载 java定时器时间配置
- spring mvc 的配置 (mvc 和servlet<action>的配置)
- JAVA 强制类型转换错误提示
- spring mvc 的两种配置方式继承controller和xml配置delegate
- 用JAVA写的插入排序
- String类型
- windows下,用eclipse连接虚拟机中的hadoop
- Java并发读取List2
- springMVC搭建
- JAVA集合类总结
- SpringMVC项目使用Thymeleaf模板引擎
- 浅析Java内存分配机制