0

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

2 Answers 2

1

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');
Sign up to request clarification or add additional context in comments.

7 Comments

Thanks @naruto, so I have to move the code "to write .CSV file" inside the while() loop, Is that correct?
yes...you have to move your dynamic data in while loop and keep on appending to fileWriter.
It cannot be ...print in the console also to see if you are iterating it correctly...as this is how every one uses to generate dynamic csv output...I just checked in my case and it is printing fine... can you show a part of your code...how you are iterating...
@Java.beginner Can you check by using System.out.println as there are few things missing from code... So it will also help you in understanding your mistake...Or run your code in debug mode and place breakpoint ....check how many times it is in loop...
Thanks @naruto, System.out.println gives out all the values (time, resistanceValue) perfectly which is more than 50,000 entries, all I want to capture those values in array or some form and write it .csv file, That's what I have been trying to get out of it. Thanks
|
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.