Saving text files
In the previous chapter, you learned about opening text files. We'll take a look at how you can save them.
How to do it...
The first thing you'll need to do is declare an object of the PrintWriter
type and initialize it with the createWriter()
function.
PrintWriter textFile; void setup() { textFile = createWriter("files/randomnumbers.txt"); }
In each cycle of the draw()
function, we'll write a random number to the file. When the frameCount
variable reaches 1000, we'll save the file and quit the application.
void draw() { textFile.println( random( 200 ) ); if ( frameCount >= 1000 ) { textFile.flush(); textFile.close(); exit(); } }
How it works...
The createWriter()
function is used to create a text file. The parameter for this file is a String containing the file name. Just like in the example on saving images, I've added a file directory to the String.
Inside the draw()
function, you'll use the textFile.println()
method. This method works just like the...