I am using a BufferedReader, and though I call the close() method, eclipse still gives me a warning. Eclipse does not give me a warning if I place the close() call before the while, but in then the code does not work. Is there either an error in my code, or what else is the problem? Code:
Hashtable<String, Hashtable<String, Integer>> buildingStats = new Hashtable<String, Hashtable<String, Integer>>();
try
{
BufferedReader br = new BufferedReader(new FileReader(new File("Assets/Setup/Buildings.txt"))); // Sets the buildings values to the values in Buildings.tx
String line;
int lineNum = 0;
while((line = br.readLine()) != null)
{
++lineNum;
String[] values = line.split(",");
if (values.length != 3)
throw new Exception("Invalid data in Assets/Setup/Buildings.txt at line " + lineNum);
if (buildingStats.containsKey(values[0]))
{
buildingStats.get(values[0]).put(values[1], Integer.parseInt(values[2]));
}
else
{
buildingStats.put(values[0], new Hashtable<String, Integer>());
buildingStats.get(values[0]).put(values[1], Integer.parseInt(values[2]));
}
}
br.close();
}
catch (IOException e)
{
e.printStackTrace();
}
catch (Exception e)
{
e.printStackTrace();
}
return buildingStats;
You should put it in a finally method like so:
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader(new File("Assets/Setup/Buildings.txt")));
// do things
} catch (Exception e){
//Handle exception
} finally {
try {
br.close();
} catch (Exception e){}
}
If you still get the warning try cleaning and rebuilding your eclipse project.