Table of Contents

How to use the FileLoader to read complex text files

Upgraded fc-11.12.2017 Removed fileLoader report, replaced by exceptions

The FileLoader was built to facilitate the reading of parameters, measurements or inventory text files.

These files may contain keyword parameters and rows with values in columns separated by tabs (e.g. 2 columns below):

# Simeo-Principes command file

parameterFileName = PhoenixRomana.txt

plantAge = 200
plantSeed = 1

archimed1ConfigFileName = archimed1.config

# Pattern coordinates in m
pattern = ((0,10);(10,0))
patternBounds = ((-5,-5);(15,15))

# simName	simulationParameters
sim1	PinnaeDistance_1(2D;1;(0:2.3);(50:10);(100:2))
sim2	PinnaeDistance_1(2D;1;(0:6);(50:15);(100:4))

To read such a file, it is possible to write a subclass of FileLoader where the public fields are expected to be automatically loaded from the file. FileLoader proposes methods to interpret the lines and turn them into objects, by using Java introspection, based on the order and types of the fields.

The lines of the file starting with a '#' comment mark and blank lines are ignored.

Possible types for the public fields in the loader:

Possible types for the public lists:

Here is an example of loader for the file upper:

package jeeb.simeo.module.edito.scripts;
 
import java.util.List;
 
import jeeb.lib.util.Record;
import jeeb.lib.util.fileloader.FileLoader;
import jeeb.lib.util.fileloader.FileLoaderCheck;
 
/**
 * Testing the new FileLoader class.
 * 
 * <pre>
 * // To test this FileLoader
 * java -cp bin jeeb.simeo.module.edito.scripts.ScriptRaphael2013FileLoader
 * </pre>
 * 
 * @author F. de Coligny - December 2013
 */
public class ScriptRaphael2013FileLoader extends FileLoader {
 
	public String parameterFileName;
	public int plantAge;
	public int plantSeed;
	public String archimed1ConfigFileName;
	public String pattern;
	public String patternBounds;
 
	public List<SimRecord> simRecords;
 
	/**
	 * Constructor.
	 */
	public ScriptRaphael2013FileLoader () throws Exception {
		super ();
	}
 
	protected void checks() throws Exception {
 
		checkAssertion(plantAge > 0, "plantAge must be > 0");
		checkAssertion(plantSeed >= 0, "plantSeed must be >= 0");
 
		checkAssertion(!simRecords.isEmpty (), "The simRecords list should not be empty");
 
	}
 
	// A line in the command file
	static public class SimRecord extends Record {
 
		// e.g. sim1
		public String simName;
		// e.g. PinnaeDistance_1(2D;1;(0:2.3);(50:10);(100:2)) !
		// FrondNervureLength_1(2D;1;(1:100);(175:400))
		public String simulationParameters;
 
		public SimRecord(String line) throws Exception {
			super(line);
		}
	}
 
}

This loader may be used like below:

	(...)
 
	// Interpret command file
	ScriptRaphael2013FileLoader loader = new ScriptRaphael2013FileLoader();
	loader.load(commandFileName);
 
	// commandFileName = reader.getCommandFileName();
	parameterFileName = loader.parameterFileName;
	plantAge = loader.plantAge;
	plantSeed = loader.plantSeed;
	pattern = loader.pattern;
	patternBounds = loader.patternBounds;
	archimed1ConfigFileName = loader.archimed1ConfigFileName;
 
	List<SimRecord> simRecords = loader.simRecords;
 
	int numberOfSimulations = simRecords.size();
 
	(...)

The constructor and load () method may throw an exception if a problem arises during the file interpretation.

Possible errors are:

It is possible to add checks by overriding the checks () method. It is possible to check simple assertions like in the example above. It is also possible to make more complicated checks like checking if the number of rows in the list is greater than 0. In case of trouble in checks (), throw an exception to stop the process.

After loading the file, all fields can be retrieved directly from the loader, they are all public fields. It is also possible to add and call a method in the fileLoader to change all these parameters into another data structure, e.g. a list of trees in a forest stand or a predefined parameter object.

Finally, it is possible to call the FileLoader toString () method after the process to check what happened. Here are two examples after a successful and after a failed loading :

FileLoader       : ScriptRaphael2013FileLoader
loaded file      : /home/coligny/workspace/amapstudio/data/simeo/edito/principesRaphael/wd1/principesCmd1.txt
loading succeeded: true

-- Loaded data --
parameterFileName = PhoenixRomana.txt
plantAge = 200
plantSeed = 1
archimed1ConfigFileName = archimed1.config
pattern = ((0,10);(10,0))
patternBounds = ((-5,-5);(15,15))

simRecords: 
sim1	PinnaeDistance_1(2D;1;(0:2.3);(50:10);(100:2))
sim2	PinnaeDistance_1(2D;1;(0:6);(50:15);(100:4))
-- end-of-loaded data --
FileLoader       : ScriptRaphael2013FileLoader
loaded file      : /home/coligny/workspace/amapstudio/data/simeo/edito/principesRaphael/wd1/principesCmd1.txt
loader report    : The simRecords list should not be empty
loading succeeded: false

Additional features

Change the separator

In a record format (subclass of Record) like SimRecord upper, the default separator between the fields of a line is a tabulation. It is possible to change this by overriding a method in SimRecord, e.g.

// Change the default \t separator by ;
public String getSeparator() {
	return ";";
}

Manage comment marks

The file reader ignores all lines starting by a 'commentMark', by default '#'

It is possible to add comment marks, lines starting with all comment marks will be ignored, e.g.

// Ignore the lines starting with "//"
fileReader.addAdditionalCommentMark("//");