日期:2014-05-18  浏览次数:20624 次

jsp/javaBean读写文件的路径问题?
我做了一个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());   }
}
}

------解决方案--------------------
使用绝对路径如:C:\\eclipse\\workspace\\itemExamination\\WebRoot\\count.txt
------解决方案--------------------
发了我的qq给你