位置1和位置2有什么区别呢?
为何放到位置1数据一直停留在1,存不进去,放到位置2就可以了呢?
public static void main(String[] args) throws Exception
{
properitiesTest();
}
private static void properitiesTest() throws IOException
{
//创建文件对象
File confie = new File("confie.properties");
//判断文件是否存在,如果不存在重新创建
if(!confie.exists())
{
confie.createNewFile();
}
//包装文件
FileInputStream fis = new FileInputStream(confie);
//位置1-》 FileOutputStream fos = new FileOutputStream(confie);
//创建Properties对象
Properties pro = new Properties(); //用Properties对象加载文件,取得数据
pro.load(fis);
//创建一个计数器变量,一个String变量取得数据用于判断alue值是否为空
int count = 0;
String s = pro.getProperty("count");
//判断取得数据是否为空
if(s != null)
{
count = Integer.parseInt(s);
System.out.println("if"+count);
if(count >= 5)
throw new RuntimeException("使用已经超过5次,请用正版软件");
}
//如果为空,或者使用次数没有超过5次,则计数器加加
count++;
System.out.println(count);
//改变后的数据重新写入其中
FileOutputStream fos = new FileOutputStream(confie); //位置2-》
pro.setProperty("count", count+"");
//存入其中
pro.store(fos, "");
//关闭资源
fis.close();
fos.close();
}
为何放到位置1数据一直停留在1,存不进去,放到位置2就可以了呢?
public static void main(String[] args) throws Exception
{
properitiesTest();
}
private static void properitiesTest() throws IOException
{
//创建文件对象
File confie = new File("confie.properties");
//判断文件是否存在,如果不存在重新创建
if(!confie.exists())
{
confie.createNewFile();
}
//包装文件
FileInputStream fis = new FileInputStream(confie);
//位置1-》 FileOutputStream fos = new FileOutputStream(confie);
//创建Properties对象
Properties pro = new Properties(); //用Properties对象加载文件,取得数据
pro.load(fis);
//创建一个计数器变量,一个String变量取得数据用于判断alue值是否为空
int count = 0;
String s = pro.getProperty("count");
//判断取得数据是否为空
if(s != null)
{
count = Integer.parseInt(s);
System.out.println("if"+count);
if(count >= 5)
throw new RuntimeException("使用已经超过5次,请用正版软件");
}
//如果为空,或者使用次数没有超过5次,则计数器加加
count++;
System.out.println(count);
//改变后的数据重新写入其中
FileOutputStream fos = new FileOutputStream(confie); //位置2-》
pro.setProperty("count", count+"");
//存入其中
pro.store(fos, "");
//关闭资源
fis.close();
fos.close();
}
解决方案 »
- 怎样可以在用BufferedWriter往文件流里写入时换行?
- 这样的语句怎么可以编译正确呢?关于main方法外有一个执行语句!
- 请问如何写一个含有vb控件的excel文件啊?
- 如何按字节复制
- 关于多维数组
- AudioClip的问题(它是抽象借口,但是不用实例化就能用??)
- 如何在一个新弹出的Internal frame中画曲线?
- 一个月连加3颗星,高兴,大量散分!!!!!
- 一个关于jTextArea获取焦点的问题
- 请问 JAVA做 WEB的系统主要用那些技术呢?做比如ERP,MIS等系统
- 如何让sublime text 2运行程序时打开命令行窗口?
- JFrame添加皮肤后JTable数据显示出错
因为文件锁的原因这句必须写在
pro.load(fis);的后面