-2

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();
                }

            }
        }
    }
}
Arnaud Denoyelle
  • 29,980
  • 16
  • 92
  • 148
  • 1
    Welcome to StackOverflow ! `it's not printing out correctly` can you be more specific ? Can you give an example of expected/actual output ? – Arnaud Denoyelle Apr 26 '19 at 14:43
  • Exactly. Please post what is in the output file, what you expect, and also what the system.out.prinln gives. It seems to me that you only need to [pretty-print your map](https://stackoverflow.com/questions/10120273/pretty-print-a-map-in-java/52218365) – user1485864 Apr 26 '19 at 14:46
  • I've added what I'm getting from the file and what I'd like it to look like. – Cassie DeFauw Apr 26 '19 at 15:16

2 Answers2

0

When you call System.out.println(map);, you see the result of map.toString(). If this is what you want to have in the file, you can do it like this :

FileOutputStream f = new FileOutputStream("hashmap.ser");  
f.write(map.toString().getBytes());
System.out.println(map);
f.close();
Arnaud Denoyelle
  • 29,980
  • 16
  • 92
  • 148
0

You should use PrintWriter, cause nö you are outputting a binary Data

Alex Mi
  • 1,409
  • 2
  • 21
  • 35