I have a legacy method which I would like to clean up:
public void readFile(String filename) {
int i;
int j;
String ins = new String();
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
try (FileReader input = new FileReader(filename);
BufferedReader bufInput = new BufferedReader(input);
Scanner scHeader = new Scanner(bufInput.readLine())) {
scHeader.next(); // String creationTime
scHeader.next(); // String idField
int numRecs = scHeader.nextInt();
scHeader.next(); // String star
scHeader.next(); // String comment
Scanner[] sc = new Scanner[4];
for (i = 0; i < numRecs; i++) {
for (j = 0; j < 4; j++) {
sc[j] = new Scanner(bufInput.readLine());
if (sc[j] == null) {
throw new IOException("No Data Block Record found");
}
}
String keyId = sc[0].next();
String validityStart = sc[0].next();
String saa = new String();
if (sc[0].hasNext()) {
saa = sc[0].next();
} else {
saa = "-999";
}
DataSet dataset = new DataSet();
MetaData meta = dataset.getMeta();
double[][] ang = new double[3][3];
ArrayData siam = new Double2d(ang);
meta.set("instrument", new StringParameter(ins, "Instrument"));
meta.set("aperture", new StringParameter(keyId, "Aperture identifier"));
meta.set("validityStart", new DateParameter(new FineTime(df.parse(validityStart)),
"Validity start time of instrument calibration"));
meta.set("SAA", new DoubleParameter(new Double(saa), "Reference solar aspect angle"));
for (j = 0; j < 3; j++) {
// Java doesn't seem to like the 'D' as the exponent identifier; replace with an 'E'
ang[j][0] = Double.parseDouble(sc[j + 1].next().replace('D', 'E'));
ang[j][1] = Double.parseDouble(sc[j + 1].next().replace('D', 'E'));
ang[j][2] = Double.parseDouble(sc[j + 1].next().replace('D', 'E'));
}
siamDataset.setData(siam);
getSets().put(keyId, dataset);
}
} catch (Exception e) {
e.printStackTrace();
}
return;
}
It is a bit of mess in terms of readability and maintenance so I wanted to clean it up. It seems to me the Scanner[]
is a little strange? Basically each block in the file is made up on 4 lines so I thought it could be better to do it that way rather than create these arrays.
I even wondered if there could be a nice concise solution using java 8?