Reputation: 85
I dont know how to save to a txt file in processing, i have tried some different things, but every time it seems like the file is overridden because of:
output = createWriter("positions.txt");
If i try to remove this line, or try something else to check if the file insist and then dont run the line, i a NullPointerException.
Is there any way to save a file without the file getting overridden?
PrintWriter output;
void setup() {
// Create a new file in the sketch directory
output = createWriter("positions.txt");
}
void draw() {
point(mouseX, mouseY);
output.println(mouseX); // Write the coordinate to the file
}
void keyPressed() {
output.flush(); // Writes the remaining data to the file
output.close(); // Finishes the file
exit(); // Stops the program
}
Upvotes: 0
Views: 2952
Reputation: 42176
Like you've noticed, Processing's createWriter()
function creates a new file every time it's called.
If you google "Processing append to text file" or "Java append to text file" you'll get a ton of results.
Basically, you want to use Java's PrintWriter
class, whose constructor takes a boolean
argument. If that argument is true
, your data will be appended to the end of the file instead of overwriting the original.
Upvotes: 1