com.townleyenterprises.io
Class TextFileProcessor

java.lang.Object
  extended bycom.townleyenterprises.io.TextFileProcessor

public class TextFileProcessor
extends java.lang.Object

This class provides a pluggable mechanism for processing line-based text files with a given character encoding. The class breaks up the input file into lines of text. The actual processing of the line is provided by providing an instance of LineProcessor apropriate for the task.

An example of echoing the file to the console:

 try
 {
 	// create a text file processor using the default encoding
 	TextFileProcessor fp = new TextFileProcessor("myfile.dat");
 	fp.processFile(new AbstractLineProcessor() {
 		public void processLine(String line) throws Exception
 		{
 			super.processLine(line);
 			System.out.println(line);
 		}
	});
 }
 catch(FileNotFoundException e)
 {
 	e.printStackTrace();
 }
 

Since:
2.1
Version:
$Id: TextFileProcessor.java,v 1.3 2004/01/30 11:50:42 atownley Exp $
Author:
Andrew S. Townley

Constructor Summary
TextFileProcessor(java.lang.String name)
          The constructor initializes the input file using the default encoding.
TextFileProcessor(java.lang.String name, java.lang.String encoding)
          The constructor initializes the input file using the specified character encoding.
 
Method Summary
 void processFile(LineProcessor lp)
          This method is used to process the file.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

TextFileProcessor

public TextFileProcessor(java.lang.String name)
The constructor initializes the input file using the default encoding.

Parameters:
name - the name of the file to process

TextFileProcessor

public TextFileProcessor(java.lang.String name,
                         java.lang.String encoding)
                  throws java.nio.charset.UnsupportedCharsetException
The constructor initializes the input file using the specified character encoding.

Parameters:
name - the name of the file to process
encoding - the encoding of the input file
Throws:
java.nio.charset.UnsupportedCharsetException - if the encoding is not supported by the JVM
Method Detail

processFile

public void processFile(LineProcessor lp)
                 throws java.io.IOException,
                        java.lang.Exception
This method is used to process the file. For each line, the @{link LineProcessor.processLine} method is called.

Parameters:
lp - the LineProcessor instance
Throws:
java.io.IOException - if there was an error reading the file
java.lang.Exception - if there was an error thrown by the LineProcessor


Copyright © 2002-2004, Andrew S. Townley and Townley Enterprises. All Rights Reserved.
This project is hosted on http://te-code.sourceforge.net.