package cn.itcast.other;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;
import java.util.Set;
public class Demo4 {
public static void main(String[] args) throws IOException {
creatProperties();
}
public static void readProperties() throws IOException{
Properties properties = new Properties();
properties.load(new FileReader("F:\\persons.properties"));
Set<Entry<Object, Object>> entrys = properties.entrySet();
for(Entry<Object, Object> entry :entrys){
System.out.println("键:"+ entry.getKey() +" 值:"+ entry.getValue());
}
properties.setProperty("狗娃", "007");
properties.store(new FileWriter("F:\\persons.properties"), "hehe");
}
public static void creatProperties() throws IOException{
Properties properties = new Properties();
properties.setProperty("狗娃", "123");
properties.setProperty("狗剩","234");
properties.setProperty("铁蛋","345");
properties.store(new FileWriter("F:\\persons.properties"), "hehe");
}
}
package cn.itcast.other;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;
public class Demo5 {
public static void main(String[] args) throws IOException {
File file = new File("F:\\count.properties");
if(!file.exists()){
file.createNewFile();
}
Properties properties = new Properties();
properties.load(new FileInputStream(file));
FileOutputStream fileOutputStream = new FileOutputStream(file);
int count = 0;
String value = properties.getProperty("count");
if(value!=null){
count = Integer.parseInt(value);
}
if(count==3){
System.out.println("你已经超出了试用次数,请购买正版软件!!");
System.exit(0);
}
count++;
System.out.println("你已经使用了本软件第"+count+"次");
properties.setProperty("count",count+"");
properties.store(fileOutputStream,"runtime");
}
}