我先描述下错误,首先代码本身不报错。可是执行出来的结果是错的。而且我用一个文件来记录访问站点的人数,可是这个文件创建不了。代码如下:请各位高手指点迷津,谢谢大家了!
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="java.io.*" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>统计站点访问人数</title>
</head>
<body>
<%!
int number=0; synchronized void countPeople()
{
if(number==0)
{
try{
System.out.println(number);
File file = new File("D:\test","countPeople.txt");
FileInputStream fis=new FileInputStream(file);
DataInputStream dis=new DataInputStream(fis);

number=dis.readInt();
number++;
fis.close();
dis.close();
}
catch(FileNotFoundException e)
{
number++;
try{
File file=new File("D:\test","countPeople.txt");
FileOutputStream fos=new FileOutputStream(file);
DataOutputStream dos=new DataOutputStream(fos);
dos.writeInt(number);
fos.close();
dos.close();
}
catch(IOException ex)
{

}
}
catch(IOException ex)
{

}
}
else
{
number++;
try{
File file=new File("D:\test","countPeople.txt");
FileOutputStream fos=new FileOutputStream("file");
DataOutputStream dos=new DataOutputStream(fos);
dos.writeInt(number);
fos.close();
dos.close();
}
catch(FileNotFoundException e)
{

}
catch(IOException ex)
{

}
}
}
%><%
if(session.isNew())
{
countPeople();
String str=String.valueOf(number);
session.setAttribute("count",str);
}
%><p>您是第<%=(String)session.getAttribute("count")%>个访问本站的人。</p>
</body>
</html>

解决方案 »

  1.   

    D:\test  --->  d:\\test 或 d:/test
      

  2.   

     把代码放到jsp里面很难调试,建议用redirect把请求发到后台action,这样单步看哪里报错
      

  3.   

    你要先把d:盘上的test目录建好
      

  4.   

    建立了!可是文件生成到eclipse里面去了。为何?
      

  5.   

    你是写成File file = new File("D:\\test","countPeople.txt");这样的?
    用的是Windows操作系统?
      

  6.   

    恩!这个我知道。如果在Linux下就不是这样写了。可是我的问题还是没有解决,谢谢热心的朋友们。真的不知道问题错在哪里了?
      

  7.   

    那你就别学绝对路径了。 你写相对路径就行了。      String dir = servlet.getServletContext().getRealPath("/upload/");    String f =servlet.getServletContext().getRealPath("/upload");
        File create_file = new File(f);
        if(!(create_file.exists())&&!(create_file.isDirectory())){  
               create_file.mkdirs();//如果不存在就创建一个名为 upload 文件夹。
          }
         文件不存在  ,创建一个txt文件就可以。     下面就是你自己写的了。
         File file = new File(dir,"countPeople.txt");
         FileInputStream fis=new FileInputStream(file);
         DataInputStream dis=new DataInputStream(fis);