Java Reference
In-Depth Information
The weblog-analyzer project contains an application that performs an analysis of data from
such a web server. The server writes a line to a log file each time an access is made. A sample
log file called weblog.txt is provided in the project folder. Each line records the date and time of
the access in the following format:
year month day hour minute
For instance, the line below records an access at 03:45am on 7 June 2011:
2011 06 07 03 45
The project consists of five classes: LogAnalyzer , LogfileReader , LogEntry ,
LoglineTokenizer , and LogfileCreator . We shall spend most of our time looking at the
LogAnalyzer class, as it contains examples of both creating and using an array (Code 4.11).
Later exercises will encourage you to examine and modify LogEntry , because it also uses an
array. The LogReader and LogLineTokenizer classes use features of the Java language that
we have not yet covered, so we shall not explore those in detail. The LogfileCreator class
allows you to create your own log files containing random data.
Code 4.11
The log-file analyzer
/**
* Read web server data and analyze
* hourly access patterns.
*
* @author David J. Barnes and Michael K├Âlling.
* @version 2011.07.31
*/
public class LogAnalyzer
{
// Array to store the hourly access counts.
private int [] hourCounts;
// Use a LogfileReader to access the data.
private LogfileReader reader;
/**
* Create an object to analyze hourly web accesses.
*/
public LogAnalyzer()
{
// Create the array object to hold the hourly
// access counts.
hourCounts = new int [24];
// Create the reader to obtain the data.
reader = new LogfileReader();
}
/**
* Analyze the hourly access data from the log file.
*/
 
Search WWH ::




Custom Search