我做了一个javaBean计数器,jsp调用如下:
<jsp:useBean id="counter" scope="request" class="com.jsq.Counter">
</jsp:useBean>
<%
   //调用counter对象的readfile方法来读取文件lyfcount.txt中的计数
  String cont=counter.ReadFile("count.txt");
   //调用counter对象的readfile方法来将计数器加一后写入到文件lyfcount.txt中
  counter.WriteFile("count.txt",cont);
%>
不管我用相对路径还是绝对路径都不对,先后用了/count.txt,./count.txt等,并且在每一级目录下都放了count.txt文件,都报错:
type Status report
message lyfcount.txt (系统找不到指定的文件。)
description The requested resource (lyfcount.txt (系统找不到指定的文件。)) is not available.---------javaBean文件------package com.jsq;
import java.io.*; 
public class Counter extends Object {
private String currentRecord = null;
private BufferedReader file;
private String path;

public Counter() {
}
public String ReadFile(String filePath) throws FileNotFoundException{
path = filePath;
file = new BufferedReader(new FileReader(path));
String returnStr =null;
try {
currentRecord = file.readLine();}
catch (IOException e)
{
System.out.println("读取错务"); }
if (currentRecord == null)
returnStr = "没有任何记录";
else{
returnStr =currentRecord; }
return returnStr; 
}
public void WriteFile(String filePath,String counter) throws FileNotFoundException
{
path = filePath; 
int Writestr = Integer.parseInt(counter)+1; 
try { 
PrintWriter pw = new PrintWriter(new FileOutputStream(filePath));
pw.println(Writestr); 
pw.close(); 
}catch(IOException e) {
System.out.println("错误"+e.getMessage()); }
}
}

解决方案 »

  1.   

    报错内容应该是:
    type Status report
    message count.txt (系统找不到指定的文件。)
    description The requested resource (count.txt (系统找不到指定的文件。)) is not available.
      

  2.   

    使用绝对路径如:C:\\eclipse\\workspace\\itemExamination\\WebRoot\\count.txt
      

  3.   

    访问http://191.168.12.68/jsq/test.jsp
    server.xml中的appBase="E:\0MyProjects\bbs\"
    E:\0MyProjects\bbs\目录下:
    \jsq\文件夹,下面有
    \WEB-INF\文件夹
    test.jsp文件WEB-INF\classes\com\jsq\Counter.class
    我在\bbs\,\jsq\,\WEB-INF\,\classes\,\jsq\都放了count.txt,还是报“系统找不到指定的文件”
      

  4.   

    要放到包里面 与class 一起
      

  5.   

    !!
    我先前用的是“E:\0MyProjects\bbs\jsq\count.txt”
    改成"E:\\0MyProjects\\bbs\\jsq\\count.txt"就好了,为什么要“\\”两个斜杠,相对路径怎么写?
      

  6.   

    to:interpb(曾曾胡,深怕情多累美人!) 
    我放到包里面 与class 一起,同样报错
      

  7.   

    server.xml中的appBase="E:\0MyProjects\bbs\"
    E:\0MyProjects\bbs\jsq\
    -->jsq\
          |---WEB-INF\
          |---test.jsp
          |---count.txt
          |
    浏览器访问是http://191.168.12.68/jsq/test.jsp
    这样的,我test.jsp中路径这样写?
    ---------------------------------------------------------------------
    <jsp:useBean id="counter" scope="request" class="com.jsq.Counter">
    </jsp:useBean>
    <%
    //调用counter对象的readfile方法来读取文件lyfcount.txt中的计数
    String cont=counter.ReadFile("file/count.txt");
    //调用counter对象的readfile方法来将计数器加一后写入到文件lyfcount.txt中
    counter.WriteFile("file/count.txt",cont);
    %>
      

  8.   

    String cont=counter.ReadFile("E:/0MyProjects/bbs/jsq/count.txt");
    counter.WriteFile("E:/0MyProjects/bbs/jsq/count.txt",cont);
    这样写没有错误!
      

  9.   

    你的本意是想把 txt文件放到哪里
      

  10.   

    E:\0MyProjects\bbs\jsq\目录下面,qq加了你
      

  11.   

    鸟tomcat,
    out.println(System.getProperty("user.dir")+"<br>");解决