Click here to Skip to main content
15,889,281 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have a project where I have used an Arduino ultrasonic sensor and have connected it to a java program which reads the values produced by the Arduino ultrasonic sensor via serial communication, the java program produces a real time chart plotting values that are being read from the Arduino.

I want to save that real time data into a csv file. The following code simply creates a file and stores it where the project is located on my computer and does contain any real time data:
Java
{
            saveButton.setText("Save Data");
            String csvFile = "data.csv";
            File f = new File(csvFile);
            FileWriter writer = new FileWriter(csvFile);    
    }

What am I missing in order to as mentioned previously store the real time data into the csv file?

What I have tried:

Online website tutorials, YouTube video tutorials and example codes found online.
Posted
Updated 15-May-18 2:44am
v2
Comments
Richard MacCutchan 15-May-18 9:56am    
Data is data, you just need to write it to the file. However, creating a file as in the above code is not going to help because you create a new one each time through that block, but don't write anything to it. Open the file at the very beginning of your program then you can write to it any time.
Member 13710355 15-May-18 10:08am    
First of all thank you for the advice but if you don't mind could you please elaborate on what you mean by " Open the file at the very beginning of your program then you can write to it any time" ?
Richard MacCutchan 15-May-18 10:37am    
1. Start program
1.1. Open file(s) and other initialisation

2. Main loop
2.1. Collect data - if no more data go to 3.
2.2 Write stuff to the file
2.3 go to 2.1

3. Close file, release other resources, and exit

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900