4

I used the code below to save output from an Arduino Uno to a text file.

The problem is whenever I run the code the old data in the text file is deleted.

I don't want the old data to be deleted, what should I change?

import processing.serial.*;
Serial mySerial;
PrintWriter output;
void setup() {
   mySerial = new Serial( this, Serial.list()[0], 9600 );
   output = createWriter( "data.txt" );
}
void draw() {
    if (mySerial.available() > 0 ) {
         String value = mySerial.readString();
         if ( value != null ) {
              output.println( value );
         }
    }
}

void keyPressed() {
    output.flush();  // Writes the remaining data to the file
    output.close();  // Finishes the file
    exit();  // Stops the program
}
Ignacio Vazquez-Abrams
  • 17,733
  • 1
  • 28
  • 32

1 Answers1

1

There are a couple of ways to append to a text file.

  1. You can either read the entire file in a temporary variable, then append the new data to it and then write to file.

  2. you can implement using printWriter instead like mentioned here: https://processing.org/discourse/beta/num_1267767630.html

Let me know if it works.