I'm working on a project where I have a file that the program accesses to get information on different crimes from a range of years. It then needs to add up the crime based on the type and put it into a file. I have the first part down, it does access the file and adds up the crime amounts by type but when I open the file that is created, it's not printing out correctly and I can't seem to find what's wrong.
This is what prints out on the file:
¬í sr java.util.HashMapÚÁÃ`Ñ F loadFactorI thresholdxp?@ w
t Violent Crimes Totalsr java.lang.Integerâ ¤÷‡8 I valuexr java.lang.Number†¬•”à‹ xp ¤Mt Rapesq ~ jt Vehicle Theftsq ~ {™t Aggravated Assaultsq ~ kƒt Homicidesq ~ t Robberysq ~ N t NonResidential Burglarysq ~ kÿt Residential Burglarysq ~ ã~t Property Crimes Totalsq ~ ïit Theftsq ~ :cx
With the system.out.println it prints:
{Violent Crimes Total=42061, Rape=1898, Vehicle Theft=97177, Aggravated Assault=27523, Homicide=399, Robbery=19981, NonResidential Burglary=27647, Residential Burglary=58238, Property Crimes Total=454505, Theft=342627}
The system print out is what I'd like to show up in the file.
public class CSVReader {
public static void main(String[] args) throws FileNotFoundException {
String csvFile = "C:\\Users\\Cassie\\Desktop\\mod04_dataset.csv";
BufferedReader br = null;
String line = "";
String cvsSplitBy = ",";
HashMap<String, Integer> map = new HashMap<>();
try {
br = new BufferedReader(new FileReader(csvFile));
br.readLine();
while ((line = br.readLine()) != null) {
String[] data = line.split(cvsSplitBy);
System.out.println(data[2] + " " + data[3]);
if (map.containsKey(data[2])) {
Integer a = map.get(data[2]);
map.put(data[2], a + Integer.parseInt(data[3]));
}
else {
map.put(data[2], Integer.parseInt(data[3]));
}
}
FileOutputStream f = new
FileOutputStream("hashmap.ser");
ObjectOutputStream s = new ObjectOutputStream(f);
s.writeObject(map);
System.out.println(map);
s.close();
f.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}