package com.mindprod.csv;
import com.mindprod.hunkio.HunkIO;
import java.io.BufferedReader;
import java.io.EOFException;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import static java.lang.System.*;
/**
* Test CSVReader to read a csv file.
*
* @author Roedy Green, Canadian Mind Products
* @version 1.1 2011-10-29 allow test with/without comments, read field at a time or line at a time
* @since 2010-12-31
*/
public final class TestCSVReader
{
/**
* true if should test processing hiding comments
*/
private static final boolean HIDE_COMMENTS = true;
/**
* true if should test processing a line at a time
*/
private static final boolean READ_A_LINE_AT_A_TIME = true;
/**
* Test driver, Command line ignored.
*
* @param args not used
*
* @noinspection InfiniteLoopStatement
*/
public static void main( String[] args )
{
out.println( "Testing with HIDE_COMMENTS = " + HIDE_COMMENTS + " and READ_A_LINE_AT_A_TIME = " +
READ_A_LINE_AT_A_TIME );
try
{
String all = HunkIO.readEntireFile( new File( "test.csv" ) );
out.println( "test file looks like this:" );
out.println( all );
CSVReader csv = new CSVReader( new BufferedReader( new FileReader( "test.csv" ) ),
',',
'\"',
"#",
HIDE_COMMENTS ,
true ,
true ,
true
);
try
{
while ( true )
{
if ( READ_A_LINE_AT_A_TIME )
{
String[] fields = csv.getAllFieldsInLine();
out.print( fields.length + " fields " );
for ( int i = 0; i < fields.length; i++ )
{
out.print( "[" + i + "]={" + fields[ i ] + "} " );
}
out.println( "wasComment: " + csv.wasComment() );
}
else
{
final String s = csv.get();
if ( s == null )
{
out.println( "(end of line)" );
}
else if ( csv.wasComment() )
{
out.println( " <" + s + ">" );
}
else
{
out.println( " [" + s + "]" );
}
}
}
}
catch ( EOFException e )
{
csv.close();
}
}
catch ( IOException e )
{
err.println();
e.printStackTrace( err );
err.println();
}
}
}