用java作计数器也很简单呀,我做了一个,很简陋,只是计数然后保存到文件。
解决方案 »
- list多线程修改同步问题
- 如何获取printf的输出的结果?
- JAVA输入输出问题
- java垃圾回收策略,有复制式回收还有一种是什么式的回收啊??
- 求助:Hibernate读取Oracle中的XML字段的方法
- 我在用jsp+javabean做一个ftp搜索引擎,有一个有关异常的问题
- 为什么找不到javax.servlet.http.*;
- 自己写咳一小段测对象lock的片断,可是怎么不能正常运行,高手指点。。
- 怎么会找不到applet呢?
- 不能显示 http://localhost:8080 为什么?????
- 我用JAVA编了个数据库查询程序,查询非常慢?如何加快一点。
- applet中使用了jbuilder4的class,怎样才能在ie中显示出来
public class Counter implements Serializable{
int count =0;
public Counter(){
}
public int getCount(){
count++;
return this.count;
}
public void setCount(int count){
this.count=count;
}
}
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.io.*" %> <HTML>
<HEAD>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<TITLE>计数器演示程序</TITLE>
</HEAD>
<BODY>
<!--创建并调用bean(counter)-->
<jsp:useBean id="counter" class="count.counter" scope="request">
</jsp:useBean>
<%
//调用counter对象的ReadFile方法来读取文件lyfcount.txt中的计数
String cont=counter.ReadFile("/myjsp/tmp/count.txt");
//调用counter对象的ReadFile方法来将计数器加一后写入到文件lyfcount.txt中
counter.WriteFile("/myjsp/tmp/count.txt",cont);%>
您是第<font color="red"><%=cont%></font>位访问者
<%
/*PrintWriter pw = new PrintWriter(new FileOutputStream("/myjsp/tmp/tmp.txt"));
String Writestr="中国";
Writestr=new String(Writestr.getBytes("iso-8859-1"),"GBK");
pw.println(Writestr);
pw.close();*/
%>
</BODY>
</HTML>bean:
package count;
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 FileNotFoundException
{
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)
//如果文件为空
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 = 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());
}
}}