雅典之夏的小站
知人者智 自知者明 Fighting!!

把以下代码粘贴到首页中,就可以从counter.txt中读取访问量。
一定要注意路径的问题,刚开始直接用counter.txt,结果总是找不到文件,而且用绝对路径也不行,
但是用/counter.txt就没有问题
<%!
int number=0;
    synchronized void countPeople(){
        if(number==0){
            try{
                FileInputStream in=new FileInputStream("/counter.txt");
                DataInputStream dataIn=new DataInputStream(in);
                number=dataIn.readInt();
                number++;
                in.close();
                dataIn.close();
            }catch(FileNotFoundException e){
    
                number++;
                try{
                    FileOutputStream out=new FileOutputStream("/counter.txt");
                    DataOutputStream dataOut=new DataOutputStream(out);
                    dataOut.writeInt(number);
                    out.close();
                    dataOut.close();
                }catch(IOException ffe){
                    System.out.print(ffe);
                }
            }catch(IOException ee){
   System.out.println(ee);
            }
            }else{
            number++;
            try{
                FileOutputStream out=new FileOutputStream("/counter.txt");
                DataOutputStream dataOut=new DataOutputStream(out);
                dataOut.writeInt(number);
                out.close();
                dataOut.close();
            }catch(FileNotFoundException e){
    System.out.println(e);
   }
            catch(IOException e){
    System.out.println(e);
   }

        }
    }

%>
<% countPeople();%>

posted on 2006-01-19 11:26 rkind 阅读(322) 评论(0)  编辑  收藏 所属分类: JAVA基础&数据库

只有注册用户登录后才能发表评论。


网站导航: