• Properties文件和Properties类
    • 是什么
    • Properties文件
    • Properties类
      • 简述
      • 构造方法
      • 基本的存储方法
      • 与流相关的方法

Properties文件和Properties类

是什么

Properties文件 Java一种以 .properties结尾的常见配置文件
Properties类 一个持久的属性集。Properties 可保存在流中或从流中加载。

简单来说,我们可以利用Properties类所创建的对象来将Properties文件中的配置信息读取出来。

Properties文件

Properties继承自Hashtable,是由一组key-value的集合

properties文件的书写要求总结:
1、注释内容由 # 或者! 开头, 如果# 或者!不在开头,则不作为注释
2、key,value之间用 = 或者 : 分隔。一行中既有=也有:时,第一个(或者=或者:)将作为key,value分隔符。
3、key 不能换行,value可以换行,换行符是\ ,且换行后的\t、空格都会忽略。

Properties类

简述

java.util.Properties 继承于Hashtable ,来表示一个持久的属性集。它使用键值结构存储数据,每个键及其对应值都是一个字符串。该类也被许多Java类使用,比如获取系统属性时,System.getProperties 方法就是返回一个Properties对象。

构造方法

  • public Properties() :创建一个空的属性列表。

基本的存储方法

  • public Object setProperty(String key, String value) : 保存一对属性。
  • public String getProperty(String key) :使用此属性列表中指定的键搜索属性值。
  • public Set<String> stringPropertyNames() :所有键的名称的集合。
public class ProDemo {public static void main(String[] args) throws FileNotFoundException {// 创建属性集对象Properties properties = new Properties();// 添加键值对元素properties.setProperty("filename", "a.txt");properties.setProperty("length", "209385038");properties.setProperty("location", "D:\\a.txt");// 打印属性集对象System.out.println(properties);// 通过键,获取属性值System.out.println(properties.getProperty("filename"));System.out.println(properties.getProperty("length"));System.out.println(properties.getProperty("location"));// 遍历属性集,获取所有键的集合Set<String> strings = properties.stringPropertyNames();// 打印键值对for (String key : strings ) {System.out.println(key+" -- "+properties.getProperty(key));}}
}
输出结果:
{filename=a.txt, length=209385038, location=D:\a.txt}
a.txt
209385038
D:\a.txt
filename -- a.txt
length -- 209385038
location -- D:\a.txt

与流相关的方法

  • public void load(InputStream inStream): 从字节输入流中读取键值对。

参数中使用了字节输入流,通过流对象,可以关联到某文件上,这样就能够加载文本中的数据了。文本数据格式:

filename=a.txt
length=209385038
location=D:\a.txt

加载代码演示:

public class ProDemo2 {public static void main(String[] args) throws FileNotFoundException {// 创建属性集对象Properties pro = new Properties();// 加载文本中信息到属性集pro.load(new FileInputStream("read.txt"));// 遍历集合并打印Set<String> strings = pro.stringPropertyNames();for (String key : strings ) {System.out.println(key+" -- "+pro.getProperty(key));}}
}
输出结果:
filename -- a.txt
length -- 209385038
location -- D:\a.txt

TIP:文本中的数据,必须是键值对形式,可以使用空格、等号、冒号等符号分隔。

Properties文件和Properties类相关推荐

  1. properties文件html,properties的中文意思

    java中的Properties是什么类呢,什么意思呢 在cad中PROPEIES是什么意思 PROPEIES在CAD中是特性的意思,快捷输入有"PR或MO",快捷键"C ...

  2. linux properties文件,读取Properties文件六种方法

    开发项目时,经常把一些参数存入Properties文件,以增加程序的灵活性.所以读取properties文件可以说是我们的java基础.我们可以通过以下六种方法读取配置参数(注意:spring对pro ...

  3. java获取properties属性_java工具类中获取properties文件的属性

    记录获取properties文件属性的工具类 package org.jasig.cas.mylogin.util; import org.slf4j.Logger; import org.slf4j ...

  4. Properties类和properties文件的简单总结

    目录 一.properties文件 二.Properties类 1. Properties类的理解 2. 常用方法Properties类 (1) 存储键值对 (2) 通过key获取对应的value ( ...

  5. java中读取properties文件内容五种方式

    一.背景 最近,在项目开发的过程中,遇到需要在properties文件中定义一些自定义的变量,以供java程序动态的读取,修改变量,不再需要修改代码的问题.就借此机会把Spring+SpringMVC ...

  6. Java项目中读取properties文件,以及六种获取路径的方法

    下面1-4的内容是网上收集的相关知识,总结来说,就是如下几个知识点: 最常用读取properties文件的方法 InputStream in = getClass().getResourceAsStr ...

  7. 用java读取properties文件--转

    今天为了通过java读取properties文件,google了很长时间,终于找到了.现在特记录之和大家一起分享.      下面直接贴出代码:java类 public class Mytest pu ...

  8. java对文件的操作详解_Java 对 Properties 文件的操作详解及简单实例

    Java 对 Properties 文件的操作详解及简单实例 发布于 2020-8-7| 复制链接 摘记: Java 对 Properties 文件的操作简介在 Java 中,我们常用 java.ut ...

  9. 五种方式让你在java中读取properties文件内容不再是难题

    2019独角兽企业重金招聘Python工程师标准>>> 方式1.通过context:property-placeholder加载配置文件jdbc.properties中的内容 < ...

最新文章

  1. modlesim使用
  2. hibernate教程--检索方式(hql,sql,QBC)
  3. linux下更换pip源
  4. 基于苹果自研芯片的 Mac 电脑对安全意味着什么?
  5. python控制浏览器模拟鼠标点击网页标题_如何使用python来模拟鼠标点击(将经过实例自动化模拟在360浏览器中自动搜索python)...
  6. eclipse中的TODO和FIXME
  7. linux bash错误,linux bash错误重定向输出
  8. cocos2dx打飞机项目笔记一:项目结构介绍
  9. 信息系统管理——项目立项管理(详细可行性研究的结构)
  10. 修改域名dns服务器地址,易名中国域名如何修改DNS设置方法
  11. python编写函数求和_Python定义函数实现累计求和操作
  12. 手机word文档docx密码忘了怎么办,忘记word文档docx密码怎么办?
  13. 为何风口过去之后,百果园反而要在无人零售上发力?
  14. 求关系模式的候选码的方法
  15. Android图文混排
  16. RxSwift 学习笔记
  17. 腾讯2018秋招笔试真题(2)
  18. 杂记-----------
  19. 正则啊啊 啊啊啊啊啊啊 啊
  20. 我以过来人的身份告诉你手工测试人员如何转测试开发?

热门文章

  1. 【离散数学】集合论 第四章 函数与集合(3) 鸽巢原理
  2. 一看就懂的二叉查找树和平衡二叉查找树
  3. 【c++经典小游戏,源码奉上(免费复制)】
  4. swiper排旋转木马自动轮播
  5. 使用对抗样本改善图像分类性能
  6. netflix会员和非会员的区别_Netflix会员低价购买方法
  7. CrossOver22全新版功能简介 免费mac虚拟机工具
  8. Sonar Scanner 分析参数:即如何配置sonar-project.properties文件
  9. selinux关闭不重启系统
  10. Redis集群——主从配置