运行此计数器程序是为何出错?

jeanyli 2003-02-12 12:12:58
<%@ page contentType="text/html;charset=gb2312" %>
<html>
<head>
<title>jsp计数器</title>
</head>
<body>
<%@ page import="java.io.*" %>
<%
//out.println(request.getHeader("Cookie"));
String currentRecord = null;//保存文本的变量
BufferedReader file; //BufferedReader对象,用于读取文件数据
String nameOfTextFile = "count.txt";

//读取
file = new BufferedReader(new FileReader(nameOfTextFile));
String readStr =null;
int writeStr =0; //如果计数文本中的计数值为空则让它显示时变成1并写入
try
{ readStr = file.readLine(); }
catch (IOException e)
{ System.out.println("读取数据错误."); }
if (readStr == null) readStr = "没有任何记录";

//判断cookie,第一次登陆时加1,刷新时不累计计数
else if (request.getHeader("Cookie")==null)
{ writeStr = Integer.parseInt(readStr)+1;}
else
{ writeStr = Integer.parseInt(readStr);}

//写入时控制因为刷新引起的重复计数
if (request.getHeader("Cookie")==null)
{
try {
PrintWriter pw = new PrintWriter(new FileOutputStream(nameOfTextFile));
pw.println(writeStr);
pw.close();}
catch(IOException e) {
out.println(e.getMessage());}
}
%>
<p align="center">您是第<b><font color="red"><%=writeStr%></font></b>位客人。</p>
</body>
</html>


出错显示;

java.io.FileNotFoundException: count.txt (系统找不到指定的文件。)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:103)
at java.io.FileInputStream.<init>(FileInputStream.java:66)
at java.io.FileReader.<init>(FileReader.java:41)
at _waiwang_0jsp._coun__jsp._jspService(/waiwang_jsp/coun.jsp:16)
at com.caucho.jsp.JavaPage.service(JavaPage.java:74)
at com.caucho.jsp.Page.subservice(Page.java:485)
at com.caucho.server.http.FilterChainPage.doFilter(FilterChainPage.java:181)
at com.caucho.server.http.Invocation.service(Invocation.java:291)
at com.caucho.server.http.CacheInvocation.service(CacheInvocation.java:132)
at com.caucho.server.http.HttpRequest.handleRequest(HttpRequest.java:217)
at com.caucho.server.http.HttpRequest.handleConnection(HttpRequest.java:159)
at com.caucho.server.TcpConnection.run(TcpConnection.java:136)
at java.lang.Thread.run(Thread.java:536)

count.txt与此.jsp文件放同一目录。

...全文
35 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
hireling 2003-02-12
  • 打赏
  • 举报
回复
把读写文件逻辑抽取到一个BEAN 里面。
JSP页面访问该BEAN 。即可。
zxhong 2003-02-12
  • 打赏
  • 举报
回复
不在同一目录时要指出绝对路径(eg:c:\)
也可以用相对路径:
String s = request.getRealPath("\\count\\"+count.txt);
(count目录在虚拟目录下)
tripofdream 2003-02-12
  • 打赏
  • 举报
回复
jsp文件的运行时路径跟count.txt不在同一目录

81,094

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧