class ReadWithScanner {
public void main(String... aArgs) throws FileNotFoundException {
ReadWithScanner parser = new ReadWithScanner("C:\\marinos/PerfSim-CF-Config.txt");
parser.processLineByLine();
log("Done.");
}
public ReadWithScanner(String aFileName){
fFile = new File(aFileName);
}
/** Template method that calls {@link #processLine(String)}. */
public final void processLineByLine() throws FileNotFoundException {
//Note that FileReader is used, not File, since File is not Closeable
Scanner scanner = new Scanner(new FileReader(fFile));
try {
//first use a Scanner to get each line
while ( scanner.hasNextLine() ){
processLine( scanner.nextLine() );
}
}
finally {
//ensure the underlying stream is always closed
//this only has any effect if the item passed to the Scanner
//constructor implements Closeable (which it does in this case).
scanner.close();
}
}
/**
Overridable method for processing lines in different ways.
<P>This simple default implementation expects simple name-value pairs, separated by an
'=' sign. Examples of valid input :
<tt>height = 167cm</tt>
<tt>mass = 65kg</tt>
<tt>disposition = "grumpy"</tt>
<tt>this is the name = this is the value</tt>
*/
protected void processLine(String aLine){
//use a second Scanner to parse the content of each line
Scanner scanner = new Scanner(aLine);
scanner.useDelimiter("=");
if ( scanner.hasNext() ){
String name = scanner.next();
log("Path = " + quote(name.trim()));
}
else {
log("Empty or invalid line. Unable to process.");
}
//no need to call scanner.close(), since the source is a String
}
// PRIVATE
private final File fFile;
private void log(Object aObject){
System.out.println(String.valueOf(aObject));
}
private String quote(String aText){
String QUOTE = "'";
return QUOTE + aText + QUOTE;
}
}