FileDocCategorySizeDatePackage
LogStrTok.javaAPI DocExample1204Sat Nov 09 10:12:18 GMT 2002None

LogStrTok

public class LogStrTok extends Object implements LogExample
Parse an Apache log file with StringTokenizer

Fields Summary
Constructors Summary
Methods Summary
public static voidmain(java.lang.String[] argv)


		StringTokenizer matcher = new StringTokenizer(logEntryLine);

		System.out.println("tokens = " + matcher.countTokens());
		// StringTokenizer CAN NOT count if you are changing the delimiter!
		// if (matcher.countTokens() != NUM_FIELDS) {
		// 	System.err.println("Bad log entry (or bug in StringTokenizer?):");
		// 	System.err.println(logEntryLine);
		// }

		System.out.println("Hostname: " + matcher.nextToken());
		// StringTokenizer makes you ask for tokens in order to skip them:
		matcher.nextToken(); // eat the "-"
		matcher.nextToken(); // again
		System.out.println("Date/Time: " + matcher.nextToken("]"));
		//matcher.nextToken(" "); // again
		System.out.println("Request: " + matcher.nextToken("\""));
		matcher.nextToken(" "); // again
		System.out.println("Response: " + matcher.nextToken());
		System.out.println("ByteCount: " + matcher.nextToken());
		System.out.println("Referer: " + matcher.nextToken("\""));
		matcher.nextToken(" "); // again
		System.out.println("User-Agent: " + matcher.nextToken("\""));