//2012-04-20 johnpfeiffer
// StringTokenizer is a legacy class that is retained for compatibility reasons although its use is discouraged in new code. It is recommended that anyone seeking this functionality use the split method of String or the java.util.regex package instead.
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.StringTokenizer;
/*
private static final String IDENTIFIER = "servicename.hostname=";
FileSystem fs = new FileSystem();
String fileLocation = fs.convertFileSystemPath( "/var/lib/tomcat6/webapps/appname/WEB-INF/app.properties" );
ArrayList <String> contents = fs.newlineDelimitedListFromFile( fileLocation );
Iterator <String> it = contents.iterator();
while( it.hasNext() )
{
String line = it.next();
line = line.trim();
if( line.contains( IDENTIFIER ) && !line.startsWith( "#" ) ) //skips comments
{
int start = line.indexOf( SHAREDSECRETIDENTIFIER );
int finish = SHAREDSECRETIDENTIFIER.length();
System.out.println( "DEBUG: " + line );
System.out.println( start + " to " + finish );
String sub = line.substring( start + finish );
System.out.println( sub );
}
}
*/
public class CSVUtility
{
public static void main(String[] args)
{
CSVUtility c = new CSVUtility();
String csv = c.stringFromFile( "C:\\var\\lib\\ssl\\test.csv" );
StringTokenizer st1 = new StringTokenizer( csv , "," );
System.out.println( "Without delimiters:" );
while( st1.hasMoreElements() )
{
String temp = st1.nextToken();
System.out.println( temp );
}
System.out.println( "With delimiters:" );
StringTokenizer st2 = new StringTokenizer( csv , "," , true ); //keep the delimiter tokens
while( st2.hasMoreElements() )
{
String temp = st2.nextToken();
System.out.println( temp );
}
}
protected String stringFromFile( String pathfile )
{
StringBuilder str = new StringBuilder();
try{
FileReader inFile = new FileReader( pathfile );
BufferedReader myBufferedReader = new BufferedReader( inFile );
String line = "";
while ( ( line = myBufferedReader.readLine() ) != null)
{ str.append( line + System.getProperty( "line.separator") );
}
myBufferedReader.close();
} catch (IOException e)
{ System.err.println( "Unable to read file: " + pathfile );
e.printStackTrace();
}
return str.toString();
}
ArrayList <String> stringToArrayList( String source , String delimiters )
{
ArrayList <String> alist = new ArrayList();
String[] elements = source.split( delimiters );
for( String value : elements )
{
StringTokenizer st = new StringTokenizer( value );
String temp = st.nextToken();
alist.add( temp );
}
return alist;
}
List <String> mySplit( String source , String delimiters )
{
// String str = "This is a sentence of words? Statement. Declaration! Ellipsis...";
// String delims = "[ .,?!]+";
String[] tokens = source.split( delimiters );
List <String> list = Arrays.asList( tokens );
return list;
}
}