A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

本帖最后由 靓仔 于 2013-12-22 16:23 编辑

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");//第一次没有time为null
                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();
               
        }
}

1.jpg (54.72 KB, 下载次数: 14)

1.jpg

评分

参与人数 1技术分 +1 收起 理由
乔兵 + 1

查看全部评分

2 个回复

正序浏览
参考:
Properties是hashtable的子类。

也就是说它具备map集合的特点。而且它里面存储的键值对都是字符串。

是集合中和IO技术相结合的集合容器。

该对象的特点:可以用于键值对形式的配置文件。

那么在加载数据时,需要数据有固定格式:键=值。



1.    构造方法

       Properties()

       Properties(Properties defaults)

2.    常用方法***

       String getProperty(String key)

              //  用指定的键在此属性列表中搜索属性。

       String getProperty(String key, String defaultValue)

              //  用指定的键在此属性列表中搜索属性。



       list(PrintStream out)

       list(PrintWriter out)

              //将属性列表输出到指定的输出流。



       load(InputStream inStream)

              //从输入流中读取属性列表(键和元素对)。

       load(Reader reader)

              //按简单的面向行的格式从输入字符流中读取属性列表(键和元素对)。



       setProperty(String key, String value)//添加属性(键和元素对)



       store(OutputStream out, String comments)

              //将此 Properties 表中的属性列表(键和元素对)写入输出流。

       store(Writer writer, String comments)

              //将此 Properties 表中的属性列表(键和元素对)写入输出字符。



       stringPropertyNames()

              //返回此属性列表中的键集,其中该键及其对应值是字符串



3.    例

      

import java.io.*;

import java.util.*;



class PropertiesDemo

{

       public static void main(String[] args) throws IOException

       {

              //method_1();

              loadDemo();

       }



       public static void loadDemo()throws IOException

       {

              Properties prop = new Properties();

              FileInputStream fis = new FileInputStream("info.txt");\\读取流



              //将流中的数据加载进集合。

              prop.load(fis);\\读取



              prop.setProperty("wangwu","39");\\修改,或设置



              FileOutputStream fos = new FileOutputStream("info.txt");\\输出流



              prop.store(fos,"haha");\\写入输入流,存储



       //     System.out.println(prop);

              prop.list(System.out);\\输出



              fos.close();

              fis.close();



       }



       //演示,如何将流中的数据存储到集合中。  原理

       //想要将info.txt中键值数据存到集合中进行操作。

       /*

              1,用一个流和info.txt文件关联。

              2,读取一行数据,将该行数据用"="进行切割。

              3,等号左边作为键,右边作为值。存入到Properties集合中即可。



       */

       public static void method_1()throws IOException

       {

              BufferedReader bufr = new BufferedReader(new FileReader("info.txt"));



              String line = null;

              Properties prop = new Properties();





              while((line=bufr.readLine())!=null)

              {

                     String[] arr = line.split("=");

                     ///System.out.println(arr[0]+"...."+arr[1]);

                     prop.setProperty(arr[0],arr[1]);

              }



              bufr.close();



              System.out.println(prop);

       }







//     设置和获取元素。

       public static void setAndGet()

       {

              Properties prop = new Properties();



              prop.setProperty("zhangsan","30");

              prop.setProperty("lisi","39");



//            System.out.println(prop);

              String value = prop.getProperty("lisi");

              //System.out.println(value);

                    

              prop.setProperty("lisi",89+"");



              Set<String> names = prop.stringPropertyNames();

              for(String s : names)

              {

                     System.out.println(s+":"+prop.getProperty(s));

              }

       }

}





4.     

      

/*

用于记录应用程序运行次数。

如果使用次数已到,那么给出注册提示。



很容易想到的是:计数器。

可是该计数器定义在程序中,随着程序的运行而在内存中存在,并进行自增。

可是随着该应用程序的退出,该计数器也在内存中消失了。



下一次在启动该程序,又重新开始从0计数。

这样不是我们想要的。



程序即使结束,该计数器的值也存在。

下次程序启动在会先加载该计数器的值并加1后在重新存储起来。



所以要建立一个配置文件。用于记录该软件的使用次数。



该配置文件使用键值对的形式。

这样便于阅读数据,并操作数据。



键值对数据是map集合。

数据是以文件形式存储,使用io技术。

那么map+io -->properties.



配置文件可以实现应用程序数据的共享。



*/

import java.io.*;

import java.util.*;



class Test

{

       public static void main(String[] args)

       {

              Properties pro = new Properties();

              File file = new File("count.ini");

              if(!(file.exists()))

              {

                     try

                     {

                            file.createNewFile();                  

                     }

                     catch (IOException ioe)

                     {

                            throw new RuntimeException("文件创建失败");

                     }



              }

              FileReader fr = null;

              try

              {

                     fr = new FileReader("count.ini");

                     pro.load(fr);

                     int count=0;

                     String value = pro.getProperty("time");

                     if(value!=null)

                     {

                            count = Integer.parseInt(value);

                            if(count>=5)

                            {

                                   System.out.println("试用次数完毕");

                                   return;

                            }

                     }

                     count++;

                     pro.setProperty("time",count+"");

                     PrintWriter pw = null;

                     try

                     {

                            pw = new PrintWriter(file);

                            pro.store(pw,"try");                                 

                     }

                     catch (IOException ioe)

                     {

                            throw new RuntimeException("写入失败");

                     }

                     finally

                     {

                             pw.close();    //不抛异常                                

                     }

              }

              catch (IOException ioe)

              {

                     throw new RuntimeException("读取失败");

              }

              finally

              {

                     try

                     {

                            if(fr!=null)

                                   fr.close();                           

                     }

                     catch (IOException ioe)

                     {

                            throw new RuntimeException("读取流关闭失败");

                     }

              }

       }

}

评分

参与人数 1技术分 +1 收起 理由
乔兵 + 1

查看全部评分

回复 使用道具 举报
程序设定一个计数器count统计程序运行的次数。
程序启动会先加载该计数器的值,并加1后重新存储起来。
所以要建立一个配置文件,用于记录该软件的使用次数。
该配置文件使用键值对的形式,这样便于阅读数据并操作数据。
键值对数据是map集合。


当程序启动时使用:prop.load(fis);//将流中的数据加载到集合中String value = prop.getProperty("time");//获取map集合中time键对于的值value。count = Integer.parseInt(value);//由于获取到的是String类型的数据,对数据进行转换。
来获取当前程序已经使用的次数count。
当程序运行结束时使用:
count++;//计数器的值加1
prop.setProperty("time" , count+"");//将集合中计数器的value值进行修改。



prop.store(fos , "");//将修改后的数据写入文件。
配置文件就这样实现程序使用次数count的共享。

评分

参与人数 1技术分 +1 收起 理由
乔兵 + 1

查看全部评分

回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马