写文件版
使用:
package com.c.elmer.util.string;
import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.PrintWriter;
public class LoginCount {
String count = "0";
String filepath = "";
public LoginCount(){
}
public LoginCount(String path){
filepath = path;
try{
File file = new File(path);
FileInputStream fi = new FileInputStream(file);
DataInputStream dis = new DataInputStream(fi);
count = dis.readLine();
dis.close();
fi.close();
if(count == null || count.equals("")){
count = "0";
}
}catch (Exception e) {
addLoginCount();
System.out.println("第一个用户登陆,新建用户登陆数统计文件:"+path);
}
}
public String getLoginCount(){
return count;
}
public String addLoginCount(){
long _lcount = (long) Double.parseDouble(count);
_lcount ++;
count = _lcount + "";
File file = new File(filepath);
try{
FileWriter fWriter = new FileWriter(file,false);
PrintWriter pWriter = new PrintWriter(fWriter);
pWriter.print(count);
pWriter.close();
fWriter.close();
}catch (Exception e) {
e.printStackTrace();
}
return count;
}
}
使用:
System.out.println("第 "+new LoginCount(request.getRealPath("/")+"/logincount.txt").addLoginCount() +" 个用户登陆.");