java 中properties 类读取k-v配置文件
Posted 周无极
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java 中properties 类读取k-v配置文件相关的知识,希望对你有一定的参考价值。
properties 读取的配置文件key和values都是string 类型
package com.bjsxt.others.pro; import java.util.Properties; /** * Properties 资源配置文件的读写 * 1、key 与value 只能为字符串 * 2、存储与读取 * setProperty(String key, String value) * getProperty(String key, String defaultValue) * @author Administrator * */ public class Demo01 { /** * @param args */ public static void main(String[] args) { //创建对象 Properties pro =new Properties(); //存储 pro.setProperty("driver", "oracle.jdbc.driver.OracleDriver"); //pro.setProperty("url", "jdbc:oracle:thin:@localhost:1521:orcl"); pro.setProperty("user", "scott"); pro.setProperty("pwd", "tiger"); //获取 String url =pro.getProperty("url","test"); System.out.println(url); } }
package com.bjsxt.others.pro; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.util.Properties; /** * 使用Properties 输出到文件 * 资源配置文件: * * 1、.properties * store(OutputStream out, String comments) store(Writer writer, String comments) 2、.xml storeToXML(OutputStream os, String comment) :UTF-8字符集 storeToXML(OutputStream os, String comment, String encoding) * @author Administrator * */ public class Demo02 { /** * @param args * @throws IOException * @throws FileNotFoundException */ public static void main(String[] args) throws FileNotFoundException, IOException { //创建对象 Properties pro =new Properties(); //存储 pro.setProperty("driver", "oracle.jdbc.driver.OracleDriver"); pro.setProperty("url", "jdbc:oracle:thin:@localhost:1521:orcl"); pro.setProperty("user", "scott"); pro.setProperty("pwd", "tiger"); //存储到e:/others 绝对路径 盘符: //pro.store(new FileOutputStream(new File("e:/others/db.properties")), "db配置"); //pro.storeToXML(new FileOutputStream(new File("e:/others/db.xml")), "db配置"); //使用相对路径 当前的工程 // pro.store(new FileOutputStream(new File("db.properties")), "db配置"); // pro.store(new FileOutputStream(new File("src/db.properties")), "db配置"); pro.store(new FileOutputStream(new File("src/com/bjsxt/others/pro/db.properties")), "db配置"); } }
package com.bjsxt.others.pro; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.util.Properties; /** * 使用Properties读取配置文件 * 资源配置文件: * 使用相对与绝对路径读取 * load(InputStream inStream) load(Reader reader) loadFromXML(InputStream in) * @author Administrator * */ public class Demo03 { /** * @param args * @throws IOException * @throws FileNotFoundException */ public static void main(String[] args) throws FileNotFoundException, IOException { Properties pro=new Properties(); //读取 绝对路径 //pro.load(new FileReader("e:/others/db.properties")); //读取 相对路径 pro.load(new FileReader("src/com/bjsxt/others/pro/db.properties")); System.out.println(pro.getProperty("user", "bjsxt")); } }
package com.bjsxt.others.pro; import java.io.IOException; import java.util.Properties; /** * 使用类相对路径读取配置文件 * bin * @author Administrator * */ public class Demo04 { /** * @param args * @throws IOException */ public static void main(String[] args) throws IOException { Properties pro =new Properties(); //类相对路径的 / bin //pro.load(Demo04.class.getResourceAsStream("/com/bjsxt/others/pro/db.properties")); //"" bin pro.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("com/bjsxt/others/pro/db.properties")); System.out.println(pro.getProperty("user", "bjsxt")); } }
以上是关于java 中properties 类读取k-v配置文件的主要内容,如果未能解决你的问题,请参考以下文章