counter.java 讀寫文件的一個(gè)bean,用于網(wǎng)站讀寫txt格式計(jì)數(shù)器
package fiombean;
import java.io.*;
public class Counter extends Object {
private String currentRecord = null;// 保存文本的變量
private BufferedReader file; // BufferedReader對(duì)象,用于讀取文件數(shù)據(jù)
private String path;// 文件完整路徑名
public Counter() {
}
// ReadFile方法用來讀取文件filePath中的數(shù)據(jù),并返回這個(gè)數(shù)據(jù)
public String ReadFile(String filePath) throws FileNotFoundException {
path = filePath;
// 創(chuàng)建新的BufferedReader對(duì)象
file = new BufferedReader(new FileReader(path));
String returnStr = null;
try {
// 讀取一行數(shù)據(jù)并保存到currentRecord變量中
currentRecord = file.readLine();
} catch (IOException e) {// 錯(cuò)誤處理
System.out.println("讀取數(shù)據(jù)錯(cuò)誤.");
}
if (currentRecord == null)
// 如果文件為空
returnStr = "沒有任何記錄";
else {// 文件不為空
returnStr = currentRecord;
}
// 返回讀取文件的數(shù)據(jù)
return returnStr;
}
// ReadFile方法用來將數(shù)據(jù)counter+1后寫入到文本文件filePath中
// 以實(shí)現(xiàn)計(jì)數(shù)增長(zhǎng)的功能
public void WriteFile(String filePath, String counter)
throws FileNotFoundException {
path = filePath;
// 將counter轉(zhuǎn)換為int類型并加一
int Writestr = Integer.parseInt(counter) + 1;
try {
// 創(chuàng)建PrintWriter對(duì)象,用于寫入數(shù)據(jù)到文件中
PrintWriter pw = new PrintWriter(new FileOutputStream(filePath));
// 用文本格式打印整數(shù)Writestr
pw.println(Writestr);
// 清除PrintWriter對(duì)象
pw.close();
} catch (IOException e) {
//錯(cuò)誤處理
System.out.println("寫入文件錯(cuò)誤" + e.getMessage());
}
}
}
import java.io.*;
public class Counter extends Object {
private String currentRecord = null;// 保存文本的變量
private BufferedReader file; // BufferedReader對(duì)象,用于讀取文件數(shù)據(jù)
private String path;// 文件完整路徑名
public Counter() {
}
// ReadFile方法用來讀取文件filePath中的數(shù)據(jù),并返回這個(gè)數(shù)據(jù)
public String ReadFile(String filePath) throws FileNotFoundException {
path = filePath;
// 創(chuàng)建新的BufferedReader對(duì)象
file = new BufferedReader(new FileReader(path));
String returnStr = null;
try {
// 讀取一行數(shù)據(jù)并保存到currentRecord變量中
currentRecord = file.readLine();
} catch (IOException e) {// 錯(cuò)誤處理
System.out.println("讀取數(shù)據(jù)錯(cuò)誤.");
}
if (currentRecord == null)
// 如果文件為空
returnStr = "沒有任何記錄";
else {// 文件不為空
returnStr = currentRecord;
}
// 返回讀取文件的數(shù)據(jù)
return returnStr;
}
// ReadFile方法用來將數(shù)據(jù)counter+1后寫入到文本文件filePath中
// 以實(shí)現(xiàn)計(jì)數(shù)增長(zhǎng)的功能
public void WriteFile(String filePath, String counter)
throws FileNotFoundException {
path = filePath;
// 將counter轉(zhuǎn)換為int類型并加一
int Writestr = Integer.parseInt(counter) + 1;
try {
// 創(chuàng)建PrintWriter對(duì)象,用于寫入數(shù)據(jù)到文件中
PrintWriter pw = new PrintWriter(new FileOutputStream(filePath));
// 用文本格式打印整數(shù)Writestr
pw.println(Writestr);
// 清除PrintWriter對(duì)象
pw.close();
} catch (IOException e) {
//錯(cuò)誤處理
System.out.println("寫入文件錯(cuò)誤" + e.getMessage());
}
}
}
Counter.jsp文件
<jsp:useBean id="counter" scope="session" class="fiombean.Counter" />
<%
//調(diào)用counter對(duì)象的ReadFile方法來讀取文件count.txt中的計(jì)數(shù)
String url = request.getRealPath("count.txt");
String count = counter.ReadFile(url);
//調(diào)用counter對(duì)象的ReadFile方法來將計(jì)數(shù)器加一后寫入到文件count.txt中
if (session.isNew())
counter.WriteFile(url, count);
%>
您是第<font color="red"> <%=count%> </font>位訪問者
<%
//調(diào)用counter對(duì)象的ReadFile方法來讀取文件count.txt中的計(jì)數(shù)
String url = request.getRealPath("count.txt");
String count = counter.ReadFile(url);
//調(diào)用counter對(duì)象的ReadFile方法來將計(jì)數(shù)器加一后寫入到文件count.txt中
if (session.isNew())
counter.WriteFile(url, count);
%>
您是第<font color="red"> <%=count%> </font>位訪問者
注意:在網(wǎng)站根目錄下建立一個(gè)count.txt文件,初始數(shù)字為0 。