Java读取Properties的方式 项目结构:经典的Maven项目结构 配置文件1和2内容一致: jdbc.driver=com.Mysql.cj.jdbc.Driverjdbc.url=mysql://localhost:3306/
配置文件1和2内容一致:
jdbc.driver=com.Mysql.cj.jdbc.Driverjdbc.url=mysql://localhost:3306/database?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghaijdbc.username=rootjdbc.passWord=123456
//读取配置文件1public void readProperties1() throws ioException { //不加/会从当前包进行寻找,加上/会从src开始找 InputStream inputStream = this.getClass().getResourceAsStream("/jdbc.properties"); Properties properties=new Properties(); properties.load(inputStream); System.out.println("jdbc.driver="+properties.getProperty("jdbc.driver")); System.out.println("jdbc.url="+properties.getProperty("jdbc.url")); System.out.println("jdbc.username="+properties.getProperty("jdbc.username")); System.out.println("jdbc.password="+properties.getProperty("jdbc.password"));} //读取配置文件2 public void readProperties1() throws IOException { InputStream inputStream = this.getClass().getResourceAsStream("/config/jdbc2.properties"); Properties properties=new Properties(); properties.load(inputStream); System.out.println("jdbc.driver="+properties.getProperty("jdbc.driver")); System.out.println("jdbc.url="+properties.getProperty("jdbc.url")); System.out.println("jdbc.username="+properties.getProperty("jdbc.username")); System.out.println("jdbc.password="+properties.getProperty("jdbc.password"));}
//读取配置文件1public void readProperties2() throws IOException { //不加/,若加了会为null InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("jdbc.properties"); Properties properties=new Properties(); properties.load(inputStream); System.out.println("jdbc.driver="+properties.getProperty("jdbc.driver")); System.out.println("jdbc.url="+properties.getProperty("jdbc.url")); System.out.println("jdbc.username="+properties.getProperty("jdbc.username")); System.out.println("jdbc.password="+properties.getProperty("jdbc.password"));}//读取配置文件2public void readProperties2() throws IOException { InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("config/jdbc2.properties"); Properties properties=new Properties(); properties.load(inputStream); System.out.println("jdbc.driver="+properties.getProperty("jdbc.driver")); System.out.println("jdbc.url="+properties.getProperty("jdbc.url")); System.out.println("jdbc.username="+properties.getProperty("jdbc.username")); System.out.println("jdbc.password="+properties.getProperty("jdbc.password"));}
方法1和2区别: (classpath即为target/classes 这个目录)
Class.getResourceAsStream() 从当前类所在的位置开始查找配置文件位置。要找到jdbc.properties和jdbc2.properties必须加/从classpath下开始查找
Class.getClassLoader().getResourceAsStream() 默认就从classpath路径下开始查找,加上/会报空指针
十分有必要知道java中类的加载过程!!!
public void readProperties3() throws IOException { //InputStream inputStream = ClassLoader.getSystemResourceAsStream("config/jdbc2.properties"); InputStream inputStream = ClassLoader.getSystemResourceAsStream("jdbc.properties"); Properties properties=new Properties(); properties.load(inputStream); System.out.println("jdbc.driver="+properties.getProperty("jdbc.driver")); System.out.println("jdbc.url="+properties.getProperty("jdbc.url")); System.out.println("jdbc.username="+properties.getProperty("jdbc.username")); System.out.println("jdbc.password="+properties.getProperty("jdbc.password")); }
public void readProperties4() throws IOException { //ClassPathResource resource = new ClassPathResource("jdbc.properties"); ClassPathResource resource = new ClassPathResource("config/jdbc2.properties"); Properties properties= PropertiesLoaderUtils.loadProperties(resource); System.out.println("jdbc.driver="+properties.getProperty("jdbc.driver")); System.out.println("jdbc.url="+properties.getProperty("jdbc.url")); System.out.println("jdbc.username="+properties.getProperty("jdbc.username")); System.out.println("jdbc.password="+properties.getProperty("jdbc.password"));}
public void readProperties5() throws IOException { //InputStream inputStream = ClassLoader.getSystemResourceAsStream("jdbc.properties"); InputStream inputStream = ClassLoader.getSystemResourceAsStream("config/jdbc2.properties"); PropertyResourceBundle bundle = new PropertyResourceBundle(inputStream); System.out.println(bundle.getString("jdbc.driver")); System.out.println(bundle.getString("jdbc.url")); System.out.println(bundle.getString("jdbc.username")); System.out.println(bundle.getString("jdbc.password")); }
//不用输入后缀public void readProperties6() { //ResourceBundle bundle=ResourceBundle.getBundle("jdbc"); ResourceBundle bundle=ResourceBundle.getBundle("config/jdbc2"); System.out.println(bundle.getString("jdbc.driver")); System.out.println(bundle.getString("jdbc.url")); System.out.println(bundle.getString("jdbc.username")); System.out.println(bundle.getString("jdbc.password")); }
来源地址:https://blog.csdn.net/yirenDM/article/details/130488107
--结束END--
本文标题: Java读取Properties配置文件的6种方式
本文链接: https://lsjlt.com/news/372191.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-04-01
2024-04-03
2024-04-03
2024-01-21
2024-01-21
2024-01-21
2024-01-21
2023-12-23
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0