package util;import java.io.*;
import java.lang.*;
import javax.servlet.http.HttpSessionListener;
import javax.servlet.http.HttpSessionEvent;public class SessionCounter implements HttpSessionListener
{
private static int counterNow = 0; // 活动会话个数,即 当前在线人数。
private static int counterAll = 0; // 总访问人数。
/* Session创建事件 */
public void sessionCreated(HttpSessionEvent se)
{
counterNow++;
try
{
counterAll = 0; // 1 访问总人数
String strCountAll = "";
File fileCounter = new File("counter.txt");
if ( !fileCounter.exists() )
{
fileCounter.createNewFile();
}
else
{
BufferedReader bufReader = new BufferedReader(new FileReader("counter.txt"));
strCountAll = bufReader.readLine ();
counterAll = Integer.parseInt ( strCountAll );
bufReader.close ();
}
counterAll++;
PrintWriter pw = new PrintWriter ( new BufferedWriter(new FileWriter("counter.txt")) );
strCountAll = Integer.toString ( counterAll );
pw.println ( strCountAll );
pw.close ();
}
catch ( IOException e )
{
System.out.println ( e.toString() );
}
} /* Session失效事件 */
public void sessionDestroyed(HttpSessionEvent se)
{
if ( counterNow > 0 )
counterNow--;
} public static int getCounterNow()
{
return counterNow;
} public static int getCounterAll()
{
try
{
String strCountAll = "";
File fileCounter = new File("counter.txt");
if ( !fileCounter.exists() )
{
fileCounter.createNewFile();
}
else
{
BufferedReader bufReader = new BufferedReader(new FileReader("counter.txt"));
strCountAll = bufReader.readLine ();
counterAll = Integer.parseInt ( strCountAll );
bufReader.close ();
}
}
catch ( IOException e )
{
System.out.println ( e.toString() );
}
return counterAll;
}
}
import java.lang.*;
import javax.servlet.http.HttpSessionListener;
import javax.servlet.http.HttpSessionEvent;public class SessionCounter implements HttpSessionListener
{
private static int counterNow = 0; // 活动会话个数,即 当前在线人数。
private static int counterAll = 0; // 总访问人数。
/* Session创建事件 */
public void sessionCreated(HttpSessionEvent se)
{
counterNow++;
try
{
counterAll = 0; // 1 访问总人数
String strCountAll = "";
File fileCounter = new File("counter.txt");
if ( !fileCounter.exists() )
{
fileCounter.createNewFile();
}
else
{
BufferedReader bufReader = new BufferedReader(new FileReader("counter.txt"));
strCountAll = bufReader.readLine ();
counterAll = Integer.parseInt ( strCountAll );
bufReader.close ();
}
counterAll++;
PrintWriter pw = new PrintWriter ( new BufferedWriter(new FileWriter("counter.txt")) );
strCountAll = Integer.toString ( counterAll );
pw.println ( strCountAll );
pw.close ();
}
catch ( IOException e )
{
System.out.println ( e.toString() );
}
} /* Session失效事件 */
public void sessionDestroyed(HttpSessionEvent se)
{
if ( counterNow > 0 )
counterNow--;
} public static int getCounterNow()
{
return counterNow;
} public static int getCounterAll()
{
try
{
String strCountAll = "";
File fileCounter = new File("counter.txt");
if ( !fileCounter.exists() )
{
fileCounter.createNewFile();
}
else
{
BufferedReader bufReader = new BufferedReader(new FileReader("counter.txt"));
strCountAll = bufReader.readLine ();
counterAll = Integer.parseInt ( strCountAll );
bufReader.close ();
}
}
catch ( IOException e )
{
System.out.println ( e.toString() );
}
return counterAll;
}
}
public void sessionDestroyed(HttpSessionEvent se)
{
if ( counterNow > 0 )
counterNow--;
} public static int getCounterNow()
{
return counterNow;
} public static int getCounterAll()
{
try
{
String strCountAll = "";
File fileCounter = new File("counter.txt");
if ( !fileCounter.exists() )
{
fileCounter.createNewFile();
}
else
{
BufferedReader bufReader = new BufferedReader(new FileReader("counter.txt"));
strCountAll = bufReader.readLine ();
counterAll = Integer.parseInt ( strCountAll );
bufReader.close ();
}
}
catch ( IOException e )
{
System.out.println ( e.toString() );
}
return counterAll;
}
}
红色字段就不用了,这个只要统计一下counterNow--就可以了。在访问关闭的时候不需要统计等访问人数。总访问人数是不断增加的而且你在这个时候有可能又new了一个新的统计存储文件在原原文有可能丢失的情况下
strCountAll = Integer.toString ( counterAll );
pw.println ( strCountAll ); PrintWriter里面有直接打印整数的方法,为啥还要转换一下呢!strCountAll = bufReader.readLine ();
counterAll = Integer.parseInt ( strCountAll );
你上面代码好像并没有往文件里面写入啊,你这个readLine()读出来的会不会是countNow啊,countNow可能为0咯!
回归原题,归结来说应该是并发多线程和io读写文件间的矛盾产生的
假设有个线程正在改写你的记录文件的同时另外一个新线程(即新的session创建)去读取你的记录文件
此时会发生什么事情?
线程并发对io读写产生影响很正常
import java.lang.*;
import javax.servlet.http.HttpSessionListener;
import javax.servlet.http.HttpSessionEvent;public class SessionCounter implements HttpSessionListener
{
private static int counterNow = 0; // 活动会话个数,即 当前在线人数。
private static int counterAll = 0; // 总访问人数。
/* Session创建事件 */
public void sessionCreated(HttpSessionEvent se)
{
counterNow++;
try
{
counterAll = 0; // 1 访问总人数
String strCountAll = "";
File fileCounter = new File("counter.txt");
if ( !fileCounter.exists() )
{
fileCounter.createNewFile();
}
else
{
BufferedReader bufReader = new BufferedReader(new FileReader("counter.txt"));
strCountAll = bufReader.readLine ();
counterAll = Integer.parseInt ( strCountAll );
bufReader.close ();
}
counterAll++;
PrintWriter pw = new PrintWriter ( new BufferedWriter(new FileWriter("counter.txt")) );
strCountAll = Integer.toString ( counterAll );
pw.println ( strCountAll );
pw.close ();
}
catch ( IOException e )
{
System.out.println ( e.toString() );
}
} /* Session失效事件 */
public void sessionDestroyed(HttpSessionEvent se)
{
if ( counterNow > 0 )
counterNow--;
} public static int getCounterNow()
{
return counterNow;
} public static int getCounterAll()
{
/*try
{
String strCountAll = "";
File fileCounter = new File("counter.txt");
if ( !fileCounter.exists() )
{
fileCounter.createNewFile();
}
else
{
BufferedReader bufReader = new BufferedReader(new FileReader("counter.txt"));
strCountAll = bufReader.readLine ();
counterAll = Integer.parseInt ( strCountAll );
bufReader.close ();
}
}
catch ( IOException e )
{
System.out.println ( e.toString() );
}
*/
return counterAll;
}
}