diff --git a/src/io/files.js b/src/io/files.js index 8c1f87756a..b0b6992107 100644 --- a/src/io/files.js +++ b/src/io/files.js @@ -1111,27 +1111,118 @@ p5.PrintWriter = function(filename, extension) { this.content = ''; //Changed to write because it was being overloaded by function below. /** + * Writes data to the PrintWriter stream * @method write - * @param {Array} data + * @param {Array} data all data to be written by the PrintWriter + * @example + *
+ * + * // creates a file called 'newFile.txt' + * var writer = createWriter('newFile.txt'); + * // write 'Hello world!'' to the file + * writer.write(['Hello world!']); + * // close the PrintWriter and save the file + * writer.close(); + * + *
+ *
+ * + * // creates a file called 'newFile2.txt' + * var writer = createWriter('newFile2.txt'); + * // write 'apples,bananas,123' to the file + * writer.write(['apples', 'bananas', 123]); + * // close the PrintWriter and save the file + * writer.close(); + * + *
+ *
+ * + * // creates a file called 'newFile3.txt' + * var writer = createWriter('newFile3.txt'); + * // write 'My name is: Teddy' to the file + * writer.write('My name is:'); + * writer.write(' Teddy'); + * // close the PrintWriter and save the file + * writer.close(); + * + *
*/ this.write = function(data) { this.content += data; }; /** + * Writes data to the PrintWriter stream, and adds a new line at the end * @method print - * @param {Array} data + * @param {Array} data all data to be printed by the PrintWriter + * @example + *
+ * + * // creates a file called 'newFile.txt' + * var writer = createWriter('newFile.txt'); + * // creates a file containing + * // My name is: + * // Teddy + * writer.print('My name is:'); + * writer.print('Teddy'); + * // close the PrintWriter and save the file + * writer.close(); + * + *
+ *
+ * + * var writer; + * + * function setup() { + * createCanvas(400, 400); + * // create a PrintWriter + * writer = createWriter('newFile.txt'); + * } + * + * function draw() { + * // print all mouseX and mouseY coordinates to the stream + * writer.print([mouseX, mouseY]); + * } + * + * function mouseClicked() { + * // close the PrintWriter and save the file + * writer.close(); + * } + * + *
*/ this.print = function(data) { this.content += data + '\n'; }; /** + * Flushes the PrintWriter object * @method flush + * @example */ this.flush = function() { this.content = ''; }; /** + * Closes the PrintWriter * @method close + * @example + *
+ * + * // create a file called 'newFile.txt' + * var writer = createWriter('newFile.txt'); + * // close the PrintWriter and save the file + * writer.close(); + * + *
+ *
+ * + * // create a file called 'newFile2.txt' + * var writer = createWriter('newFile2.txt'); + * // write some data to the file + * writer.write([100, 101, 102]); + * // close the PrintWriter and save the file + * writer.close(); + * + *
*/ this.close = function() { // convert String to Array for the writeFile Blob