(90)Properties在IO中应用:配置文件(store、load)
2017-07-24 09:04
316 查看
Properties:①是HashTable的子类,也就是说它具备map集合的特点,而且它里面存储的键值对都是字符串
②是集合中和IO技术相结合的集合容器。
③该对象的特点,也可以用于键值对形式的配置文件。
加载数据时需要固定的格式。通常是键=值
public Set stringPropertyNames():返回所有键值
一、想要将infor.txt中的键值数据存入到集合中进行操作
思路:
1,用一个流和infor.txt文件关联
2,读取一行数据,将该行数据用“=”进行切割
3,等号左面作为键,右面作为值,存入到Propertied集合中即可
其实这是public void load(InputStream inStream) throws IOException的原理,此方法就能完成如上的寻找键值的过程。其功能是从输入流中读取属性列表。用该方法实现将键值对存入集合中。
public void list(PrintStream out):将属性列表输出到指定的输出流,这个指定的输出流可以是控制台:System.out
public void store(OutputStream out, String comments) throws IOException : 以适合使用load(InputStream)方法加载到Properties表中,将此Properties表中的属性列表(键值对)写入输出流。
在内存(Properties)中修改数据,存入到文件中
需求:用于记录应用程序运行次数,如果使用次数一道,那么给出注册提示。
很容易想到的是:计数器
可是若该计数器定义在程序中,随着程序的运行而在内存中存在,并进行自增。
可是随着程序的退出,该计数器也就在内存中消失。下一次在启动改程序,又重新开始从0计数,这不是我们想要的。
我们想要的是程序即使结束,该计数器的值也存在,下次程序启动会先加载该计数器的值,并加1后重新存储起来。
所以要建立一个配置文件,用于记录该软件的使用次数,该配置文件使用键值对的形式,这样便于阅读数据和操作数据。
键值对数据是map集合,数据是以文件形式存储,使用io技术,map+io–>properties.配置文件可以实现应用程序的共享
②是集合中和IO技术相结合的集合容器。
③该对象的特点,也可以用于键值对形式的配置文件。
加载数据时需要固定的格式。通常是键=值
public Set stringPropertyNames():返回所有键值
Properties prop=new Properties(); prop.setProperty("zhangsan", "30"); prop.setProperty("lisi", "39"); //String value=prop.getProperty("zhangsan"); //System.out.println(value); Set<String> s=prop.stringPropertyNames(); Iterator<String> it=s.iterator(); while(it.hasNext()) { String str=it.next(); System.out.print("键:"+str); System.out.print("值:"+prop.getProperty(str)); System.out.println(); }
一、想要将infor.txt中的键值数据存入到集合中进行操作
思路:
1,用一个流和infor.txt文件关联
2,读取一行数据,将该行数据用“=”进行切割
3,等号左面作为键,右面作为值,存入到Propertied集合中即可
import java.util.*; import java.io.*; public class propertiesDemo { public static void GetAndSet() { Properties prop=new Properties(); prop.setProperty("zhangsan", 4000 "30"); prop.setProperty("lisi", "39"); //String value=prop.getProperty("zhangsan"); //System.out.println(value); Set<String> s=prop.stringPropertyNames(); Iterator<String> it=s.iterator(); while(it.hasNext()) { String str=it.next(); System.out.print("键:"+str); System.out.print("值:"+prop.getProperty(str)); System.out.println(); } } public static void main(String[] args) { Properties prop=new Properties(); FileReader fr=null; BufferedReader bufr=null; try { fr=new FileReader("E:\\infor.txt"); bufr=new BufferedReader(fr); String line=null; while((line=bufr.readLine())!=null) { String[] str=line.split("="); prop.setProperty(str[0], str[1]); } Set<String> s=prop.stringPropertyNames(); Iterator<String>it=s.iterator(); while(it.hasNext()) { String str=it.next(); System.out.println("键:"+str+" 值:"+prop.getProperty(str)); } }catch(IOException e) { throw new RuntimeException("读取失败"); } finally { try { if(bufr!=null) bufr.close(); }catch(IOException e){ throw new RuntimeException("关闭失败"); } } } }
其实这是public void load(InputStream inStream) throws IOException的原理,此方法就能完成如上的寻找键值的过程。其功能是从输入流中读取属性列表。用该方法实现将键值对存入集合中。
public void list(PrintStream out):将属性列表输出到指定的输出流,这个指定的输出流可以是控制台:System.out
FileInputStream fis=null; try { fis=new FileInputStream("E:\\infor.txt"); Properties prop=new Properties(); //将流中数据加载进集合 prop.load(fis); prop.list(System.out); } }catch(IOException e) { throw new RuntimeException("读取失败"); }关闭资源动作省略
public void store(OutputStream out, String comments) throws IOException : 以适合使用load(InputStream)方法加载到Properties表中,将此Properties表中的属性列表(键值对)写入输出流。
在内存(Properties)中修改数据,存入到文件中
FileInputStream fis=null; try { fis=new FileInputStream("E:\\infor.txt"); Properties prop=new Properties(); //将流中数据加载进集合 prop.load(fis); // prop.list(System.out); prop.setProperty("language", "english"); FileOutputStream fos=new FileOutputStream("E:\\infor.txt"); prop.store(fos, "haha"); prop.list(System.out); }catch(IOException e) { throw new RuntimeException("读取失败"); }关闭资源动作省略
需求:用于记录应用程序运行次数,如果使用次数一道,那么给出注册提示。
很容易想到的是:计数器
可是若该计数器定义在程序中,随着程序的运行而在内存中存在,并进行自增。
可是随着程序的退出,该计数器也就在内存中消失。下一次在启动改程序,又重新开始从0计数,这不是我们想要的。
我们想要的是程序即使结束,该计数器的值也存在,下次程序启动会先加载该计数器的值,并加1后重新存储起来。
所以要建立一个配置文件,用于记录该软件的使用次数,该配置文件使用键值对的形式,这样便于阅读数据和操作数据。
键值对数据是map集合,数据是以文件形式存储,使用io技术,map+io–>properties.配置文件可以实现应用程序的共享
//开始时并没有这个配置文件,所以要先建立文件 File file=new File("E:\\count.ini"); try { if(!file.exists()) file.createNewFile(); } catch (IOException e1) { throw new RuntimeException("配置文件创建失败"); } FileInputStream fis=null; FileOutputStream fos=null; try { Properties prop=new Properties(); fis=new FileInputStream(file); prop.load(fis);//将文件加载到集合 int count=0; String value=prop.getProperty("time");//开始时没有time,则count++,即可 if(value!=null) { count=Integer.parseInt(value); if(count>=5) { System.out.println("免费使用次数已到!!"); return ; } } count++; prop.setProperty("time",count+"");//可以设置之前有的或者没有的键值对 fos=new FileOutputStream(file); prop.store(fos,"注释"); }catch(IOException e) { throw new RuntimeException("文件写入失败"); } finally { try { if(fos!=null) fos.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { if(fis!=null) fis.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
相关文章推荐
- 黑马程序员——Properties读取load(),操作,存储store()配置文件简介
- java非web应用修改 properties/xml配置文件后,无需重启应用即可生效---自动加载
- 读取web应用中properties配置文件(这种方法可能不是最好的)
- 软件开发工程师(JAVA)中级考试大纲-----四(四)Log4J的原理及配置;Log4J常用的API;在项目中应用日志框架Log4J关键类和接口介绍;Java properties配置文件log
- java Io 配置文件Properties练习
- log4j用properties文件配置,无法应用过滤器的解决方法
- JAVA之旅(二十九)——文件递归,File结束练习,Properties,Properties存取配置文件,load,Properties的小练习
- Spring加载properties配置资源文件的应用
- JAVA之旅(二十九)——文件递归,File结束练习,Properties,Properties存取配置文件,load,Properties的小练习
- 42-IO流(Properties集合的-基本功能). 44-IO流(Properties集合的-store方法) load方法 45-IO流(Properties集合的-修改配置信息)
- log4j用properties文件配置,无法应用过滤器的解决方法
- 将properties文件的配置设置为整个Web应用的全局变量实现方法
- ArrayList 与HashSet的比较,及应用反射读取properties配置文件中的数据进行实例化再调用,以及类加载器的使用;还有HashCode的分析,及导致内存泄露,内存溢出的原因之一
- 反射的应用,读取properties配置文件中的数据(普通InputStream方式,类加载方式),再调用运行
- properties配置应用,为什么需要使用properties文件
- JAVA基础 day22 File类的方法和应用 利用递归获取目录下的所有文件 Properties类的应用(和IO技术结合,存取配置文件) 打印流和合并流等功能类的学习应用 切割文件练习
- 编写一个文件,读取 src 下面day12包 下面的my.properties 文件,文件内容如下, className=day12.User 结合 IO、反射知识,完成 配置文件中对象的创建和方法调用。并编写方法,列举出 Student 类中所有的属性、
- java IO 使用Properties设置键值对配置信息 写入文件和从文件中读取
- 读取web应用中properties配置文件(这种方法可能不是最好的)
- Java Properties 类读取配置文件信息