Add float and string value in a single array and use that to write .CSV file

Am trying to create a .CSV file using float and string values in a single array. And use the array values to write .CSV file (I have many entries ex: > 50,000)

Ist String value...

long _time = (dataValue.getInt(0) & 0xffffffffL);
Date date = new Date(_time*1000L);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
String formattedDate = sdf.format(date); // 2015-07-03 09:48:41

IInd Float value...

float _resistanceValue = dataValue.getFloat(8); // 1.60774944E8

I need to join these two values dynamically {inside the while loop} in an array and using that value to write .CSV file

I have tried to join the values like...

ArrayList updateValue = new ArrayList();
updateValue.add(formattedDate,_resistanceValue);
//(Which I am not successful)

Using the below code to write .CSV file

private static final String FILE_HEADER = "Time,ResistanceValue";
private static final String NEW_LINE_SEPARATOR = "\n";
FileWriter fileWriter = null;

String fileName = "C:\\temp.csv";
FileWriter fileWriter = null;
        try {
            fileWriter = new FileWriter(fileName);
            //Write the CSV file header
            fileWriter.append(FILE_HEADER.toString());
            //Add a new line separator after the header
            fileWriter.append(NEW_LINE_SEPARATOR);

            //Write a values of "formattedDate" & "_resistanceValue"  into the CSV file
            fileWriter.append(updateValue);

            System.out.println("CSV file was created successfully !!!");
        } catch (Exception e) {
            System.out.println("Error in CsvFileWriter !!!");
            e.printStackTrace();
        } finally {
            try {
                fileWriter.flush();
                fileWriter.close();
            } catch (IOException e) {
                System.out.println("Error while flushing/closing fileWriter !!!");
                e.printStackTrace();
            }
        }

Please give me a direction to achieve it. Thanks

Answers


You have to write each one by one, separated by comma(,) as delimiter as you need csv.

So Instead of this

//Write a values of "formattedDate" & "_resistanceValue"  into the CSV file
        fileWriter.append(updateValue);

It should be

//for loop to get the data and then for each data ,keep on writing in file.

    fileWriter.append(formattedDate);
    fileWriter.append(',');
    fileWriter.append(_resistanceValue);
    fileWriter.append('\n');

I worked around and used long approach to achieve it....

Firstly, declared three lists...

ArrayList<String> addTime = new ArrayList<String>();
ArrayList<String> addRV = new ArrayList<String>();
ArrayList<String> addTimeandRV = new ArrayList<String>();

Adding the appropriate value to it....

addTime.add(loopIncr, formattedDate); //add data&time to list
addRV.add(loopIncr, formattedResistanceValue); // add RV to list

and joined as @naruto's suggestion and write into a file (Worked!!!)...

    for(int i = 0; i < addTime.size(); i++) {
        addTimeandRV.add(addTime.get(i));
        addTimeandRV.add(",");
        addTimeandRV.add(addRV.get(i));
        addTimeandRV.add(NEW_LINE_SEPARATOR);
    }

    for (String str : addTimeandRV) {
        fileWriter.write(str);
    }

I got successful output .CSV file


Need Your Help

Bootstrap Popover inside a table

jquery ajax popover

I have an issue to implement bootstrap popover inside a table. Here is a popover code i have took from the web.

Delphi 7 - source of web page (gzipped) - WebBrowser

delphi web gzip delphi-7 twebbrowser

How to put the source code of a website in memo1 when it is compressed with gzip.