目录
Properties
介绍
-
java.util.Properties,主要用于java的配置文件。
-
配置文件中很多变量是经常改变的,这样做也是为了方便用户,让用户能够脱离程序本身去修改相关的变量设置。
-
在Java中,其配置文件常为.properties文件,格式为文本文件,文件的内容的格式是“键=值”或者“键 值”的格式
-
文本注释信息可以用"#"来注释。
特点
-
Properties是Hashtable<Object,Object>的子类
-
Properties类表示了一个可持久的属性集
-
Properties可以保存在流中或从流中加载
-
Properties中每个键和对应的值都是一个字符串(String类型)
-
Properties有一个特殊的作用:专门用来加载xxx.properties配置文件
Properties对象
构造方法 | 说明 |
---|---|
public Properties() | 创建Properties对象 |
方法名 | 说明 |
---|---|
public Object setProperty(String key, String value) | 设置key和value |
public String getProperty(String key) | 通过key获取value,返回值是字符串 |
public Object get(Object key) | 通过key获取value,返回值是Object |
public Object put(Object key, Object value) | 设置key和value,支持任意类型 |
public Object replace(Object key, Object value) | 通过key替换值 |
public Object remove(Object key) | 通过key删除元素 |
public boolean isEmpty() | 对象中无元素返回true |
public void putAll(Map<?, ?> t) | 将Map集合添加进对象中 |
public Collection<Object> values() | 将Properties转成集合 |
public Enumeration<?> propertyNames() | 获取所有key的枚举对象 |
遍历
//使用Entry遍历 for(Map.Entry<Object,Object> entry:properties.entrySet()){ System.out.println(entry.getKey()+"=>"+entry.getValue()); }
Properties文件
介绍
-
用于保存一些数据,文件中的格式都是key=value形式,每个键值对不需要符号隔开,直接回车换行
方法名 | 说明 |
---|---|
public void load(InputStream inStream) | 通过输入流读取文件 |
获取项目根目录路径
this.getClass().getClassLoader().getResourceAsStream("xx.properties")
获取输入流
InputStream inputStream=this.getClass().getClassLoader().getResourceAsStream("stu.properties");
读取properties文件
Properties properties=new Properties(); InputStream inputStream=this.getClass().getClassLoader().getResourceAsStream("my.properties"); BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(inputStream,"UTF-8")); properties.load(bufferedReader); for(Map.Entry<Object,Object> entry:properties.entrySet()){ System.out.println(entry.getKey()+"="+entry.getValue());}
properties写入数据
PrintWriter printWriter=new PrintWriter(new FileOutputStream("src\\data.properties")); String data="address=常州2"; //判断key是否存在 Properties properties=new Properties(); InputStream inputStream=this.getClass().getClassLoader().getResourceAsStream("data.properties"); BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(inputStream)); properties.load(bufferedReader); if(properties.containsKey(data.split("=")[0])){ properties.setProperty(data.split("=")[0],data.split("=")[1]); for(Map.Entry<Object,Object> entry:properties.entrySet()){ printWriter.println(entry.getKey()+"="+entry.getValue()); printWriter.flush(); } }
进程和线程
进程:
-
进程是操作系统中的一个任务(一个应用程序运行在一个进程中)。
-
进程(process)就是一块包含了某些资源的内存区域。操作系统利用进程把它的工作划分为一些功能单元。
-
进程中所包含的一个或多个执行单元称为线程(thread)。进程还拥有一个私有的虚拟地址空间,该空间仅能被它所包含的线程访问。
-
线程只能归属于一个进程并且它只能访问该进程所拥有的资源。当操作系统创建一个进程后,该进程会自动申请一个名为主线程或首要线程的线程。