I would like a review of the transformation of the first method to the updated one below:
private static ArrayList<String> getValuesfromXML(String key1, String key2) { ArrayList<String> valueArray = new ArrayList<String>(); try { String scriptPath = getScriptPath("prop.xml"); File file = new File(scriptPath); FileInputStream fileInput = new FileInputStream(file); Properties properties = new Properties(); properties.loadFromXML(fileInput); fileInput.close(); valueArray.add(properties.getProperty(key1)); valueArray.add(properties.getProperty(key2)); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return valueArray; }
It looks as if it's GC'd so there's no memory leaks. And is not using a finally on the filehandle really an issue?
/**
* read property values from the configuration file. Using standard Java Properties class.
*
* @param key1
* - Property key
* @param key2
* - Property Key
* @return - ArrayList values of properties based on keys
*/
private static ArrayList<String> getPropertyValue (String key1, String key2){
final ArrayList<String> valueArray = new ArrayList<>();
String scriptPath = getScriptPath(propertiesFileName);
try (FileInputStream fileInput = new FileInputStream(scriptPath)) {
Properties properties = new Properties();
properties.loadFromXML(fileInput);
valueArray.add(properties.getProperty(key1));
valueArray.add(properties.getProperty(key2));
} catch (Exception ex) {
ex.printStackTrace();
}
return valueList;
}