最新文章专题视频专题问答1问答10问答100问答1000问答2000关键字专题1关键字专题50关键字专题500关键字专题1500TAG最新视频文章推荐1 推荐3 推荐5 推荐7 推荐9 推荐11 推荐13 推荐15 推荐17 推荐19 推荐21 推荐23 推荐25 推荐27 推荐29 推荐31 推荐33 推荐35 推荐37视频文章20视频文章30视频文章40视频文章50视频文章60 视频文章70视频文章80视频文章90视频文章100视频文章120视频文章140 视频2关键字专题关键字专题tag2tag3文章专题文章专题2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章专题3
当前位置: 首页 - 正文

java对properties文件的操作

来源:动视网 责编:小OO 时间:2025-09-23 10:40:53
文档

java对properties文件的操作

对properties文件的操作1.资源文件所存放的位置  资源文件妨碍classpath下,即工程项目的class包下2.获取系统资源文件的方式有2中  a. 通过 InputStreaminputstream=ClassLoader.getSystemResourceAsStream("info.properties");  b.通过InputStreaminputstream=this.getClass().getResourceAsStream("/info.properties");
推荐度:
导读对properties文件的操作1.资源文件所存放的位置  资源文件妨碍classpath下,即工程项目的class包下2.获取系统资源文件的方式有2中  a. 通过 InputStreaminputstream=ClassLoader.getSystemResourceAsStream("info.properties");  b.通过InputStreaminputstream=this.getClass().getResourceAsStream("/info.properties");
对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(); 

文档

java对properties文件的操作

对properties文件的操作1.资源文件所存放的位置  资源文件妨碍classpath下,即工程项目的class包下2.获取系统资源文件的方式有2中  a. 通过 InputStreaminputstream=ClassLoader.getSystemResourceAsStream("info.properties");  b.通过InputStreaminputstream=this.getClass().getResourceAsStream("/info.properties");
推荐度:
  • 热门焦点

最新推荐

猜你喜欢

热门推荐

专题
Top