Properties集合
2016-01-05 19:10
435 查看
Properties集合的-基本功能
Properties集合的-list方法
Properties集合的-store方法
Properties集合的-修改配置信息
Properties集合的-练习
public class Io_42 { public static void main(String[] args) { /** * Map * |--Hashtable * |--Properties * * Properties集合: * 特点: * 1.该集合中的键和值都是字符串类型。 * 2.集合中的数据可以保存到流中,或者从流中获取。 * * 通常集合用于操作以键值对形式存在的配置文件 * */ propertiesDemo(); } /** * * Properties集合的存和取 * */ public static void propertiesDemo(){ //创建一个Properties集合 Properties prop=new Properties(); //存储元素 prop.setProperty("zhangsan", "30"); prop.setProperty("lisi", "31"); prop.setProperty("wangwu", "36"); prop.setProperty("zhaoliu", "20"); //修改元素 prop.setProperty("wangwu", "26"); //取出所有元素 /* Set<String> names=prop.stringPropertyNames(); for (String name:names) { String value=prop.getProperty(name); System.out.println(name+":"+value); }*/ //取出一个 //System.out.println(prop.getProperty("wangwu")); } }
Properties集合的-list方法
public class Io_43 { /** * 演示Properties集合和流对象相结合的功能 * */ public static void main(String[] args) { method2(); } public static void method2(){ //创建一个Properties集合 Properties prop=new Properties(); //存储元素 prop.setProperty("zhangsan", "30"); prop.setProperty("lisi", "31"); prop.setProperty("wangwu", "36"); prop.setProperty("zhaoliu", "20"); //该方法一般用于测试 prop.list(System.out); } }
Properties集合的-store方法
public class Io44_1 { public static void main(String[] args) throws IOException { methodDemo_3(); } public static void methodDemo_3() throws IOException{ //创建一个Properties集合 Properties prop=new Properties(); //存储元素 prop.setProperty("zhangsan", "30"); prop.setProperty("lisi", "31"); prop.setProperty("wangwu", "36"); prop.setProperty("zhaoliu", "20"); //想要将这些集合中的字符串键值信息持久化存储到文件中。 //需要关联输出流 FileOutputStream fos=new FileOutputStream("E:\\info.txt"); //将集合数据存储到文件中。使用store方法。 prop.store(fos, "name+age"); fos.close(); } }
Properties集合的-修改配置信息
public class Io45_1 { public static void main(String[] args) throws Exception { test(); } @SuppressWarnings("unused") private static void methodDemo_4() throws IOException { Properties prop=new Properties(); //集合中的数据来自一个文件, //注意:必须要保证该文件中的数据是键值对 //需要使用到读取流来完成 FileInputStream fis=new FileInputStream("info.txt"); //使用load(); 作用:从输入流中读取属性列表(键和元素对)。 prop.load(fis); prop.list(System.out); } //模拟一下load方法 public static void myLoad() throws Exception{ Properties prop=new Properties(); BufferedReader bufr=new BufferedReader(new FileReader("info.txt")); String line=null; while((line=bufr.readLine())!=null){ if(line.startsWith("#")){ continue; } String[] arr=line.split("="); // System.out.println(arr[0]+":"+arr[1]); prop.setProperty(arr[0], arr[1]); } prop.list(System.out); bufr.close(); } //对已有的配置文件中的信息进行修改。 /* * 读取这个文件。 4000 * 并将这个文件中的键值数据存储到集合中, * 再通过集合对数据进行修改。 * 在通过流将修改后的数据存储到文件中。 * **/ public static void test() throws Exception{ //读取这个文件 File file=new File("info.txt"); if(!file.exists()){ file.createNewFile(); } //FileReader fr=new FileReader("info.txt"); FileReader fr=new FileReader(file); //创建集合存储配置信息 Properties prop=new Properties(); //将流中信息存储到集合中 prop.load(fr); prop.setProperty("wangwu", "6"); //FileOutputStream fos=new FileOutputStream("info.txt"); FileWriter fw=new FileWriter(file); prop.store(fw, "name+age"); fr.close(); fw.close(); } }
Properties集合的-练习
/* * 定义功能,获取一个应用程序运行的次数,如果超过 * 5次。给出使用已到请注册的提示。并不要运行程序 * * * 思路: * 1,应该有计数器。 * 每次程序启动都需要计数一次,并且在 原有的次数上进行计数。 * * 2.计数器就是一个变量。突然冒出一个想法, * 程序启动时候计数,计数器必须存在内存中并运算。 * * 可是程序一结束,计数器就消失了。那么再次启动该程序,计数器又被重新被初始化了。 * * 而我们需要多次启动同一个应用程序,使用的是同一个计数器。 * * 这就需要计数器的生命周期变长。从内存存储到硬盘文件中。 * * * 3.如何使用这个计数器呢? * 首先.程序启动时,应该先读取这个用于记录计数器信息的配置文件。 * * 获取上一次计数器的次数,并进行试用次数的判断 * 其次。对该次数进行自增,并自增后的次数重新存储到配置文件中。 * * 4.文件中的信息该如何进行存储并体现呢。 * 直接存储次数值可以。但是不明确该数据的含义。所以起名字就变得很重要了。 * 这就有了名字和值的对应,所以可以使用键值对。 * 可以映射关系map集合搞定。又需要读取硬盘上的数据,所以map+io=Properties。 * * * * */ public class Io46_1 { public static void main(String[] args) throws Exception { geuAppCount(); } public static void geuAppCount() throws Exception{ //将配置文件封装成File对象 File confile=new File("count.properties"); if(!confile.exists()){ confile.createNewFile(); } FileInputStream fis=new FileInputStream(confile); Properties prop=new Properties(); prop.load(fis); //从集合中通过键获取次数。 String value=prop.getProperty("time"); //定义计数器,记录获取到的次数 int count=0; if(value!=null){ count =Integer.parseInt(value); if(count>=5){ // System.out.println("使用次数已到,请注册"); // return ; throw new RuntimeException("使用次数已到,请注册"); } System.out.println("第"+count+"次使用"); } count++; //将改变后的次数重新存储到集合中, prop.setProperty("time", count+""); FileOutputStream fos=new FileOutputStream(confile); prop.store(fos, ""); fos.close(); fis.close(); } }
相关文章推荐
- java对世界各个时区(TimeZone)的通用转换处理方法(转载)
- java-注解annotation
- java-模拟tomcat服务器
- java-用HttpURLConnection发送Http请求.
- java-WEB中的监听器Lisener
- Android IPC进程间通讯机制
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- 介绍一款信息管理系统的开源框架---jeecg
- 聚类算法之kmeans算法java版本
- java实现 PageRank算法
- PropertyChangeListener简单理解
- c++11 + SDL2 + ffmpeg +OpenAL + java = Android播放器
- 插入排序
- 冒泡排序
- 堆排序
- 快速排序
- 二叉查找树