一.Properties基础方法学习
package java_Studing_day50_IO流10;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Properties;
import java.util.Set;
public class propertiesStudy {
以下为学习代码:
public propertiesStudy() {
// TODO Auto-generated constructor stub
}
public static void main(String[] args) throws IOException {
//propertiesDemo();
//propertiesDemo2();
//propertiesDemo3();
//propertiesDemo4();
//myLoad();
modify();
}
// TODO Auto-generated method stub
//1.修改已有的配置表中的信息
//读取这个文件
private static void modify() throws IOException {
File file=new File("F:\\PersistentConfigurationFile.txt");
if(file.exists());
BufferedReader bfr=new BufferedReader (new FileReader("F:\\PersistentConfigurationFile.txt"));
Properties prop=new Properties();//新建配置表
prop.load(bfr);//加载本地文件到配置表
prop.setProperty("Renekton", "6300");//修改配置表信息
BufferedWriter bfw=new BufferedWriter(new FileWriter("F:\\PersistentConfigurationFile.txt"));
prop.store(bfw, "name&sale2ndModify"); //存储到本地文件中,完成更新
bfr.close();
bfw.close();
}
public static void myLoad() throws IOException{
Properties prop=new Properties();
BufferedReader bfr=new BufferedReader(new FileReader("F:\\PersistentConfigurationFile.txt"));//关联配置文件
String line=null;
while((line=bfr.readLine())!=null){
if(line.startsWith("#")) //如果不是#开头,则继续,否则退出本次循环
continue;
String []arr=line.split("=");//将第一行的数据用=分割,分别保存在String[]的0位和1位
System.out.println(arr[0]+"-------------"+arr[1]); //打印这个数组
}
bfr.close();
}
public static void propertiesDemo4() throws IOException {
// 集合中的数据来自一个文件中
//注意:必须保证该文件中的数据是键值对
//需要使用到读取流
Properties prop=new Properties();
FileInputStream fis=new FileInputStream("F:\\PersistentConfigurationFile.txt");
BufferedInputStream bfr=new BufferedInputStream(fis);
prop.load(bfr);//load和流关联
prop.list(System.out);
}
public static void propertiesDemo3() throws IOException {
//创建一个properties集合
Properties prop=new Properties();
//存储元素
prop.setProperty("VN", "4800");
prop.setProperty("Jinx", "6300");
prop.setProperty("Renekton", "4800");
prop.setProperty("Ashe", "450");
prop.setProperty("Poppy", "3150");
//想要将这些集合中的字符串键值信息持久化存储到文件中
//需要关联输出流
BufferedWriter bfw=new BufferedWriter(new FileWriter("F:\\PersistentConfigurationFile.txt"));
prop.store(bfw, "HeroName+Sale");//Store和流关联
bfw.close();
}
public static void propertiesDemo2(){
//创建一个properties集合
Properties prop=new Properties();
//存储元素
prop.setProperty("VN", "4800");
prop.setProperty("Jinx", "6300");
prop.setProperty("Renekton", "4800");
prop.setProperty("Ashe", "450");
prop.setProperty("Poppy", "3150");
prop=System.getProperties();
//list方法,将配置列表中的信息输出到一个输出流,这个在调试时很有用
prop.list(System.out);//打印到控制台
}
public static void propertiesDemo(){
//创建一个properties集合
Properties prop=new Properties();
//存储元素
prop.setProperty("VN", "4800");
prop.setProperty("Jinx", "6300");
prop.setProperty("Renekton", "4800");
prop.setProperty("Ashe", "450");
prop.setProperty("Poppy", "3150");
//删除元素
prop.remove("renekton");
//修改元素
prop.setProperty("VN", "6300");
//取出所有元素
Set<String> names=prop.stringPropertyNames();//返回一个set集合
for(String name:names){
String value=prop.getProperty(name);
System.out.println(name+"---------"+value);
}
}
}
2.Properties集合的练习与总结
package java_Studing_day50_IO流10;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;
public class PropertiesPractice {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
getAppCount();//此方法在程序运行前运行,可以监控程序运行的次数
}
public static void getAppCount() throws IOException {
//将配置文件封装成File对象
File config=new File("F:\\Appcounter.properties");
if(config.exists()){
config.createNewFile();
}
FileInputStream fis=new FileInputStream(config);
Properties prop=new Properties();
//加载本地配置表
prop.load(fis);
//从集合中通过键获取次数,得到String类型数据
String value=prop.getProperty("time");
//创建计数器
int count=0;
//如果
if(value!=null){
count=Integer.parseInt(value);//将String类型的次数信息转Int型,然后传给计数器
if(count>=5){
System.out.println("免费试用次数已到,请注册!谢谢!");
throw new RuntimeException("使用次数已到,给钱!") ;
}
}
count++;
//将改变后的次数从新存储到集合中
prop.setProperty("time", count+"");
FileOutputStream fos=new FileOutputStream(config);
//更新,存储到本地
prop.store(fos, "counterModify");
fis.close();
fos.close();
}
}