它也是一种key-value结构的集合,它是HashTable的子类,它同HashTable在某些规则上是一样,也是添加了synchronized关键字的。例外,同样,不允许放置null 值 和null键
它的主要使用场景:从配置文件中 加载内容到程序来中
mysql.properties:
jdbc.url=1231wrwerjdbc.userName=userpujdbc.password=123456jdbc.driverClassName=234
key为等号左边的部分,value为等号右边的值
import java.io.File;import java.io.FileInputStream;import java.io.IOException;import java.util.Properties;import com.woniuxy.java21.bean.CarBean;import com.woniuxy.java21.bean.StudentBean;public class PropertiesStudy {public static void main(String[] args) {// TODO Auto-generated method stub// study01();study02();}private static void study02() {// TODO Auto-generated method stubProperties props = new Properties();FileInputStream fis = null;try {// File.separatorChar 代表 \\ 在windows系统中String path = System.getProperty("user.dir") + File.separatorChar + "src" + File.separatorChar +"mysql.properties" ;// System.out.println(path);File file = new File(path);fis = new FileInputStream(file);props.load(fis);System.out.println(props.get("jdbc.url"));System.out.println(props.get("jdbc.userName"));System.out.println(props.get("jdbc.password"));System.out.println(props.get("jdbc.driverClassName"));} catch (Exception e) {// TODO: handle exceptione.printStackTrace();}finally {try {fis.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}private static void study01() {// TODO Auto-generated method stub// Properties props = new Properties();// //同样可以放置null值和null键// props.put(null, null);// //也可以放置其他东西// props.put(new CarBean(), new StudentBean());// props.put("userName", "小张");// System.out.println(props.get("userName"));}}
