主题:这个问题帮我看一下,不知道错在哪里了????
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>
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>