简单图片的计数器也可用java实现


利用java实现简单图片的计数器,运行图:




 



计数器

//同步更新计数器


synchronized void counter(){ 

       ServletContext application=getServletContext();   //构造application对象(可选)

        String szPath=application.getRealPath("/");       //得到当前路径

                szPath=szPath+"hits.txt";                //计数器文件 0-9999999999999...

        String szRecord="";                               //记数 String

        int nRecord=0;                             //记数 int

        try{ 

           BufferedReader file=new BufferedReader(new FileReader(szPath));

            szRecord=file.readLine();                     //读取计数器文件

        }

        catch(Exception e){ 

           System.out.println(e); 

       }

  if(szRecord==null){

   szRecord="0";                                         //如果计数器文件为空

        } 

       nRecord=java.lang.Integer.parseInt(szRecord)+1;    //计数器+1

  try{  

          File f=new File(szPath);

            PrintWriter pw=new PrintWriter(new FileWriter(f));

            pw.print(nRecord); //写文件。这种频繁写文件保存记数的方法,不可取。 

           pw.close();

        } 

       catch(Exception e){  

          System.out.println(e); 

       } 

   }  

%>

//显示计数器
    if(session.isNew()){                               //如果是新会话
        counter();
    }    
    String Path=application.getRealPath("/");
    // out.println(Path);
    String szPath=Path+"hits.txt";
    String szRecord="";
    BufferedReader file=new BufferedReader(new FileReader(szPath));
    try{
        szRecord=file.readLine();
        if(szRecord==null){
           szRecord="0";  
        }
    }
    catch(Exception e){
        System.out.println(e);
    }
 //显示7位数字gif图像
    String szOut="

当前访问量:";
    int i=0;
    int k=7-szRecord.length();                          //"0"的个数
    for (i=0;i         szOut=szOut+"";
    }        
    for (i=0;i         szOut=szOut+"";
    }
    szOut=szOut+"";
    out.println(szOut);     
%>

想了解更多java学习课程,可以关注e良师益友网。

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/29881142/viewspace-1327230/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/29881142/viewspace-1327230/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值