本帖最后由 吴刚 于 2013-6-9 22:29 编辑 import java.io.*;
import java.util.*;
class RunCount
{
public static void main(String[] args) throws IOException
{
Properties prop = new Properties();
File file = new File("count.ini");
if(!file.exists())
file.createNewFile();
FileInputStream fis = new FileInputStream(file);
prop.load(fis);
int count = 0;
String value = prop.getProperty("time");
if(value!=null)
{
count = Integer.parseInt(value);
if(count>=5)
{
System.out.println("您好,使用次数已到,拿钱!");
return ;
}
}
count++;
prop.setProperty("time",count+"");
FileOutputStream fos = new FileOutputStream(file);
prop.store(fos,"");
fos.close();
fis.close();
}
} 我看了老毕的视频,然后自己敲了一遍,发现自己把标注的那个代码放在了前面几行,结果就不能计数了,把它移到下面又可以了?怎么回事???
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;
public class CountDemo1 {
/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
Properties prop = new Properties();
File file = new File("c:\\b.txt");
if (!file.exists()) {
file.createNewFile();
}
FileInputStream fis = new FileInputStream(file);
prop.load(fis);
int count = 0;
prop.setProperty("time", count+"");
String value = prop.getProperty("time");
if (value != null) {
count = Integer.parseInt(value);
if (count>=5) {
System.out.println("软件到期了");
return;
}
}
count++;
FileOutputStream fos = new FileOutputStream(file);
prop.store(fos, "hahhaha");
fis.close();
fos.close();
}
}
|