正在學習,很多東西都不懂?哪位做過?
熱心網友
//test。jsp文件計數器演示程序您是第位訪問者 //counter。java 讀寫文件的一個beanimport 。*;public class counter extends Object {private String currentRecord = null;//保存文本的變量private BufferedReader file; //BufferedReader對象,用于讀取文件數據private String path;//文件完整路徑名public counter() {}//ReadFile方法用來讀取文件filePath中的數據,并返回這個數據public String ReadFile(String filePath) throws FileNotFoundException{path = filePath;//創建新的BufferedReader對象file = new BufferedReader(new FileReader(path));String returnStr =null;try{//讀取一行數據并保存到currentRecord變量中currentRecord = adLine();}catch (IOException e){//錯誤處理 intln("讀取數據錯誤。");}if (currentRecord == null)//如果文件為空returnStr = "沒有任何記錄";else{//文件不為空returnStr =currentRecord;}//返回讀取文件的數據return returnStr;}//ReadFile方法用來將數據counter+1后寫入到文本文件filePath中//以實現計數增長的功能public void WriteFile(String filePath,String counter) throws FileNotFoundException{path = filePath;//將counter轉換為int類型并加一int Writestr = rseInt(counter)+1;try {//創建PrintWriter對象,用于寫入數據到文件中PrintWriter pw = new PrintWriter(new FileOutputStream(filePath));//用文本格式打印整數Writestr intln(Writestr);//清除PrintWriter對象 ose();} catch(IOException e) {//錯誤處理 intln("寫入文件錯誤"+ tMessage());}}} 到這里,程序寫完了,將counter。java編譯為 ass,同樣放在對應的class目錄下,在根目錄下建立一個lyfcount。txt文件,文件內容就一個數字0,直接在瀏覽器中敲入地址就可以看到計數器了,刷新瀏覽器會看到不斷變幻的數字。
熱心網友
JSP 計數器制作手冊2004-5-15 2:17:39 (文章類別:JSP) 計數器是一般網站必備的東東,別小看它了,每當站長看著小小計數器上的數字飛速增長的時候,感覺實在是好極了。以前我們用cgi、asp來寫計數器,這方面的文章很多了,在這里,我們將會采用目前比較流行的jsp技術演示如何做一個計數器。 其中我們用到了兩個文件,test。jsp文件用于在瀏覽器中運行,counter。java是后臺的一個小java bean程序,用來讀計數器的值和寫入計數器的值。而對于計數器的保存,我們采用了一個文本文件lyfcount。txt。下面是詳細的程序代碼(test。jsp放到web目錄下,counter。java放到class目錄)://test。jsp文件您是第位訪問者//counter。java 讀寫文件的一個beanimport 。*;public class counter extends Object {private String currentRecord = null;//保存文本的變量private BufferedReader file; //BufferedReader對象,用于讀取文件數據private String path;//文件完整路徑名public counter() {}//ReadFile方法用來讀取文件filePath中的數據,并返回這個數據public String ReadFile(String filePath) throws FileNotFoundException{path = filePath;//創建新的BufferedReader對象file = new BufferedReader(new FileReader(path));String returnStr =null;try{//讀取一行數據并保存到currentRecord變量中currentRecord = adLine();}catch (IOException e){//錯誤處理 intln("讀取數據錯誤。");}if (currentRecord == null)//如果文件為空returnStr = "沒有任何記錄";else{//文件不為空returnStr =currentRecord;}//返回讀取文件的數據return returnStr;}//ReadFile方法用來將數據counter+1后寫入到文本文件filePath中//以實現計數增長的功能public void WriteFile(String filePath,String counter) throws FileNotFoundException{path = filePath;//將counter轉換為int類型并加一int Writestr = rseInt(counter)+1;try {//創建PrintWriter對象,用于寫入數據到文件中PrintWriter pw = new PrintWriter(new FileOutputStream(filePath));//用文本格式打印整數Writestr intln(Writestr);//清除PrintWriter對象 ose();} catch(IOException e) {//錯誤處理 intln("寫入文件錯誤"+ tMessage());}}} 到這里,程序寫完了,將counter。java編譯為 ass,同樣放在對應的class目錄下,在根目錄下建立一個lyfcount。txt文件,文件內容就一個數字0,直接在瀏覽器中敲入地址就可以看到計數器了,刷新瀏覽器會看到不斷變幻的數字。 (如果運行時候提示找不到文件,請將上面test。jsp中的readfile那一句注釋后運行一次則lyfcount。txt文件自動建立,然后就可以正常運行。) 上面的程序完整源代碼在我的網站 。