博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java web读取属性文件的几种方式总结
阅读量:4053 次
发布时间:2019-05-25

本文共 4488 字,大约阅读时间需要 14 分钟。

 

一个成功执行的事例:

package com.test.properties;import java.io.IOException;import java.io.InputStream;import java.util.Properties;public class TestProperties { public static void main(String[] args) {  Properties configProps = new Properties();  String configFile = "com/test/properties/info.properties";  try {   // Config File must be in classpath   ClassLoader cl = Thread.currentThread().getContextClassLoader();   InputStream in = cl.getResourceAsStream(configFile);   configProps.load(in);   in.close();  } catch (IOException e) {   e.printStackTrace();  }  /**   * 获取绝对URI路径   * Thread.currentThread().getContextClassLoader().getResource("")   * 结果为:file:/C:/Documents%20and%20Settings/dashan.yin/workspace/createpdf/WebRoot/WEB-INF/classes/   */  System.out.println(Thread.currentThread().getContextClassLoader().getResource(""));    System.out.println(configProps.getProperty("url")); }}

info.properties文件:

url=http://localhost:8081/webmonitor

 

读取Properties文件读取,路径,空格,中文问题读取Properties文件和路径问题

  如:读取jdbc.properties文件
路径
读取的类位于:package com.query.util;
getClass().getResourceAsStream(jdbc.properties)
      则jdbc.properties描述的路径是相对于这个类所在包的根路径而言的,
   即为相对于文件夹util所在目录开始,
getClass().getResourceAsStream(/jdbc.properties) 以/开始
   则/jdbc.properties描述的路径是相对于这各类当前的文件夹而言的,
   即为相对于文件夹com所在目录开始,
读取的方法
1。使用java.util.Properties类的load()方法
示例: InputStream in = lnew BufferedInputStream(new FileInputStream(name));
       Properties p = new Properties();
       p.load(in);
2。使用java.util.ResourceBundle类的getBundle()方法
示例: ResourceBundle rb = ResourceBundle.getBundle(name, Locale.getDefault());
3。使用java.util.PropertyResourceBundle类的构造函数
示例: InputStream in = new BufferedInputStream(new FileInputStream(name));
       ResourceBundle rb = new PropertyResourceBundle(in);
4。使用class变量的getResourceAsStream()方法
示例: InputStream in = JProperties.class.getResourceAsStream(name);
       Properties p = new Properties();
       p.load(in);
5。使用class.getClassLoader()所得到的java.lang.ClassLoader的getResourceAsStream()方法
示例: InputStream in = JProperties.class.getClassLoader().getResourceAsStream(name);
       Properties p = new Properties();
       p.load(in);
6。使用java.lang.ClassLoader类的getSystemResourceAsStream()静态方法
示例: InputStream in = ClassLoader.getSystemResourceAsStream(name);
       Properties p = new Properties();
       p.load(in);
补充
Servlet中可以使用javax.servlet.ServletContext的getResourceAsStream()方法
示例:InputStream in = context.getResourceAsStream(path);
       Properties p = new Properties();
       p.load(in);
一般常使用第五种
空格
//获得文件路径,并对路径进行处理
private static String getUrl()
{
  String path = configLoad.class.getResource("config.properties").toString();
  path = path.replace("%20", " "); //引号中有一个半角的空格
  path = path.substring(6);
  return path;
}
}
那么这里返回了一个Properties类型的值,在这里就可以使用getProperty()来获得值
如:Properties pro = configLoad.getConfig();
String http = pro.getProperty("url").toString();

 

 

 

另一种实现方式:

1. 资源文件所存放的位置

   资源文件妨碍classpath下,即工程项目的class包下
2. 获取系统资源文件的方式有2中
   a.  通过  InputStream inputstream = ClassLoader.getSystemResourceAsStream("info.properties");
   b. 通过 InputStream inputstream = this.getClass().getResourceAsStream("/info.properties");
采用第一种方式获取资源文件时,文件不以"/" 开头,而采用方法b的话,文件必须"/"开头
3. 提取加载资源文件的信息

Properties properties = new Properties();     InputStream inputstream = ClassLoader.getSystemResourceAsStream("info.properties");   // InputStream inputstream = this.getClass().getResourceAsStream("/info.properties");       properties.load(inputstream);

4. 操作资源文件
   a. 根据key值在资源文件中查询value值
      1. getProperty(String key) 用指定的键在此属性列表中搜索属性。
      2. getProperty(String key, String defaultValue)   用指定的键在属性列表中搜索属性。
    
   b. 获取所有的键值对的信息

Enumeration
enumvalue = (Enumeration
) properties.propertyNames();// 返回属性列表中所有键的枚举,如果在主属性列表中未找到同名的键,则包括默认属性列表中不同的键 while (enumvalue.hasMoreElements()) { String key = enumvalue.nextElement(); System.out.println(key + " : " + properties.getProperty(key)); }

c. 向资源文件中添加键值信息,如果key值相同就会将原有的信息覆盖

URL url = ClassLoader.getSystemResource("info.properties");   File file = new File(url.toURI());              InputStream is = new FileInputStream(file);   properties.load(is);   properties.setProperty("key", "value");     OutputStream fos = new FileOutputStream(file);   properties.store(fos, null);     fos.flush();   is.close();

d. 删除相关的键值对

File file = new File(ClassLoader.getSystemResource("info.properties").toURI());   InputStream is = new FileInputStream(file);     properties.load(is);   properties.remove("key");     OutputStream fos = new FileOutputStream(file);   properties.store(fos, null);     is.close();   fos.flush();   fos.close();

 

 

转载地址:http://bltci.baihongyu.com/

你可能感兴趣的文章
最简单的使用redis自带程序实现c程序远程访问redis服务
查看>>
redis学习总结-- 内部数据 字符串 链表 字典 跳跃表
查看>>
iOS 对象序列化与反序列化
查看>>
iOS 序列化与反序列化(runtime) 01
查看>>
iOS AFN 3.0版本前后区别 01
查看>>
iOS ASI和AFN有什么区别
查看>>
iOS QQ侧滑菜单(高仿)
查看>>
iOS 扫一扫功能开发
查看>>
iOS app之间的跳转以及传参数
查看>>
iOS __block和__weak的区别
查看>>
Android(三)数据存储之XML解析技术
查看>>
Spring JTA应用之JOTM配置
查看>>
spring JdbcTemplate 的若干问题
查看>>
Servlet和JSP的线程安全问题
查看>>
GBK编码下jQuery Ajax中文乱码终极暴力解决方案
查看>>
Oracle 物化视图
查看>>
PHP那点小事--三元运算符
查看>>
解决国内NPM安装依赖速度慢问题
查看>>
Brackets安装及常用插件安装
查看>>
Centos 7(Linux)环境下安装PHP(编译添加)相应动态扩展模块so(以openssl.so为例)
查看>>