Properties
来源:互联网 发布:mac qq接受文件没反应 编辑:程序博客网 时间:2024/06/15 23:46
@(笔记)[MarkDown|集合|我的博客|每日一类]
07.21
- Properties类
- 方法的使用
- Properties 综合使用
Properties类
public class Propertiesextends Hashtable<Object,Object>
Properties 类表示了一个持久的属性集。Properties 可保存在流中或从流中加载。属性列表中每个键及其对应值都是一个字符串。
新建一个文件
内容为
键1=值1
键2=值2
…
使用此类可以对文件数据进行持久化保存
方法的使用
load(InputStream inStream);
从输入流中读取属性列表(键和元素对)。
Properties pro = new Properties();pro.load(new FileInputStream(相对路径或绝对路径));//此方法将抛出FileNotFoundException
getPropertiy(String key)
使用指定的键在此属性列表中搜索属性,返回StringgetProperty(String key, String defaultValue)
用指定的键在属性列表中搜索属性。返回StringsetProperty(String key, String value)
如果存在了这个键,则更改对应的值,如果不存在,则添加这个属性,这个方法是调用Hashtable的put的结果public void store(OutputStream out, String comments) throws IOException
写入文件中
props.store(new FileOutputStream(相对路径或绝对路径), 说明);
Properties 综合使用
/** @Author: Code.Ai* @Date: 2015-07-21 23:57:11* @Last Modified by: Code.Ai* @Last Modified time: 2015-07-22 00:51:58*/import java.io.*;import java.util.*;public class PropertiesTest { static Properties props = new Properties(); public static void main(String[] args) { //读文件 Read(); System.out.println(props.getProperty("Teacher")); //添加数据 //使用setProperties()方法,如果文件中没有相同的键,则添加,如果有相同的,就覆盖 props.setProperty("Teacher","胡老大"); props.setProperty("Student","Code"); System.out.println(props.getProperty("Student")); Save(); //删除全部数据 //直接在没有加载文件的情况下使用store()方法存入空数据到文件中 Read(); //存入Sudtent对象 for(int i = 0 ; i < 3; i++){ Scanner scan = new Scanner(System.in); System.out.println("请输入学生名字:"); String name = scan.next(); System.out.println("请输入学生年龄:"); int age = scan.nextInt(); System.out.println("请输入学生成绩"); int score = scan.nextInt(); Student stu = new Student(name,age,score); props.setProperty(stu.getName(), stu.getName() + "&" + stu.getAge() + "&" + stu.getScore()); } Save(); //删除指定的键 直接调用Map类的remove方法 props.remove("Student"); Save(); //构造学生对象 //把值取出来分解,然后赋值给学生 //调用继承Map的values()方法,取得所有的值 Collection<String> cl = props.values(); for(String str : cl){ System.out.println(str); //拆分字符串 } } public static void Read(){ try { props.load(new FileInputStream("data.properties")); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } //存文件 public static void Save(){ try { props.store(new FileOutputStream("data.properties"), null); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }}
0 0
- Properties
- properties
- Properties
- PROPERTIES
- Properties
- properties
- properties
- Properties
- Properties
- Properties
- properties
- Properties
- properties
- Properties
- Properties
- Properties
- properties
- Properties
- 求二叉树中节点的最大距离
- 关系表达式的使用
- 02 JAVA 对象补充 抽象VS接口
- IOS开发中的委托模式
- 集合工具类
- Properties
- ShadowSocks 代理设置,超有用!
- 虚拟桌面
- [Elasticsearch] 向已存在的索引中添加自定义filter/analyzer
- 正则表达式去除粘贴代码行号
- 【思路题】【多校第一场】【1001.OO’s Sequence】
- spring对shiro注解支持的原理
- STM32CUBE——7 DS18B20
- C语言之函数调用08—暴力法求4个数的最大公约数和最小公倍数