---------------------- ASP.Net+Android+IOS開發、.Net培訓、期待與您交流!
----------------------
用於記錄應用程序運行次數。
如果使用次數已到,那麼給出註冊提示。我們思想上很容易想到的是:計數器。
可是該計數器定義在程序中,隨着程序的運行而在內存中存在,並進行自增。
可是隨着該應用程序的退出,該計數器也就在內存中消失了。
下一次在啓動該程序,又重新開始從0計數。
這樣的結果不是我們想要的。
程序即使結束,該計數器的值也存在。
下一次程序啓動會先加載該計數器的值並加1後再重新存儲起來。
所以要建立一個配置文件。用於記錄該軟件使用的次數。
該配置文件使用鍵值對的形式。
這樣便於閱讀數據並操作數據。
鍵值對數據時map集合。
數據時以文件形式存儲,使用IO技術。
那麼map+io-->properties.
配置文件可以實現應用程序數據的共享。
import java.io.*;
import java.util.*;
class RunCount
{
public static void main(String[] args)
{
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();
}
}
---------------------- ASP.Net+Android+IOS開發、.Net培訓、期待與您交流!
----------------------詳細請查看:http://edu.csdn.net