视频1 视频21 视频41 视频61 视频文章1 视频文章21 视频文章41 视频文章61 推荐1 推荐3 推荐5 推荐7 推荐9 推荐11 推荐13 推荐15 推荐17 推荐19 推荐21 推荐23 推荐25 推荐27 推荐29 推荐31 推荐33 推荐35 推荐37 推荐39 推荐41 推荐43 推荐45 推荐47 推荐49 关键词1 关键词101 关键词201 关键词301 关键词401 关键词501 关键词601 关键词701 关键词801 关键词901 关键词1001 关键词1101 关键词1201 关键词1301 关键词1401 关键词1501 关键词1601 关键词1701 关键词1801 关键词1901 视频扩展1 视频扩展6 视频扩展11 视频扩展16 文章1 文章201 文章401 文章601 文章801 文章1001 资讯1 资讯501 资讯1001 资讯1501 标签1 标签501 标签1001 关键词1 关键词501 关键词1001 关键词1501 专题2001
java对properties文件的操作
2025-09-23 10:40:53 责编:小OO
文档
对properties文件的操作

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

   资源文件妨碍classpath下,即工程项目的class包下

2. 获取系统资源文件的方式有2中

   a.  通过  InputStream inputstream = ClassLoader.getSystemResourceAsStream("info.properties"); 

   b. 通过 InputStream inputstream = this.getClass().getResourceAsStream("/info.properties");

采用第一种方式获取资源文件时,文件不以"/" 开头,而采用方法b的话,文件必须"/"开头

3. 提取加载资源文件的信息

Java代码

Properties properties = new Properties();

InputStream inputstream = ClassLoader.getSystemResourceAsStream("info.properties");

// InputStream inputstream = this.getClass().getResourceAsStream("/info.properties");

 

properties.load(inputstream);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. 获取所有的键值对的信息

    

Java代码

Enumeration enumvalue = (Enumeration) properties.propertyNames();// 返回属性列表中所有键的枚举,如果在主属性列表中未找到同名的键,则包括默认属性列表中不同的键

while (enumvalue.hasMoreElements())

 {

      String key = enumvalue.nextElement();

      System.out.println(key + " : " + properties.getProperty(key));

 } Enumeration enumvalue = (Enumeration) properties.propertyNames();// 返回属性列表中所有键的枚举,如果在主属性列表中未找到同名的键,则包括默认属性列表中不同的键

 while (enumvalue.hasMoreElements())

 {

      String key = enumvalue.nextElement();

      System.out.println(key + " : " + properties.getProperty(key));

 }
 

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

        

Java代码

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();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. 删除相关的键值对

      

Java代码

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();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(); 

下载本文
显示全文
专题