package io;
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 PropertyTimes {
public static void main(String[] args) {
File file = new File("d:\\heimaio\\time.properties");
if(!file.exists()){
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
FileInputStream fis =null;
FileOutputStream fos=null;
try {
fis = new FileInputStream(file);
fos = new FileOutputStream(file);
Properties p = new Properties();
p.load(fis);
// 获取times值并判断。判断配置文件中times键对应的值是否大于5,怎么在输出到 time.properties文件中times对应的值总是0。
//帮忙解决下
String value = p.getProperty("times");
System.out.println(value);
int times=0;
if(value==null){
p.setProperty("times", times+"");
}else{
times =Integer.parseInt(value);
if(times>=5){
System.out.println("超了");
return;
}
times++;
p.setProperty("times", times+"");
}
p.store(fos, "次数");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
|