- 用 于
- 游戏术语
- 定 义
- 任何装饰、布置用的可移动物件
- 分 类
- 消耗品、装备品和任务品
- 中文名
- 道具
- 简 写
- PROPS
- 外文名
- PROPERTIES
-
2019-05-23 23:21:46
Properties
含义:
Properties是Map集合中的一个实现类
特点:
1.按键值对的形式存放元素,但键与值都是字符串 ;
2. Properties 可以直接操作文件,即可以把Properties存放的内容放置到文件中或者从文件中读取内容放置到Properties的容器中;
3. 是数据持久化的一种手段Properties作为容器的API操作
Properties props=new properties();//产生一个容器对象
1.从文件中读取数据:props.load(new FileInputStream("文件名"));//调用load方法直接把properties文件中的数据以键值对的形式读入到Properties对象中 //因为最终要返回一个Map对象,所以要先new出一个Map容器对象,准备用来装从Properties中拿过来的数据。 HashMap<String, StudentBean> stuMap = new HashMap<>(); //从Properties文件的数据结构可以看出来,所有的数据即在Properties的键当中又在值当中; 遍历键,通过键可以得到对应的值! Set 键 = props.keySet();//所有的Map都提供keySet方法获取所有的键,被装入到一个Set集合中! 再根据键值对进行遍历得出所有值: //因为Properties不支持泛型,所以只能交给Object for(Object 对应值 : 键) { 得到对应值进行相应格式的拆分,得到想要的值; }
2.将数据存储到文件中:
//产生一个Properties对象---作为一个容器,然后只有它可以操作Properties的数据存储到文件中 //最后把构造好的字符串以正确的键值对形式放入到props当中 props.setProperty(key, value); //调用Properties的store方法,将该Properties容器的数据自动写入到指定文件中 props.store(new FileOutputStream("students.properties"), " ");
props.setProperty(“键名”,“值”);//容器中放置对象
props.setProperty(“键名”,“值”);//修改数据要求键在数据中存在‘
props.getProperty(“键名”,“值”);//获取数据通过键获取值
props.remover(“键名”);//删除数据
props.size();//获取元素个数
遍历:
Set 键 = props.keySet();//遍历所有的键
Collection 值 = props.values();//遍历所有的值,得到所有的值放在值集合中更多相关内容 -
基于jQuery.i18n.properties 实现前端页面的资源国际化Demo 源码
2017-09-01 09:46:51该Demo源码是博文《基于jQuery.i18n.properties 实现前端页面的资源国际化》里面的源码Demo。博文地址:http://blog.csdn.net/aixiaoyang168/article/details/49336709。 可供下载学习使用。 -
Java中读取properties配置文件的八种方式总结
2020-12-29 14:01:04在做Java项目开发过程中,涉及到一些数据库服务连接配置、缓存服务器连接配置等,通常情况下我们会将这些不太变动的配置信息存储在以 .properties 结尾的配置文件中。当对应的服务器地址或者账号密码信息有所变动时...一、前言
在做
Java
项目开发过程中,涉及到一些数据库服务连接配置、缓存服务器连接配置等,通常情况下我们会将这些不太变动的配置信息存储在以.properties
结尾的配置文件中。当对应的服务器地址或者账号密码信息有所变动时,我们只需要修改一下配置文件中的信息即可。同时为了让Java
程序可以读取.properties
配置文件中的值,Java
的JDK
中提供了java.util.Properties
类可以实现读取配置文件。二、Properties类
Properties 类位于 java.util.Properties中,是Java 语言的处理配置文件所使用的类,其中的
xxx.Properties
类主要用于集中的持久存储Java
的配置文件内容,可以读取后缀是.properties
和.cfg
的配置文件。Properties继承了Hashtable 类,以Map 的形式进行放置值,
put(key,value)
和get(key)
,文本注释信息可以用"#"来注释。Properties 类表示了一个持久的属性集。Properties 可保存在流中或从流中加载。属性列表中每个键及其对应值都是一个字符串。
Properties 文件内容的格式是:键=值 形式,Key值不能够重复。 例如:
jdbc.driver=com.mysql.jdbc.Driver
- Properties类的继承关系图:
- Properties类的源码关系图:
- 主要方法介绍:
它提供了几个核心的方法:
-
getProperty ( String key): 用指定的键在此属性列表中搜索属性。也就是通过参数 key ,得到 key 所对应的 value。
-
load ( InputStream inStream): 从输入流中读取属性列表(键和元素对)。通过对指定的文件(比如说上面的 test.properties 文件)进行装载来获取该文件中的所有键 - 值对。以供 getProperty ( String key) 来搜索。
-
setProperty ( String key, String value) : 调用 Hashtable 的方法 put 。他通过调用基类的put方法来设置 键 - 值对。
-
store ( OutputStream out, String comments): 以适合使用 load 方法加载到 Properties 表中的格式,将此 Properties 表中的属性列表(键和元素对)写入输出流。与 load 方法相反,该方法将键 - 值对写入到指定的文件中去。
-
clear (): 清除所有装载的 键 - 值对。该方法在基类中提供。
三、Properties常用方法实践
Properties
类我们从文件的写入和读取来实践其具体用法,下面演示练习将以下数据库配置信息写入到jdbc.properties
文件中jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf8 jdbc.username=root jdbc.password=123456
- 项目目录结构如下
四、Java写入Properties
Properties类调用setProperty方法将键值对保存到内存中,此时可以通过getProperty方法读取,propertyNames方法进行遍历,但是并没有将键值对持久化到属性文件中,故需要调用store方法持久化键值对到属性文件中。
import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; import java.util.Properties; /** * @desc: 写入Mysql数据库了连接信息到jdbc.properties中 * @author: cao_wencao * @date: 2020-12-29 13:41 */ public class PropertiesStoreTest { public static void main(String[] args) { Properties properties = new Properties(); OutputStream output = null; try { output = new FileOutputStream("src/main/resources/jdbc.properties"); properties.setProperty("jdbc.driver", "com.mysql.jdbc.Driver"); properties.setProperty("jdbc.url","jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf8" ); properties.setProperty("jdbc.username", "root"); properties.setProperty("jdbc.password", "123456"); // 保存键值对到文件中 properties.store(output, "Thinkingcao modify"); } catch (IOException io) { io.printStackTrace(); } finally { if (output != null) { try { output.close(); } catch (IOException e) { e.printStackTrace(); } } } } }
输出结果,在resources目录下多一个文件jdbc.properties,内容如下:
#Thinkingcao modify #Tue Dec 29 13:43:48 CST 2020 jdbc.url=jdbc\:mysql\://localhost\:3306/mybatis?characterEncoding\=utf8 jdbc.username=root jdbc.driver=com.mysql.jdbc.Driver jdbc.password=123456
五、Java读取Properties
Java读取Properties文件的方法有很多,下面介绍8种方式,分别读取resource目录下的jdbc.properties和resource/config/application.properties。
- application.properties文件内容如下
minio.endpoint=http://localhost:9000 minio.accessKey=minioadmin minio.secretKey=minioadmin minio.bucketName=demo
1. 从当前的类加载器的getResourcesAsStream来获取
/** * 1. 方式一 * 从当前的类加载器的getResourcesAsStream来获取 * InputStream inputStream = this.getClass().getResourceAsStream(name) * * @throws IOException */ @Test public void test1() throws IOException { InputStream inputStream = this.getClass().getResourceAsStream("jdbc.properties"); Properties properties = new Properties(); properties.load(inputStream); properties.list(System.out); System.out.println("=============================================="); String property = properties.getProperty("jdbc.url"); System.out.println("property = " + property); }
2. 从当前的类加载器的getClassLoader().getResourcesAsStream来获取
/** * 2. 方式二 * 从当前的类加载器的getResourcesAsStream来获取 * InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream(name) * * @throws IOException */ @Test public void test2() throws IOException { InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("config/application.properties"); Properties properties = new Properties(); properties.load(inputStream); properties.list(System.out); System.out.println("=============================================="); String property = properties.getProperty("minio.endpoint"); System.out.println("property = " + property); }
3. 使用Class类的getSystemResourceAsStream静态方法 和使用当前类的ClassLoader是一样的
/** * 3. 方式三 * 使用Class类的getSystemResourceAsStream方法 和使用当前类的ClassLoader是一样的 * InputStream inputStream = ClassLoader.getSystemResourceAsStream(name) * * @throws IOException */ @Test public void test3() throws IOException { InputStream inputStream = ClassLoader.getSystemResourceAsStream("config/application.properties"); Properties properties = new Properties(); properties.load(inputStream); properties.list(System.out); System.out.println("=============================================="); String property = properties.getProperty("minio.endpoint"); System.out.println("property = " + property); }
4. 使用Spring-core包中的ClassPathResource读取
/** * 4. 方式四 * Resource resource = new ClassPathResource(path) * * @throws IOException */ @Test public void test4() throws IOException { Resource resource = new ClassPathResource("config/application.properties"); Properties properties = PropertiesLoaderUtils.loadProperties(resource); properties.list(System.out); System.out.println("=============================================="); String property = properties.getProperty("minio.endpoint"); System.out.println("property = " + property); }
5. 从文件中读取,new BufferedInputStream(InputStream in)
/** * 5. 方式五 * 从文件中获取,使用InputStream字节,主要是需要加上当前配置文件所在的项目src目录地址。路径配置需要精确到绝对地址级别 * BufferedInputStream继承自InputStream * InputStream inputStream = new BufferedInputStream(new FileInputStream(name) * 这种方法读取需要完整的路径,优点是可以读取任意路径下的文件,缺点是不太灵活 * @throws IOException */ @Test public void test5() throws IOException { InputStream inputStream = new BufferedInputStream(new FileInputStream("src/main/resources/config/application.properties")); Properties properties = new Properties(); properties.load(inputStream); properties.list(System.out); System.out.println("=============================================="); String property = properties.getProperty("minio.endpoint"); System.out.println("property = " + property); }
6.从文件中读取,new FileInputStream(String name)
/** * 6. 方式六 * 从文件中获取,使用InputStream字节,主要是需要加上当前配置文件所在的项目src目录地址。路径配置需要精确到绝对地址级别 * FileInputStream继承自InputStream * InputStream inputStream = new FileInputStream(name) * 这种方法读取需要完整的路径,优点是可以读取任意路径下的文件,缺点是不太灵活 * @throws IOException */ @Test public void test6() throws IOException { InputStream inputStream = new FileInputStream("src/main/resources/config/application.properties"); Properties properties = new Properties(); properties.load(inputStream); properties.list(System.out); System.out.println("=============================================="); String property = properties.getProperty("minio.endpoint"); System.out.println("property = " + property); }
7. 使用PropertyResourceBundle读取InputStream流
/** * 7. 方式七 * 使用InputStream流来进行操作ResourceBundle,获取流的方式由以上几种。 * ResourceBundle resourceBundle = new PropertyResourceBundle(inputStream); * @throws IOException */ @Test public void test7() throws IOException { InputStream inputStream = ClassLoader.getSystemResourceAsStream("config/application.properties"); ResourceBundle resourceBundle = new PropertyResourceBundle(inputStream); Enumeration<String> keys = resourceBundle.getKeys(); while (keys.hasMoreElements()) { String s = keys.nextElement(); System.out.println(s + " = " + resourceBundle.getString(s)); } }
8. 使用ResourceBundle.getBundle读取
/** * 8. 方式八 * ResourceBundle.getBundle的路径访问和 Class.getClassLoader.getResourceAsStream类似,默认从根目录下读取,也可以读取resources目录下的文件 * ResourceBundle rb = ResourceBundle.getBundle("b") //不需要指定文件名的后缀,只需要写文件名前缀即可 */ @Test public void test8(){ //ResourceBundle rb = ResourceBundle.getBundle("jdbc"); //读取resources目录下的jdbc.properties ResourceBundle rb2 = ResourceBundle.getBundle("config/application");//读取resources/config目录下的application.properties for(String key : rb2.keySet()){ String value = rb2.getString(key); System.out.println(key + ":" + value); } }
输出结果:
minio.endpoint:http://localhost:9000 minio.bucketName:demo minio.secretKey:minioadmin minio.accessKey:minioadmin
六、Properties配合Spring框架使用
- 加载.properties方式一
<!-- 1.加载 jdbc.properties 配置文件 --> <context:property-placeholder location="classpath:jdbc.properties" system-properties-mode="NEVER"/>
除了上面这种方式之外,还有下面这种List集合的方式
- 加载.properties方式二
<!-- 4.引入外部配置文件 由于后期可能会引入多个配置文件 所以采用list的形式 --> <bean id="propertyPlaceholder" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <value>classpath:/config/jdbc.properties</value> <value>classpath:/config/application.properties</value> </list> </property> </bean>
七、完整代码
import org.junit.Test; import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.Resource; import org.springframework.core.io.support.PropertiesLoaderUtils; import java.io.BufferedInputStream; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.util.*; /** * @desc: Properties读取配置文件属性值的方式 * @author: cao_wencao * @date: 2020-12-29 10:08 */ public class PropertiesTest { /** * 1. 方式一 * 从当前的类加载器的getResourcesAsStream来获取 * InputStream inputStream = this.getClass().getResourceAsStream(name) * * @throws IOException */ @Test public void test1() throws IOException { InputStream inputStream = this.getClass().getResourceAsStream("jdbc.properties"); Properties properties = new Properties(); properties.load(inputStream); properties.list(System.out); System.out.println("=============================================="); String property = properties.getProperty("jdbc.url"); System.out.println("property = " + property); } /** * 2. 方式二 * 从当前的类加载器的getResourcesAsStream来获取 * InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream(name) * * @throws IOException */ @Test public void test5() throws IOException { InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("config/application.properties"); Properties properties = new Properties(); properties.load(inputStream); properties.list(System.out); System.out.println("=============================================="); String property = properties.getProperty("minio.endpoint"); System.out.println("property = " + property); } /** * 3. 方式三 * 使用Class类的getSystemResourceAsStream方法 和使用当前类的ClassLoader是一样的 * InputStream inputStream = ClassLoader.getSystemResourceAsStream(name) * * @throws IOException */ @Test public void test4() throws IOException { InputStream inputStream = ClassLoader.getSystemResourceAsStream("config/application.properties"); Properties properties = new Properties(); properties.load(inputStream); properties.list(System.out); System.out.println("=============================================="); String property = properties.getProperty("minio.endpoint"); System.out.println("property = " + property); } /** * 4. 方式四 * Resource resource = new ClassPathResource(path) * * @throws IOException */ @Test public void test2() throws IOException { Resource resource = new ClassPathResource("config/application.properties"); Properties properties = PropertiesLoaderUtils.loadProperties(resource); properties.list(System.out); System.out.println("=============================================="); String property = properties.getProperty("minio.endpoint"); System.out.println("property = " + property); } /** * 5. 方式五 * 从文件中获取,使用InputStream字节,主要是需要加上当前配置文件所在的项目src目录地址。路径配置需要精确到绝对地址级别 * BufferedInputStream继承自InputStream * InputStream inputStream = new BufferedInputStream(new FileInputStream(name) * 这种方法读取需要完整的路径,优点是可以读取任意路径下的文件,缺点是不太灵活 * @throws IOException */ @Test public void test3() throws IOException { InputStream inputStream = new BufferedInputStream(new FileInputStream("src/main/resources/config/application.properties")); Properties properties = new Properties(); properties.load(inputStream); properties.list(System.out); System.out.println("=============================================="); String property = properties.getProperty("minio.endpoint"); System.out.println("property = " + property); } /** * 6. 方式六 * 从文件中获取,使用InputStream字节,主要是需要加上当前配置文件所在的项目src目录地址。路径配置需要精确到绝对地址级别 * FileInputStream继承自InputStream * InputStream inputStream = new FileInputStream(name) * 这种方法读取需要完整的路径,优点是可以读取任意路径下的文件,缺点是不太灵活 * @throws IOException */ @Test public void test6() throws IOException { InputStream inputStream = new FileInputStream("src/main/resources/config/application.properties"); Properties properties = new Properties(); properties.load(inputStream); properties.list(System.out); System.out.println("=============================================="); String property = properties.getProperty("minio.endpoint"); System.out.println("property = " + property); } /** * 7. 方式七 * 使用InputStream流来进行操作ResourceBundle,获取流的方式由以上几种。 * ResourceBundle resourceBundle = new PropertyResourceBundle(inputStream); * @throws IOException */ @Test public void test7() throws IOException { InputStream inputStream = ClassLoader.getSystemResourceAsStream("config/application.properties"); ResourceBundle resourceBundle = new PropertyResourceBundle(inputStream); Enumeration<String> keys = resourceBundle.getKeys(); while (keys.hasMoreElements()) { String s = keys.nextElement(); System.out.println(s + " = " + resourceBundle.getString(s)); } } /** * 8. 方式八 * ResourceBundle.getBundle的路径访问和 Class.getClassLoader.getResourceAsStream类似,默认从根目录下读取,也可以读取resources目录下的文件 * ResourceBundle rb = ResourceBundle.getBundle("b") //不需要指定文件名的后缀,只需要写文件名前缀即可 */ @Test public void test8(){ //ResourceBundle rb = ResourceBundle.getBundle("jdbc"); //读取resources目录下的jdbc.properties ResourceBundle rb2 = ResourceBundle.getBundle("config/application");//读取resources/config目录下的application.properties for(String key : rb2.keySet()){ String value = rb2.getString(key); System.out.println(key + ":" + value); } } /** * 单独抽取的方法,用户检测能否正确操纵Properties * * @param inputStream * @throws IOException */ private void printKeyValue(InputStream inputStream) throws IOException { Properties properties = new Properties(); properties.load(inputStream); Set<Object> keys = properties.keySet(); for (Object key : keys) { System.out.println(key + " = " + properties.get(key)); } } }
- Properties类的继承关系图:
-
Properties类简介
2019-09-09 17:04:10Java中有个比较重要的类Properties(Java.util.Properties),主要用于读取Java的配置文件,各种语言都有自己所支持的配置文件,配置文件中很多变量是经常改变的,这样做也是为了方便用户,让用户能够脱离程序本身去...概述
Properties 继承于 Hashtable。表示一个持久的属性集,属性列表以key-value的形式存在,key和value都是字符串。
Java中有个比较重要的类Properties(Java.util.Properties),主要用于读取Java的配置文件,各种语言都有自己所支持的配置文件,配置文件中很多变量是经常改变的,这样做也是为了方便用户,让用户能够脱离程序本身去修改相关的变量设置。像Python支持的配置文件是.ini文件,同样,它也有自己读取配置文件的类ConfigParse,方便程序员或用户通过该类的方法来修改.ini配置文件。在Java中,其配置文件常为.properties文件,格式为文本文件,文件的内容的格式是“键=值”的格式,文本注释信息可以用"#"来注释。
常用方法
除了从Hashtable中所定义的方法,Properties定义了以下方法:
返回值类型 方法和描述 String
getProperty(String key)
搜索具有此属性列表中的指定键属性。String
getProperty(String key, String defaultValue)
搜索具有此属性列表中的指定键属性。void
list(PrintStream out)
将此属性列表输出到指定的输出流。void
list(PrintWriter out)
将此属性列表输出到指定的输出流。void
load(InputStream inStream)
从输入的字节流中读取属性列表 (键和元素对)。void
load(Reader reader)
属性列表 (键和元素对) 从流中读取的输入的字符在一个简单的面向行的格式。void
loadFromXML(InputStream in)
加载所有到此属性表所指定的输入流的 XML 文档表示的属性。Enumeration<?>
propertyNames()
在此属性列表中,如果具有相同名称的密钥不已发现从主要属性列表中的默认属性列表中包括非重复键返回所有键的枚举。void
save(OutputStream out, String comments)
弃用。 如果发生 I/O 错误,则保存在属性列表中,此方法不会引发时抛出。保存的属性列表的首选的方法是通过store(OutputStream out, String comments)
方法或storeToXML(OutputStream os, String comment)
方法。Object
setProperty(String key, String value)
调用Hashtable
方法put
。void
store(OutputStream out, String comments)
此属性列表 (键和元素对) 此Properties
表中写入输出流中适合装载到Properties
表中使用load(InputStream)
方法的格式。void
store(Writer writer, String comments)
此属性列表 (键和元素对) 此Properties
表中写入输出字符流格式适合使用load(Reader)
方法。void
storeToXML(OutputStream os, String comment)
发出代表所有包含此表中的属性的 XML 文档。void
storeToXML(OutputStream os, String comment, String encoding)
发出代表所有包含在此表中,使用指定的编码的属性的 XML 文档。Set<String>
stringPropertyNames()
返回一组键此属性列表中的关键和其对应的值都是字符串,默认属性列表中包括非重复键,如果具有相同名称的密钥不已发现从主要属性列表。常用方法实践
Properties类
下面我们从写入、读取、遍历等角度来解析Properties类的常见用法
项目路径如下
src main java com jourwon prop PropertiesTest resources config.properties prop.properties
PropertiesTest为测试类,prop.properties文件的内容如下
username=root password=123456
写入
Properties类调用setProperty方法将键值对保存到内存中,此时可以通过getProperty方法读取,propertyNames方法进行遍历,但是并没有将键值对持久化到属性文件中,故需要调用store方法持久化键值对到属性文件中。
public static void main(String[] args) throws IOException { Properties properties = new Properties(); OutputStream output = null; try { output = new FileOutputStream("src/main/resources/config.properties"); properties.setProperty("username", "root"); properties.setProperty("password", "123456"); // 保存键值对到文件中 properties.store(output, "JourWon modify"); } catch (IOException io) { io.printStackTrace(); } finally { if (output != null) { try { output.close(); } catch (IOException e) { e.printStackTrace(); } } } }
输出结果,在resources目录下多一个文件config.properties,内容如下
#JourWon modify #Mon Sep 09 14:23:44 CST 2019 password=123456 username=root
读取
下面给出常见的六种读取properties文件的方式:
public static void main(String[] args) throws IOException { // PropertiesTest.getPath1(); // PropertiesTest.getPath2(); // PropertiesTest.getPath3(); // PropertiesTest.getPath4(); // PropertiesTest.getPath5(); PropertiesTest.getPath6(); } /** * 一、 使用java.util.Properties类的load(InputStream in)方法加载properties文件 * 主要是需要加上src这个文件夹名。路径配置需要精确到绝对地址级别 * * @return */ public static void getPath1() throws IOException { InputStream in = new BufferedInputStream(new FileInputStream( new File("src/main/resources/prop.properties"))); printKeyValue(in); } /** * 二、 使用java.util.ResourceBundle类的getBundle()方法 * 注意:这个getBundle()方法的参数只能写成包路径+properties文件名,注意不需要带上后缀名。 * * @return */ public static void getPath2() { ResourceBundle rb = ResourceBundle .getBundle("prop"); printKeyValueRb(rb); } /** * 三、 使用java.util.PropertyResourceBundle类的构造函数 * * @return */ public static void getPath3() throws IOException { InputStream in = new BufferedInputStream(new FileInputStream("src/main/resources/prop.properties")); ResourceBundle rb = new PropertyResourceBundle(in); printKeyValueRb(rb); } /** * 四、 使用class变量的getResourceAsStream()方法 * 注意:getResourceAsStream()方法的参数按格式写到包路径+properties文件名+.后缀 * * @return */ public static void getPath4() throws IOException { InputStream in = PropertiesTest.class .getResourceAsStream("/prop.properties"); printKeyValue(in); } /** * 五、 * 使用class.getClassLoader()所得到的java.lang.ClassLoader的 * getResourceAsStream()方法 * getResourceAsStream(name)方法的参数必须是包路径+文件名+.后缀 * 否则会报空指针异常 * * @return */ public static void getPath5() throws IOException { InputStream in = PropertiesTest.class.getClassLoader() .getResourceAsStream("./././prop.properties"); printKeyValue(in); } /** * 六、 使用java.lang.ClassLoader类的getSystemResourceAsStream()静态方法 * getSystemResourceAsStream()方法的参数格式也是有固定要求的 * * @return */ public static void getPath6() throws IOException { InputStream in = ClassLoader .getSystemResourceAsStream("./././prop.properties"); printKeyValue(in); } /** * 单独抽取的方法,用户检测能否正确操纵Properties * * @param inputStream * @throws IOException */ public static void printKeyValue(InputStream inputStream) throws IOException { Properties properties = new Properties(); properties.load(inputStream); Set<Object> keys = properties.keySet(); for (Object key : keys) { System.out.println(key + " = " + properties.get(key)); } if (inputStream != null) { inputStream.close(); } } public static void printKeyValueRb(ResourceBundle rb) { Set<String> keys = rb.keySet(); for (String key : keys) { System.out.println(key + " = " + rb.getString(key)); } }
输出结果都是
password = 123456 username = root
其中第一、四、五、六种方式都是先获得文件的输入流,然后通过Properties类的load(InputStream inStream)方法加载到Properties对象中,最后通过Properties对象来操作文件内容。
第二、三中方式是通过ResourceBundle类来加载Properties文件,然后ResourceBundle对象来操做properties文件内容。
其中最重要的就是每种方式加载文件时,文件的路径需要按照方法的定义的格式来加载,否则会抛出各种异常,比如空指针异常。遍历
下面给出四种遍历Properties中的所有键值对的方法:
/** * 输出properties的key和value */ public static void printProp(Properties properties) { System.out.println("---------(方式一)------------"); for (String key : properties.stringPropertyNames()) { System.out.println(key + "=" + properties.getProperty(key)); } System.out.println("---------(方式二)------------"); //返回属性key的集合 Set<Object> keys = properties.keySet(); for (Object key : keys) { System.out.println(key.toString() + "=" + properties.get(key)); } System.out.println("---------(方式三)------------"); Set<Map.Entry<Object, Object>> entrySet = properties.entrySet(); //返回的属性键值对实体 for (Map.Entry<Object, Object> entry : entrySet) { System.out.println(entry.getKey() + "=" + entry.getValue()); } System.out.println("---------(方式四)------------"); Enumeration<?> e = properties.propertyNames(); while (e.hasMoreElements()) { String key = (String) e.nextElement(); String value = properties.getProperty(key); System.out.println(key + "=" + value); } }
-
jquery.i18n.properties-1.0.9.js
2015-10-29 16:23:01jquery.i18n.properties-1.0.9.js 下载 -
Java读写Properties配置文件(Properties类)
2022-04-27 11:04:36PropertiesProperties基本介绍Properties常用方法Properties读取配置文件Properties写入配置文件 Properties基本介绍 专门用来读写配置文件的集合类,Properties类表示一组持久的属性。 Properties可以保存到流中或...Properties基本介绍
专门用来读写配置文件的集合类,
Properties
类表示一组持久的属性。Properties
可以保存到流中或从流中加载。 属性列表中的每个键及其对应的值都是一个字符串。
属性列表可以包含另一个属性列表作为其“默认值”; 如果在原始属性列表中找不到属性键,则会搜索此第二个属性列表。
因为Properties
从继承Hashtable
时,put
种putAll
方法可应用于Properties
对象。 强烈不鼓励使用它们,因为它们允许调用者插入其键或值不是Strings
。 应该使用setProperty
方法。 如果store
或save
方法在包含非String
键或值的“受损害”Properties
对象上调用,则调用将失败。 类似地,如果在包含非String
密钥的“受损害”Properties
对象上调用propertyNames
或list方法的调用将失败。配置文件的格式:键 = 值(key = value)
PS:键值对不需要有空格,值不需要用引号包起来。默认类型为String
properties是配置文件。
主要的作用是通过修改配置文件可以方便地修改代码中的参数,实现不用改class文件即可灵活变更参数。
解释:java运行中java文件会变成class文件,之后无法通过反编译找到原样的代码,这样的话,如果java类中某个参数变更,就很难灵活的实现参数修改,这个时候properties 文件就能很灵活的实现配置,减少代码的维护成本和提高开发效率。Properties常用方法
load()
//加载配置文件的键值对到Properties对象中list()
//将数据显示到指定位置getProperty(key)
//根据键获取值setProperty(key,value)
//设置键值对到Properties对象中store()
//将Properties中的键值对存储到配置文件之后,在idea中,保存信息到配置文件,如果含有中文,会存储为unicode码
loadFromXML(InputStream)
和storeToXML(OutputStream, String, String)
//方法以简单的XML格式加载和存储属性。 默认情况下,使用UTF-8字符编码,但是如果需要,可以指定特定编码。 需要实现支持UTF-8和UTF-16,并可能支持其他编码。 XML属性文档具有以下DOCTYPE声明:Properties读取配置文件
使用Properties类来读取test.properties文件
1.创建Properties对象
2.加载指定配置文件
3.把键值对(k=v)显示在控制台
4.根据key获取对应的value值
import java.io.FileReader; import java.io.IOException; import java.util.Properties; public class Test01 { public static void main(String[] args) throws IOException { //创建Properties对象 Properties properties = new Properties(); //加载配置文件 properties.load(new FileReader("E:\\Java_基础\\code\\IO流\\src\\test.properties")); //将内容显示在控制台 properties.list(System.out); //通过key获取value String user = properties.getProperty("user"); String password = properties.getProperty("password"); System.out.println("用户名:"+user); System.out.println("密码:"+password); } }
Properties写入配置文件
使用Properties来创建配置文件并设置内容
1.创建Properties对象
2.创建对应的键值对
setProperty(String key,String value)
3.将键值对存储到指定的配置文件中
store(Writer writer, String comments)
PS:如果文件中没有key,就是创建一对键值对,如果该文件有key,就是修改该键对应的值
import java.io.FileWriter; import java.io.IOException; import java.util.Properties; public class Test02 { public static void main(String[] args) throws IOException { //创建对象 Properties properties = new Properties(); //设置键值对 properties.setProperty("user","root"); properties.setProperty("password","abcdef"); //写入到指定文件,如果没有,就创建该文件 properties.store(new FileWriter("E:\\Java_基础\\code\\IO流\\src\\demo.properties"),null); //如果comments参数不为空,则首先将ASCII #字符,注释字符串和行分隔符写入输出流。 因此, comments可以作为识别评论。 //注释行始终写入,由ASCII #组成,当前日期和时间(如当前时间Date的toString方法生成的)以及由Writer生成的行分隔Writer 。 } }
-
8.Properties类
2021-12-01 11:03:341.Properties 1.Propertiesg概述: 是一个Map体系的集合类 Properties可以保存到流中或从流中加载 2.练习:Properties作为Map集合使用 package Study09; import java.util.Properties; import java.util.Set; /*... -
【Java】Properties类
2022-04-01 15:29:21Properties(Java.util.Properties)是Java中一个比较重要的类,主要用于读取Java的配置文件。各种语言都有自己所支持的配置文件,配置文件中很多变量是经常改变的,这样做也是为了方便用户,让用户能够脱离程序本身... -
java怎么读取properties文件、修改properties文件和封装properties
2019-08-09 10:48:55什么是Properties文件,Properties文件和XML一样是java中经常使用到的一种配置文件,读取和使用起来比较方便,并且可读性好。我们在编写代码的过程中为了将代码提高可参数化经常会将一些配置属性存放在我们的配置... -
Properties 文件操作
2022-03-21 10:17:07然后就想到了Properties 对象对数据进行存储。 2、使用方式 这里使用了静态方法对对象进行读写。 /** * 按照key读取 Properties * @param tempPath 文件存储目录 * @param fileName 文件名称 -
Android 使用Properties配置文件
2022-03-22 18:46:25在Android中使用Properties保存以及提取参数的实体类,文件格式为一行一个参数。 -
Java 读取 .properties 配置文件的几种方式
2021-12-21 14:59:43** Java 开发中,需要将一些易变的配置参数放置再 XML 配置文件或者 ... Properties properties = new Properties(); // 使用ClassLoader加载properties配置文件生成对应的输入流 InputStream in = Propertie -
jquery.i18n.properties-min-1.0.9.js前端国际化文件
2013-10-31 11:21:56jquery.i18n.properties-min-1.0.9.js前端国际化文件,项目中用到的前端国际化文件。 $.i18n.properties( { name : 'web_i18n', // Resource name path : '/resources/i18n/', //Resource path cache : true, ... -
Java读取Properties文件的六种方法
2021-02-12 10:19:11Java读取Properties文件有以下六种方法:1。使用java.util.Properties类的load()方法StringfileName="E:/system.properties";InputStreamin=newBufferedInputStream(newFileInputStream(fileName));Propertiesp=new... -
Properties,Properties的store方法,Properties的load方法
2020-09-20 18:56:17Properties package Properties_Demo; import java.util.Properties; import java.util.Set; public class Demo01 { public static void main(String[] args) { Show01(); } private static void Show01... -
properties文件和环境变量
2021-03-08 20:58:07本文概览:介绍了通过java.util.Properties类和Spring配置中获取配置文件中值1 Properties文件1.1 使用java.util.Properties类使用Properties来读取配置文件,代码举例如下public class TestProperty {@Testpublic ... -
Android获取SystemProperties方法
2022-04-25 15:06:08Android API从21后开始,不再直接支持通过SystemProperties.get/set方式来获取/设置系统属性。 目前有两种方式可以获取/设置系统属性,分别为通过反射方法及AndroidStudio中通过引入jar包来解决。 二、方法一 通过... -
SpringBoot读取properties文件中的值
2022-04-23 18:35:36SpringBoot项目读取properties文件中的属性值 -
Java读写Properties配置文件
2021-02-28 13:06:52Java读写Properties配置文件,1.Properties类与Properties配置文件Properties类继承自Hashtable类并且实现了Map接口,也是使用一种键值对的形式来保存属性集。不过Properties有特殊的地方,就是它的键和值都是字符串... -
配置文件(properties类)
2022-04-19 23:21:39基本介绍 1.专门用于读写配置文件的集合类; 配置文件格式: 键=值 2.注意:键值对,不需要空格,值,不需要用引号 ;默认类型:String ...5)store:将Properties中的键值对 存储到配置文件,在ide -
Android gradle.properties 基础使用和常规配置
2022-04-20 11:41:041.在使用Android Studio新建Android项目之后,在项目根目录下会默认生成一个gradle.properties文件,它是由IDE自动生成的gradle.properties文件。 2.他是项目级别的Gradle配置文件,gradle.properties里面定义的... -
cmkae命令set_target_properties
2021-12-04 10:56:44一、介绍 命令的格式如下 set_target_properties(target1 target2 ... PROPERTIES prop1 ...Sets properties on targets. The syntax for the command is to list all the targets you want to change, and then -
Java中properties文件编码问题
2022-02-08 11:54:141、properties文件显示乱码问题 原因是因为properties默认使用ASCII码,就算在文件中填写了中文,再打开后依然会转换成ASCII码的形式。 首先确定properties配置文件的编码格式,通常情况下properties的默认编码格式... -
在线YAML转Properties工具
2022-04-21 21:36:00在线YAML转Properties工具 在线YAML转Properties工具 YAML是一个可读性高,用来表达数据序列化的格式。YAML参考了其他多种语言,包括:C语言、Python、Perl,并从XML、电子邮件的数据格式(RFC 2822)中获得灵感。 ... -
java读取properties文件的几种方法
2021-03-03 11:57:39一、项目中经常会需要读取配置文件(properties文件),因此读取方法总结如下:1、通过java.util.Properties读取1 Properties p=newProperties();2 //p需要InputStream对象进行读取文件,而获取InputStream有多种方法... -
在shell脚本中如何读取properties文件中的值
2022-02-18 20:29:28本文介绍了一种在shell脚本中读取properties的方法。 -
JAVA操作properties配置文件
2018-07-31 22:41:08java中的properties文件是一种配置文件,主要用于表达配置信息,文件类型为*.properties,格式为文本文件,文件的内容是格式是"键=值"的格式。 <2> 在properties文件中,可以用"#&... -
IDEA 中 .properties文件的中文显示乱码问题的解决办法
2022-04-21 09:41:08今天使用IDEA 搭建Spring Boot 项目,配置application.properties 配置文件,录入中文,在右下角出现如下截图提示语: 重新打开application.properties 文件出现汉字乱码,依据提示信息修改源码编码格式和*.... -
c#操作properties,读写配置文件
2011-12-27 15:05:52c#操作properties,读写配置文件,非常方便,几行代码就搞定 -
properties读取数组
2020-10-13 11:54:29properties读取数组