回 帖 发 新 帖 刷新版面

主题:这个问题帮我看一下,不知道错在哪里了????

FileWRBean.java


package ch08;
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 WriteOver(){ 
    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;
         }
}
}










Sample12.jsp
<%@ page language="java" contentType="text/html;charset=gb2312"%>
<jsp:useBean id="fwrBean" class="ch08.FileWRBean" scope="session"/>
<%@ page import="java.io.*"%>
<html>
<head>
<title>计数器</title>
</head>
<body>
<center>
<% 
String path=request.getRealPath("/")+"file3.txt";
fwrBean.setPath(path);
fwrBean.ReadFile();
int count=Integer.parseInt(fwrBean.getStr_read())+1;
String str_writtn=String.valueOF(count);
fwrBean.setstr_written(str_writtn);
fwrBean.WriteOver();
%>
总访问人次为:<%=str_writtn%>
</center>
</body>
</html>

回复列表 (共1个回复)

沙发

帮你修改了一下:很多拼写错误,你那个文件路径呢?是界面输入,还是写在程序里面呢?
getPah()的结果呢?

package ch08;
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 WriteOver(){ 
    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;
         }
}
}


<%@ page language="java" contentType="text/html;charset=gb2312"%>
<%@ page import="ch08.FileWRBean" %> 
<%@ page import="java.io.*"%>
<jsp:useBean id="fwrBean" class="ch08.FileWRBean" scope="session"/>

<html>
<head>
<title>计数器</title>
</head>
<body>

<center>
<% 
String path=request.getRealPath("/")+"file3.txt";
fwrBean.setPath(path);
fwrBean.ReadFile();
int count=Integer.parseInt(fwrBean.getStr_read())+1;
String str_writtn=String.valueOf(count); //这里Of不是OF
fwrBean.setStr_written(str_writtn);//这里是setStr_written
fwrBean.WriteOver();
%>
总访问人次为:<%=str_writtn%>
</center>
</body>
</html>

[url=http://www.programfan.com/club/showbbs.asp?id=207041]帮忙支持一下[/url]

我来回复

您尚未登录,请登录后再回复。点此登录或注册