We are about to switch to a new forum software. Until then we have removed the registration on this forum.
The only way I can find to write to a file is by creating it. E.g. output = createWriter("positions.txt");. The problem is that I want to log values from every time that I open the program in the same file. Is there any way that I can do this? This is just example code but it is my problem in a nutshell:
PrintWriter output;
void setup() {
output = createWriter("positions.txt");
}
void draw() {
output.println(mouseX + "t" + mouseY); // Write the coordinate to the file
output.flush(); // Writes the remaining data to the file
}
Answers
I suggest to search logging or append to file from the main site. This question comes up often (might be worth a Technical FAQ entry...).
A simple example; but lacking loading: 3:-O
saveStrings() not really that helpful in this case, i don't think. having to save your entire history every run (and possible previous runs) and then output that at the end would make for pretty terrible logging.
better just to open a file in append mode, and write the necessary information. unfortunately the processing helper method that saves you having to worry about writers and output streams and all that stuff doesn't give you an append flag.
stackoverflow recommends the following java:
but i haven't tried that in processing (probably needs extra imports?) and opening and closing the file everytime you want to write something is probably sub-optimal.
anyone had any luck with log4j, say, and processing?
One of the things you can find with the search I suggested...
http://bazaar.launchpad.net/~philho/+junk/Processing/view/head:/_SmallPrograms/Email/Logger.java
Not super-efficient, indeed, but as long as you don't write megabytes of data per second, it should be OK.
I used this and made a simple example to quickly enter 3D points into a file.