以前写过计数器的代码,一直没能解决,今天重新拿起,还是出现了同样郁闷的错误,不得不向兄弟们求救,但声明哦,我这没分了,无能给你分,有兴趣的话继续看如下代码:
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.io.*"%>
<% BufferedReader inf = new BufferedReader(new FileReader("/counter.txt"));
int tmp = Integer.parseInt(inf.readLine());
int i=0;
try 
{
i = Integer.parseInt(request.getSession().getValue("tal").toString());
} catch (NullPointerException t) {i=0; }if (i==0) {
tmp++;PrintWriter outf = new PrintWriter(new BufferedWriter(new FileWriter("/counter.txt")));
outf.println(tmp);
outf.close();
inf.close();
request.getSession().putValue("tal", "1");
}BufferedReader inf2 = new BufferedReader(new FileReader("/counter.txt"));
%>
<%
String zeroes="";
String hits = inf2.readLine();
inf2.close();
for (int t=0; t < 8-hits.length(); t++) {
zeroes=zeroes+"0"; }
out.println(zeroes + hits);
%>错误提示是:    \counter.txt (系统找不到指定的文件。)
我也用过bean来计数,出现的是同样的错误,我用的是Tomcat5.0,谢谢

解决方案 »

  1.   

    counter.txt就放在与jsp文件相同的目录(web目录)下
      

  2.   

    ======================================================
    <%@page contentType="text/html;charset=gb2312" %>
    <%@page import="java.io.*" %>
    <html>
    <body>
      <%! int number=0;
        synchronized void countpeople()
        {
          if(number==0)
          {
             try
                {
                   File f=new File("d:/","count.txt");
                   FileInputStream in=new FileInputStream(f);
                   DataInputStream datain=new DataInputStream(in);
                   number=datain.readInt();
                   number++;
                   in.close();datain.close();
                }
            catch(FileNotFoundException e)
                {
                  number++;
                  try
                    {
                      File f=new File("d:/","count.txt");
                      FileOutputStream out=new FileOutputStream(f);
                      DataOutputStream dataout=new DataOutputStream(out);
                      dataout.write(number);
                      out.close();dataout.close();
                    }
                 catch(Exception ee)   
                    {}
                }
            catch(Exception ee)   
                    {}          
          }
         else
          {
            number++;
            try
                    {
                      File f=new File("d:/","count.txt");
                      FileOutputStream out=new FileOutputStream(f);
                      DataOutputStream dataout=new DataOutputStream(out);
                      dataout.write(number);
                      out.close();dataout.close();
                    }
            catch(Exception e)   
                   {}     
          } 
        }
       %>
       <%
         if (session.isNew())
            {countpeople();
             String str=String.valueOf(number);
             session.setAttribute("count",str);
            }
       %>
       您是第<%=(String)session.getAttribute("count") %>wei ~
    </body>
    </html>
    ================================================================
    你自己参考吧