properties集合的简单例子
2018-02-04 22:50
295 查看
package com.lxb.test;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.Properties;
import java.util.Set;
public class PropertiesDemo {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
//存储数据
propertiedDemo();
//list方法
propertiedDemo2();
//store方法
propertiedDemo3();
//读取配置文件并修改信息
propertiedDemo4();
//获取一个应用程序运行的次数,超过5次,给出使用次数已到请注册的请求
propertiedDemo5();
}
private static void propertiedDemo5() throws IOException {
// TODO Auto-generated method stub
/**
* 思路:
* 1:应该有计数器
* 每次程序启动都需要计数一次,并且是在原有的次数上进行计数
* 2:计数器就是一个变量
* 3:如何使用计数器
* 程序启动时,应该先读取这个用于记录计数器信息的配置文件
* 获取上一次技术次数,并进行使用次数的判断
* 其次,对该次数进行自增,并将自增后的次数重新存储到配置文件中
* 4:文件中的信息该如何进行存储体现
* 直接存储次数可以,但是不明确该数据的含义,所以起名字就变得很重要、
* 这就有了名字和值的对应,所以可以使用键值对
* 可以映射关系map集合搞定,有需要读取硬盘上的数据,所以map+io=properties
*/
File f=new File("e:\\count.properties");
if (!f.exists()) {
f.createNewFile();
}
Properties prop=new Properties();
/**
* 集合的数据来自于一个文件
* 注意:必须保证文件中的数据是键值对
* 需要用到读取流
*/
FileInputStream fis=new FileInputStream(f);
InputStreamReader isr=new InputStreamReader(fis);
prop.load(isr);
prop.list(System.out);
if(prop.getProperty("count")==null) {
prop.setProperty("count", "1");
FileOutputStream fos=new FileOutputStream(f);
OutputStreamWriter osw=new OutputStreamWriter(fos);
prop.store(osw, "count");
fos.close();
osw.close();
}else {
Integer count=Integer.valueOf(prop.get("count").toString());
if(count==5) {
System.out.println("去注册吧");
}else {
count++;
prop.setProperty("count", count+"");
FileOutputStream fw=new FileOutputStream(f);
OutputStreamWriter osw=new OutputStreamWriter(fw);
prop.store(osw, "count");
fw.close();
osw.close();
}
}
}
private static void propertiedDemo4() throws IOException {
// TODO Auto-generated method stub
Properties prop=new Properties();
/**
* 集合的数据来自于一个文件
* 注意:必须保证文件中的数据是键值对
* 需要用到读取流
*/
FileInputStream fis=new FileInputStream("e:\\demo.txt");
InputStreamReader isr=new InputStreamReader(fis);
prop.load(isr);
prop.list(System.out);
//对已有的配置文件中的信息进行修改
prop.setProperty("03", "赵六");
FileOutputStream fw=new FileOutputStream("e:\\demo.txt");
OutputStreamWriter osw=new OutputStreamWriter(fw);
prop.store(osw, "num+name");
fw.close();
osw.close();
}
private static void propertiedDemo3() throws IOException {
// TODO Auto-generated method stub
Properties prop=new Properties();
//持久化存储到文件中,需要关联输出流
prop.setProperty("01", "张三");
prop.setProperty("02", "李四");
prop.setProperty("03", "王五");
FileOutputStream fos=new FileOutputStream("e:\\demo.txt");
OutputStreamWriter osw=new OutputStreamWriter(fos);
prop.store(osw, "num+name");
fos.close();
}
private static void propertiedDemo2() {
// TODO Auto-generated method stub
Properties prop=new Properties();
//存储元素
prop=System.getProperties();
prop.list(System.out);
}
private static void propertiedDemo() {
// TODO Auto-generated method stub
Properties prop=new Properties();
//存储元素
prop.setProperty("01", "张三");
prop.setProperty("02", "李四");
prop.setProperty("03", "王五");
//取出所有元素
Set<String> set=prop.stringPropertyNames();
for (String string : set) {
System.out.println(string+":"+prop.getProperty(string));
}
}
}
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.Properties;
import java.util.Set;
public class PropertiesDemo {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
//存储数据
propertiedDemo();
//list方法
propertiedDemo2();
//store方法
propertiedDemo3();
//读取配置文件并修改信息
propertiedDemo4();
//获取一个应用程序运行的次数,超过5次,给出使用次数已到请注册的请求
propertiedDemo5();
}
private static void propertiedDemo5() throws IOException {
// TODO Auto-generated method stub
/**
* 思路:
* 1:应该有计数器
* 每次程序启动都需要计数一次,并且是在原有的次数上进行计数
* 2:计数器就是一个变量
* 3:如何使用计数器
* 程序启动时,应该先读取这个用于记录计数器信息的配置文件
* 获取上一次技术次数,并进行使用次数的判断
* 其次,对该次数进行自增,并将自增后的次数重新存储到配置文件中
* 4:文件中的信息该如何进行存储体现
* 直接存储次数可以,但是不明确该数据的含义,所以起名字就变得很重要、
* 这就有了名字和值的对应,所以可以使用键值对
* 可以映射关系map集合搞定,有需要读取硬盘上的数据,所以map+io=properties
*/
File f=new File("e:\\count.properties");
if (!f.exists()) {
f.createNewFile();
}
Properties prop=new Properties();
/**
* 集合的数据来自于一个文件
* 注意:必须保证文件中的数据是键值对
* 需要用到读取流
*/
FileInputStream fis=new FileInputStream(f);
InputStreamReader isr=new InputStreamReader(fis);
prop.load(isr);
prop.list(System.out);
if(prop.getProperty("count")==null) {
prop.setProperty("count", "1");
FileOutputStream fos=new FileOutputStream(f);
OutputStreamWriter osw=new OutputStreamWriter(fos);
prop.store(osw, "count");
fos.close();
osw.close();
}else {
Integer count=Integer.valueOf(prop.get("count").toString());
if(count==5) {
System.out.println("去注册吧");
}else {
count++;
prop.setProperty("count", count+"");
FileOutputStream fw=new FileOutputStream(f);
OutputStreamWriter osw=new OutputStreamWriter(fw);
prop.store(osw, "count");
fw.close();
osw.close();
}
}
}
private static void propertiedDemo4() throws IOException {
// TODO Auto-generated method stub
Properties prop=new Properties();
/**
* 集合的数据来自于一个文件
* 注意:必须保证文件中的数据是键值对
* 需要用到读取流
*/
FileInputStream fis=new FileInputStream("e:\\demo.txt");
InputStreamReader isr=new InputStreamReader(fis);
prop.load(isr);
prop.list(System.out);
//对已有的配置文件中的信息进行修改
prop.setProperty("03", "赵六");
FileOutputStream fw=new FileOutputStream("e:\\demo.txt");
OutputStreamWriter osw=new OutputStreamWriter(fw);
prop.store(osw, "num+name");
fw.close();
osw.close();
}
private static void propertiedDemo3() throws IOException {
// TODO Auto-generated method stub
Properties prop=new Properties();
//持久化存储到文件中,需要关联输出流
prop.setProperty("01", "张三");
prop.setProperty("02", "李四");
prop.setProperty("03", "王五");
FileOutputStream fos=new FileOutputStream("e:\\demo.txt");
OutputStreamWriter osw=new OutputStreamWriter(fos);
prop.store(osw, "num+name");
fos.close();
}
private static void propertiedDemo2() {
// TODO Auto-generated method stub
Properties prop=new Properties();
//存储元素
prop=System.getProperties();
prop.list(System.out);
}
private static void propertiedDemo() {
// TODO Auto-generated method stub
Properties prop=new Properties();
//存储元素
prop.setProperty("01", "张三");
prop.setProperty("02", "李四");
prop.setProperty("03", "王五");
//取出所有元素
Set<String> set=prop.stringPropertyNames();
for (String string : set) {
System.out.println(string+":"+prop.getProperty(string));
}
}
}
相关文章推荐
- 集合 Properties 的 简单例子(Spring)
- 集合 Properties 的 简单例子(Spring)
- Hibernate List集合映射简单例子
- MultiActionController,PropertiesMethodNameResolver,ParameterMethodNameResolver简单例子
- Hibernate Map集合映射简单例子
- spring集合类型的setter注入的一个简单例子
- 简单读properties文件的例子
- Hibernate Set集合映射简单例子
- otl 操作 sqlite3 简单例子
- 一个简单html的例子
- 给力!简单!易懂!位运算之求集合的所有子集
- 在Struts2的官方网站上,有doubleselect的用法示例,但是那个例子比较简单。 <s:doubleselect label="doubleselect test2" name="menu" list="#{'fruit':'Nice Fruit
- unity插件开发——一个例子:简单的svn集成
- 一个简单的jQuery回调函数例子
- java接口简单例子
- C#之WCF入门1—简单的wcf例子
- spring拦截器的一个简单例子
- 简单例子分析java类加载顺序
- 简单读取Properties文件的方法
- 一个简单的、基本的、完整的使用biblatex包的例子