Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add examples for PrintWriter write, print, and close #2569

Merged
merged 1 commit into from
Jan 23, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 93 additions & 2 deletions src/io/files.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
* <div class="norender">
* <code>
* // 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();
* </code>
* </div>
* <div class='norender'>
* <code>
* // 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();
* </code>
* </div>
* <div class='norender'>
* <code>
* // 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();
* </code>
* </div>
*/
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
* <div class='norender'>
* <code>
* // 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();
* </code>
* </div>
* <div class='norender'>
* <code>
* 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();
* }
* </code>
* </div>
*/
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
* <div class="norender">
* <code>
* // create a file called 'newFile.txt'
* var writer = createWriter('newFile.txt');
* // close the PrintWriter and save the file
* writer.close();
* </code>
* </div>
* <div class='norender'>
* <code>
* // 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();
* </code>
* </div>
*/
this.close = function() {
// convert String to Array for the writeFile Blob
Expand Down