今日昨日及总访问量的统计(无须数据库)

今日昨日及总访问量的统计(无须数据库)的方法

首先在WebRoot下创建Count的文件夹,在其中建count.txt文件,输入一个0,访问一次就将0+1的值写入文件;

编写一个读取和写入的java类文件

import java.io.*;
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 IOException
{
path = filePath;
//创建新的BufferedReader对象
file = new BufferedReader(new FileReader(path));
String returnStr =null;
try
{
//读取一行数据并保存到currentRecord变量中
currentRecord = file.readLine();
}
catch (IOException e)
{
//错误处理
System.out.println("读取数据错误.");
}
if (currentRecord == null)
{
//如果文件为空
NewFile newf=new NewFile();
newf.createFile(path);
returnStr = "没有任何记录";
}
else
{
//文件不为空
returnStr =currentRecord;
}
file.close();
//返回读取文件的数据
return returnStr;
}
//ReadFile方法用来将数据counter+1后写入到文本文件filePath中
//以实现计数增长的功能
public void WriteFile(String filePath,String counter) throws FileNotFoundException
{
path = filePath;
//将counter转换为int类型并加一
int Writestr = Integer.parseInt(counter)+1;
try {
//创建PrintWriter对象,用于写入数据到文件中
PrintWriter pw = new PrintWriter(new FileOutputStream(filePath));
//用文本格式打印整数Writestr
pw.println(Writestr);
//清除PrintWriter对象
pw.close();
} catch(IOException e) {
//错误处理
System.out.println("写入文件错误"+e.getMessage());
}
}

}

如果只需要总访问量的话,就不用往下看了,直接在页面上读取文本文件count.txt的内容就可以了.但如果还需要日访问量就继续往下看吧.

再编写一个自动创建txt文件的类,如:当在2008-07-07访问网站时,就自动创建一个2008-7-7.txt文件,并且给它写入0的字符,

import java.io.*;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

public class NewFile {

public NewFile(){
}

public void createFile(String path){

// 获取当前时间
java.util.Calendar todays = new java.util.GregorianCalendar();
String todaytimes = todays.getTime().toLocaleString();
String[] date_arr=todaytimes.split(" ");
String dc_date=date_arr[0];

try
{//如果昨天没有记录,就自动创建一个昨天信息
String[] day_ar=dc_date.split("-");
int aftdate=Integer.parseInt(day_ar[2])-1;
int yuedate=Integer.parseInt(day_ar[1]);
if(aftdate<=0){
aftdate=30;
yuedate=yuedate-1;
}
File fileafter=new File(path+"/Count/"+day_ar[0]+"-"+yuedate+"-"+aftdate+".txt");
if(!fileafter.exists()){
fileafter.createNewFile();
String fstr="0";
RandomAccessFile fm = null;
try {
fm = new RandomAccessFile(fileafter,"rw");
fm.writeBytes(fstr);
} catch (IOException e3) {
e3.printStackTrace();
} finally{
if(fm!=null){
try {
fm.close();
} catch (IOException e4) {
e4.printStackTrace();
}
}
}
}

File filename = new File(path+"/Count/"+dc_date+".txt");
if(!filename.exists()){
filename.createNewFile();
String filein="0";
RandomAccessFile mm = null;
try {
mm = new RandomAccessFile(filename,"rw");
mm.writeBytes(filein);
} catch (IOException e1) {
e1.printStackTrace();
} finally{
if(mm!=null){
try {
mm.close();
} catch (IOException e2) {
e2.printStackTrace();
}
}
}
}
}
catch(IOException e1){
e1.printStackTrace();
}
finally{
deleteFile(path);
}

}

public void deleteFile(String path){

// 获取当前时间
java.util.Calendar todays = new java.util.GregorianCalendar();
String todaytimes = todays.getTime().toLocaleString();
String[] date_arr=todaytimes.split(" ");
String dc_date=date_arr[0];

String[] day_arr=dc_date.split("-");
//删除上月所有的日期文本文件
for(int i=1;i<=31;i++)
{
int yuedate=Integer.parseInt(day_arr[1])-1;
String dc_day=day_arr[0]+"-"+yuedate+"-"+i;
File files=new File(path+"/Count/"+dc_day+".txt");
if(files.exists()){
files.delete();
}
}
}
}
最后在页面的读取

<%
//调用counter对象的ReadFile方法来读取文件lyfcount.txt中的计数
String url=request.getRealPath("/Count/count.txt");
String cont=counter.ReadFile(url);
//调用counter对象的ReadFile方法来将计数器加一后写入到文件lyfcount.txt中
counter.WriteFile(url,cont);
%>
全站总访问量:<%=cont%> <br />
<%
path = this.getServletContext().getRealPath("").replace("\\", "/");
newfile.createFile(path);
String d_url=request.getRealPath("/Count/"+dc_date+".txt");
String d_cont=counter.ReadFile(d_url);
//调用counter对象的ReadFile方法来将计数器加一后写入到文件lyfcount.txt中
counter.WriteFile(d_url,d_cont);
%>
今日总访问量:<%=d_cont%> <br />
<%
String[] day_arr=dc_date.split("-");
int aftdate=Integer.parseInt(day_arr[2])-1;
int yuedate=Integer.parseInt(day_arr[1]);
if(aftdate<=0){
aftdate=30;
yuedate=yuedate-1;
}
String dc_day=day_arr[0]+"-"+yuedate+"-"+aftdate;
String a_url=request.getRealPath("/Count/"+dc_day+".txt");
String a_cont=counter.ReadFile(a_url);
//调用counter对象的ReadFile方法来将计数器加一后写入到文件lyfcount.txt中
%>
昨日总访问量:<%=a_cont%> <br />
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值