Skip to content

Commit bab73eb

Browse files
authored
Merge pull request #2569 from carolinehermans/master
Add examples for PrintWriter write, print, and close
2 parents 0b8e2c9 + 08fb4d2 commit bab73eb

File tree

1 file changed

+93
-2
lines changed

1 file changed

+93
-2
lines changed

src/io/files.js

+93-2
Original file line numberDiff line numberDiff line change
@@ -1111,27 +1111,118 @@ p5.PrintWriter = function(filename, extension) {
11111111
this.content = '';
11121112
//Changed to write because it was being overloaded by function below.
11131113
/**
1114+
* Writes data to the PrintWriter stream
11141115
* @method write
1115-
* @param {Array} data
1116+
* @param {Array} data all data to be written by the PrintWriter
1117+
* @example
1118+
* <div class="norender">
1119+
* <code>
1120+
* // creates a file called 'newFile.txt'
1121+
* var writer = createWriter('newFile.txt');
1122+
* // write 'Hello world!'' to the file
1123+
* writer.write(['Hello world!']);
1124+
* // close the PrintWriter and save the file
1125+
* writer.close();
1126+
* </code>
1127+
* </div>
1128+
* <div class='norender'>
1129+
* <code>
1130+
* // creates a file called 'newFile2.txt'
1131+
* var writer = createWriter('newFile2.txt');
1132+
* // write 'apples,bananas,123' to the file
1133+
* writer.write(['apples', 'bananas', 123]);
1134+
* // close the PrintWriter and save the file
1135+
* writer.close();
1136+
* </code>
1137+
* </div>
1138+
* <div class='norender'>
1139+
* <code>
1140+
* // creates a file called 'newFile3.txt'
1141+
* var writer = createWriter('newFile3.txt');
1142+
* // write 'My name is: Teddy' to the file
1143+
* writer.write('My name is:');
1144+
* writer.write(' Teddy');
1145+
* // close the PrintWriter and save the file
1146+
* writer.close();
1147+
* </code>
1148+
* </div>
11161149
*/
11171150
this.write = function(data) {
11181151
this.content += data;
11191152
};
11201153
/**
1154+
* Writes data to the PrintWriter stream, and adds a new line at the end
11211155
* @method print
1122-
* @param {Array} data
1156+
* @param {Array} data all data to be printed by the PrintWriter
1157+
* @example
1158+
* <div class='norender'>
1159+
* <code>
1160+
* // creates a file called 'newFile.txt'
1161+
* var writer = createWriter('newFile.txt');
1162+
* // creates a file containing
1163+
* // My name is:
1164+
* // Teddy
1165+
* writer.print('My name is:');
1166+
* writer.print('Teddy');
1167+
* // close the PrintWriter and save the file
1168+
* writer.close();
1169+
* </code>
1170+
* </div>
1171+
* <div class='norender'>
1172+
* <code>
1173+
* var writer;
1174+
*
1175+
* function setup() {
1176+
* createCanvas(400, 400);
1177+
* // create a PrintWriter
1178+
* writer = createWriter('newFile.txt');
1179+
* }
1180+
*
1181+
* function draw() {
1182+
* // print all mouseX and mouseY coordinates to the stream
1183+
* writer.print([mouseX, mouseY]);
1184+
* }
1185+
*
1186+
* function mouseClicked() {
1187+
* // close the PrintWriter and save the file
1188+
* writer.close();
1189+
* }
1190+
* </code>
1191+
* </div>
11231192
*/
11241193
this.print = function(data) {
11251194
this.content += data + '\n';
11261195
};
11271196
/**
1197+
* Flushes the PrintWriter object
11281198
* @method flush
1199+
* @example
11291200
*/
11301201
this.flush = function() {
11311202
this.content = '';
11321203
};
11331204
/**
1205+
* Closes the PrintWriter
11341206
* @method close
1207+
* @example
1208+
* <div class="norender">
1209+
* <code>
1210+
* // create a file called 'newFile.txt'
1211+
* var writer = createWriter('newFile.txt');
1212+
* // close the PrintWriter and save the file
1213+
* writer.close();
1214+
* </code>
1215+
* </div>
1216+
* <div class='norender'>
1217+
* <code>
1218+
* // create a file called 'newFile2.txt'
1219+
* var writer = createWriter('newFile2.txt');
1220+
* // write some data to the file
1221+
* writer.write([100, 101, 102]);
1222+
* // close the PrintWriter and save the file
1223+
* writer.close();
1224+
* </code>
1225+
* </div>
11351226
*/
11361227
this.close = function() {
11371228
// convert String to Array for the writeFile Blob

0 commit comments

Comments
 (0)