国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JSP > 正文

一個使用TXT文本的JSP網(wǎng)站訪問計數(shù)器(不用任何數(shù)據(jù)庫)

2024-09-05 00:19:15
字體:
供稿:網(wǎng)友
//counter.java 讀寫文件的一個bean
===========================
 //網(wǎng)站讀寫txt格式計數(shù)器
 package net.com.util;
 
 import java.io.*;

 public class counter extends object {
 private string currentrecord = null;//保存文本的變量
 private bufferedreader file; //bufferedreader對象,用于讀取文件數(shù)據(jù)
 private string path;//文件完整路徑名
 public counter() {
 }
// readfile方法用來讀取文件filepath中的數(shù)據(jù),并返回這個數(shù)據(jù)
 public string readfile(string filepath) throws filenotfoundexception
 {
 path = filepath;
// 創(chuàng)建新的bufferedreader對象
 file = new bufferedreader(new filereader(path));
 string returnstr =null;
 try
 {
// 讀取一行數(shù)據(jù)并保存到currentrecord變量中
 currentrecord = file.readline();
 }
 catch (ioexception e)
 {//錯誤處理
 system.out.println("讀取數(shù)據(jù)錯誤.");
 }
 if (currentrecord == null)
// 如果文件為空
 returnstr = "沒有任何記錄";
 else
 {//文件不為空
 returnstr =currentrecord;
 }
// 返回讀取文件的數(shù)據(jù)
 return returnstr;
 }
// readfile方法用來將數(shù)據(jù)counter+1后寫入到文本文件filepath中
// 以實現(xiàn)計數(shù)增長的功能
 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對象,用于寫入數(shù)據(jù)到文件中
 printwriter pw = new printwriter(new fileoutputstream(filepath));
// 用文本格式打印整數(shù)writestr
 pw.println(writestr);
// 清除printwriter對象
 pw.close();
 } catch(ioexception e) {
// 錯誤處理
 system.out.println("寫入文件錯誤"+e.getmessage());
 }
 }

 }
 
  ====================================
// counter.jsp文件
<%@ page contenttype="text/html;charset=gbk"%>
<!--創(chuàng)建并調(diào)用bean(counter)-->
<jsp:usebean id="counter" scope="page" class="net.com.util.counter"/>
<%
//調(diào)用counter對象的readfile方法來讀取文件lyfcount.txt中的計數(shù)
string url=request.getrealpath("count.txt");
string cont=counter.readfile(url);
//調(diào)用counter對象的readfile方法來將計數(shù)器加一后寫入到文件lyfcount.txt中
counter.writefile(url,cont);%>
您是第<font color="red"> <%=cont%> </font>位訪問者

======================================
//注意:在counter的同一目錄下建立一個count.txt文件。。初始數(shù)字為0
======================================


發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 昂仁县| 墨江| 革吉县| 喀喇| 华宁县| 铅山县| 和田市| 内黄县| 芒康县| 镇宁| 兴隆县| 尉犁县| 葫芦岛市| 津南区| 子洲县| 吉首市| 汉源县| 奉贤区| 台南县| 上林县| 图们市| 射洪县| 宝清县| 崇信县| 乌兰察布市| 祥云县| 新邵县| 桐柏县| 望奎县| 凤翔县| 安泽县| 丰宁| 唐河县| 渭南市| 柘城县| 定襄县| 阿荣旗| 西青区| 乐都县| 高平市| 东平县|