How to dump Hashmap data (variable key/value size) into a .csv file at a specific memory location in Android Studio?

Viewed 192

My hashmap has String type as key and ArrayList type as value, example {"v1"=[1.1,1.2,1.3], "v2"=[2.1,2.2,2.3,2.4]} where "v1" and "v2" are keys. There are more than 40 keys (previously unknown size) and each key has a ArrayList which may not be uniform in size (size >200). I want to write this hashmap data in the following format in a .csv file in my Android phone.

v1   v2 
1.1  2.1
1.2  2.2
1.3  2.3

Until now, my Hashmap had fixed key/array size and I used the following code snippet to dump the data:

Calendar calendar = Calendar.getInstance();
                int hour = calendar.get(Calendar.HOUR_OF_DAY);
                int minute = calendar.get(Calendar.MINUTE);
                int second = calendar.get(Calendar.SECOND);
              
                File folder = new File(Environment.getExternalStorageDirectory() + "/santobedi" + hour + "-" + minute + "-" + second);
                String csv = folder.getAbsolutePath() + "/data.csv";
                try {
                    file_writer = new FileWriter(csv, true);

                    if (isReady == false) {  
                        String s = "v1, v2,\n";
                        file_writer.append(s);
                        isReady = true; // The columns heads are set
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }

Later, I'd append (row-wise) the values on a loop checking the Boolean value isReady. Now, the size of the key is not fixed, so I cannot set the column heads as before. I searched for a possible solution, this question is almost similar to my situation. However, it is not related to memory location inside the Android phone and the accepted answer is still not solving my problem.

Any help, please!

1 Answers

I solved my problem with the following approach:

 public FileWriter file_writer;
 Calendar calendar = Calendar.getInstance();
                int hour = calendar.get(Calendar.HOUR_OF_DAY);
                int minute = calendar.get(Calendar.MINUTE);
                int second = calendar.get(Calendar.SECOND);
                File folder = new File(Environment.getExternalStorageDirectory() + "/Santobedi" + hour + "-" + minute + "-" + second);
                folder.mkdir();
                String csv = folder.getAbsolutePath() + "/data.csv";
                try { file_writer = new FileWriter(csv, true);
                } catch (IOException e) {
                    e.printStackTrace();
                }

Later, a writeToCSV method is used to append data in the file_writer as follows:

public void writeToCsv() throws IOException {

        for(Map.Entry<String , ArrayList<Float>>IwantItasColumn:myHashMap.entrySet())
        {
            String s = "";
           for(int i =0; i<IwantItasColumn.getValue().size();i++)
           {
               s = s+ ","+ String.valueOf(IwantItasColumn.getValue().get(i));
           }
           s = AccessPointId.getKey() + "," + s + "\n";
           file_writer.append(s);
        }

        try {
            file_writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }}

The above code snippet appends the data row-wise in the data.csv file (disregard to the size of the hashmap, size of keys, size of ArrayList). For example:

v1 1.1 1.2 1.3
v2 2.1 2.2 2.3

A simple row to column transformation in Excel gives me the required result.

Related