js| Access | counter | data | A bean for database//counter.java read and write files
===========================
//website read-write txt format counter
package net.com.util;
import java.io.*;
public class Counter extends Object {
private String CurrentRecord = null;//Save text variable
private BufferedReader file; BufferedReader object that is used to read the file data
private String path;//file full pathname
public Counter () {
}
// The ReadFile method is used to read the data in the file FilePath and returns this data
public string ReadFile (String filePath) throws FileNotFoundException
{
path = FilePath;
Creates a new BufferedReader object,
file = newer bufferedreader (path);
string Returnstr =null;
try
{
//reads a row of data and saves it to the CurrentRecord variable
currentrecord = File.readline ();
&NBSP}
catch (IOException e)
{//error handling
system.out.println ("read data error.");
}
if (CurrentRecord = = null)
//If the file is empty
returnstr = "no records";
else
{//file is not empty
returnstr =currentrecord;
&NBSP}
//Returns the data read from the file
return returnstr
&NBSP} The
//ReadFile method is used to write data counter+1 to a text file filepath
//to implementCount growth feature
public void WriteFile (String filepath,string counter) throws FileNotFoundException
{
path = FilePath;
//Convert counter to int type and add one
int writestr = integer.parseint (counter) +1;
try {
///Create PrintWriter object to write data to file
printwriter pw = new PrintWriter (new FileOutputStream (FilePath) );
//print integer writestr in text Format
pw.println (WRITESTR);
Clear the PrintWriter object
pw.close ();
&NBSP} catch (IOException e) {
//error handling
system.out.println ("Write File Error" +e.getmessage ());
&NBSP}
}
}
====================================
counter.jsp file
<%@ page contenttype= "TEXT/HTML;CHARSET=GBK"%>
<!--Create and invoke bean (counter)-->
<jsp:usebean id= "Counter" scope= "page" class= "Net.com.util.Counter"/>
<%
Call the Counter object's ReadFile method to read the count in the file Lyfcount.txt
String Url=request.getrealpath ("Count.txt");
String Cont=counter. ReadFile (URL);
Calls the Counter object's ReadFile method to add a counter to the file Lyfcount.txt
Counter. WriteFile (Url,cont);%>
You are the first <font color= "Red" > <%=cont%> </font> site visitor
======================================
Note: Create a count.txt file in the same directory as counter. The initial number is 0.
======================================