properties文件的读取

Demo

 1 //声明资源器类
 2         Properties pro=new Properties();
 3         //获取路径
 4         URL url= PropertiesTest.class.getClassLoader().getResource("driver.properties");
 5         String path=url.getPath();
 6         try {
 7             path=URLDecoder.decode(path, "utf-8");//防止中文或者 空格的出现,进行反编码
 8             File file=new File(path);
 9             //加载文件
10             pro.load(new FileInputStream(file));
11             //获取信息
12             String driver= pro.getProperty("driver");
13             String jdbcurl=pro.getProperty("url");
14             String name=pro.getProperty("name");
15             String pwd=pro.getProperty("pwd");
16             
17             System.err.println("driver="+driver);
18             System.err.println("url="+jdbcurl);
19             System.err.println("name="+name);
20             System.err.println("pwd="+pwd);
21             
22             
23         }

Properties文件#driver.properties,在src根目录下

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/jdbcdb
name=root
pwd=mysql

运行结果:

原文地址:https://www.cnblogs.com/liuwt365/p/4101393.html