读取properties配置文件
来源:互联网 发布:阿里云怎么退款 编辑:程序博客网 时间:2024/06/11 14:57
有的时候我们需要把一些url信息保存在一个properties文件中。方便统一修改和管理。
这时就需要一个类文件来读取properties的内容。ps:properties文件一般放在resource目录下。代码如下:
import java.io.*;import java.util.Properties;/** * Created by Administrator on 2016/4/23. */public class PropertiesUtil { private String propertiesName="";//properties文件的名称 public PropertiesUtil() { } public PropertiesUtil(String propertiesName) {//有参构造,创建这个对象的时候就把properties文件的名称带入 this.propertiesName = propertiesName; }<span style="white-space:pre"></span>//这是读取方法。 public String readProperty(String key){ String value=""; InputStream is=null; try { is=PropertiesUtil.class.getClassLoader().getResourceAsStream(propertiesName); Properties p=new Properties(); p.load(is); value=p.getProperty(key); } catch (IOException e) { e.printStackTrace(); }finally { try { is.close(); } catch (IOException e) { e.printStackTrace(); } } return value; }//这时获得properties对象的方法。 public Properties getProperties(){ Properties p=new Properties(); InputStream is=null; try{ is=PropertiesUtil.class.getClassLoader().getResourceAsStream(propertiesName); p.load(is); }catch (Exception e){ e.printStackTrace(); }finally{ try { is.close(); } catch (IOException e) { e.printStackTrace(); } } return p; }//写入properties键值对的方法 public void writeProperty(String key,String value){ InputStream is=null; OutputStream os=null; Properties p=new Properties(); try { is=new FileInputStream(propertiesName); p.load(is); os=new FileOutputStream(Properties.class.getClassLoader().getResource(propertiesName).getFile()); p.setProperty(key,value); p.store(os,key); os.flush(); os.close(); } catch (Exception e) { e.printStackTrace(); }finally { try { if(null!=is){ is.close(); } if(null !=os){ os.close(); } } catch (IOException e) { e.printStackTrace(); } } } public static void main(String[] args) { PropertiesUtil p=new PropertiesUtil("server.properties");//创建一个工具类对象 Properties ps=p.getProperties();//得到properties对象。 System.out.println(ps.getProperty("mp"));//根据键得到值。 }}--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
properties文件内容:
mp=zhangsan
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
0 0
- java 读取properties配置文件
- Java读取properties配置文件
- 读取properties配置文件
- java读取properties配置文件
- java 读取配置文件properties
- 读取.properties配置文件
- java 读取*.properties配置文件
- Java读取properties配置文件
- java 读取配置文件(properties)
- java读取 .properties配置文件
- Properties 读取配置文件
- java读取properties配置文件
- Properties配置文件读取
- spring 读取properties配置文件
- 读取Properties配置文件内容
- 读取.properties配置文件
- birt读取配置文件.properties
- Java读取properties配置文件
- 二分查找,递归和非递归
- linux 安装 solr
- java(优化13) java高并发量网站解决方案
- 使用ab对nginx进行压力测试
- ListView 选择模式
- 读取properties配置文件
- hdu 5672 String【尺取法】
- 【NOIP practice】BSOJ 1947 编码 递推
- SQList的操作
- 【读书笔记】Android安全机制解析与应用实践之安卓的启动
- 2016年4月21日百度笔试题--ShortestJob
- Android-自定义PopupWindow
- C关键字Register
- 基于GDAL的栅格数据/遥感影像IO (非分块)