javabean:
package ch10;
import java.io.*;
public class FileWRBean
{
private String path;
private String str_written;
private String str_read;
public FileWRBean()
{
}
public String getPath()
{
return path;
}
public void setPath(String path)
{
this.path=path;
}
public String getStr_written()
{
return str_written;
}
public void setStr_written(String str_written)
{
this.str_written=str_written;
}
public String getStr_read()
{
return str_read;
}
public void setStr_read(String str_read)
{
this.str_read=str_read;
}
public int ReadFile()
{
try
{
FileReader freader=new FileReader(path);
BufferedReader bfdreader=new BufferedReader(freader);
str_read=bfdreader.readLine();
String str_line=bfdreader.readLine();
while(str_line!=null)
{
str_read=str_read+"<br>"+str_line;
str_line=bfdreader.readLine();
}
bfdreader.close();
freader.close();
return 0;
}
catch (IOException e)
{
System.out.println("文件读取错误!");
return -1;
}
}
public int WriterOver()
{
try
{
FileWriter fwriter=new FileWriter(path);
BufferedWriter bfwriter=new BufferedWriter(fwriter);
bfwriter.write(str_written,0,str_written.length());
bfwriter.flush();
bfwriter.close();
return 0;
}
catch (IOException e)
{
System.out.println("文件写入错误!");
return -1;
}
}
public int WriteAppend()
{
try
{
FileWriter fwriter=new FileWriter(path,true);
BufferedWriter bfwriter=new BufferedWriter(fwriter);
bfwriter.newLine();
bfwriter.write(str_written,0,str_written.length());
bfwriter.flush();
bfwriter.close();
    return 0;
}
catch (IOException e)
{
System.out.println("文件写错误!");
return -1;
}
}
}
jsp:<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %>
<jsp:useBean id="fwrBean" class="ch10.FileWRBean" scope="session"/>
<%@ page import="java.io.*"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>计数器</title>
</head><body>
<center>
<%
   String path=request.getRealPath("/")+"ch11"+"file3.txt";
   fwrBean.setPath(path);
   fwrBean.ReadFile();
   int count=Integer.parseInt(fwrBean.getStr_read())+1;
   String str_written=String.valueOf(count);
   fwrBean.setStr_written(str_written);
   fwrBean.WriterOver();
%>
总共访问人次为:<%=str_written%>
</center>
</body>
</html>
执行时,出现了NumberFormatException异常
怎么解决阿?
我在执行前已经给file3.txt存入0了

解决方案 »

  1.   

    String path=request.getRealPath("/")+"ch11"+"file3.txt";
    "ch11/"?
      

  2.   

    前辈!我用断点试过之后,说fwrBean.setPath(path);有问题,但是我的应该没什么问题呀?
      

  3.   

    String path=request.getRealPath("/")+"ch11"+"file3.txt";是你的地址错了
    改为:String path=request.getRealPath("/")+"ch11/"+"file3.txt";
    就可以了
      

  4.   

    好,上午忙没有测试,刚才测了一遍你的程序基本没什么问题,具体错误我估计是你String path=request.getRealPath("/")+"ch11"+"file3.txt"路径的错误,再就是file3.txt你放的位置,我图简单改成了String path=request.getRealPath("/")+"file3.txt"把file3放在工程的WebRoot下就没有问题,我已经测试过了~!
      

  5.   

    不好意思啊,下午不在线。
    ch11文件夹放在tomcat的webapps目录下,file3.txt放在ch11下,jsp文件放在ch11下,javabean也放在ch11->WEB-INF->classes目录下。我试了,还不对呀!把file3.txt放在webapps下也不对呀!
      

  6.   

    呵呵,我刚才又试了一边,确实是这个问题,应该写成String path=request.getRealPath("/")+"file3.txt",我做了个测试。问题解决了,呵呵,给分!
      

  7.   

    呵呵,您胸襟very very big!!!!