// Needs to be abstracted further, currently hardcoded to output TrimmedFile to Console
// limitation: how big should the buffer be? (right now hard coding is the most efficient?)
// does the semantic of just getting one line, then displaying to Console out, really use less memory?
// TODO: fix the "do one thing" , Object should have getFile , trimFile , displayTrimFile
// requires CommandLineParams.java
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
public class TrimFile
{
public static void main( String[] args )
{
CommandLineParams cmdlineparams = new CommandLineParams( 1 , "version 0.1: java -jar TrimFile.jar filename" );
cmdlineparams.check( args , System.out );
File f = new File( args[0] );
TrimFile tf = new TrimFile( f );
}
TrimFile( File f ) //constructor( File )
{
if( f.isFile() )
{
BufferedReader br = null;
try
{
FileReader fr = new FileReader( f );
br = new BufferedReader( fr );
String trimmedline = new String();
String currentline = new String();
currentline = br.readLine();
while( currentline != null ) // pointer != null
{
trimmedline = currentline.trim(); // pointer refers to new object
System.out.println( trimmedline );
currentline = br.readLine(); // gc optimally discards unused objects?
}
}catch( IOException io )
{ System.out.println( "Error reading file: " + io );
System.exit( 1 );
}catch( Exception e )
{ System.out.println( "Error: " + e );
e.printStackTrace();
System.exit( 1 );
}
finally
{
try
{
if( br != null )
{ br.close();
}
}catch( Exception e )
{ System.out.println( "Error closing: " + e );
e.printStackTrace();
System.exit( 1 );
}
}
} //end if isFile
} //end constructor( file )
} //end class