您的位置:首页 > 编程语言 > Java开发

写入配置文件并读取值demo

2018-03-20 14:06 218 查看
package IO;
import java.io.*;
import java.util.Properties;

import javax.management.RuntimeErrorException;

public class PropertiesTest {

/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
getAppCount();
}

public static void getAppCount() throws IOException {
File config = new File("count.Properties");
if (!config.exists()) {
config.createNewFile();
}
FileInputStream fis = new FileInputStream(config);
Properties prop = new Properties();
prop.load(fis);
String value = prop.getProperty("time");
int count=0;

if (value!=null) {
count = Integer.parseInt(value);
if (count>=10) {
throw new RuntimeException("请注册!!!");
}
}
count++;
int use=10-count;
System.out.println("还有"+use+"次使用机会!!");
prop.setProperty("time", count+"");
FileOutputStream fos = new FileOutputStream(config);
prop.store(fos, "");
fos.close();
fis.close();
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java