|
192 | 192 | </datafile> |
193 | 193 |
|
194 | 194 |
|
195 | | - <program interactive="activecode" language="java" add-files= "my-file"> |
| 195 | + <program xml:id="file-read-exp1" interactive="activecode" language="java" add-files= "my-file"> |
196 | 196 | <code> |
197 | 197 | import java.io.File; |
198 | 198 | import java.io.FileNotFoundException; |
|
220 | 220 | We will then create a new File object exactly the same as the one from the section on creating files. Additionally, we will create a Scanner object. The Scanner object is the object that does the file reading. We will call this scanner fileReader: |
221 | 221 | </p> |
222 | 222 |
|
223 | | - <program interactive="activecode" language="java"> |
| 223 | + <program xml:id="file-read-exp2" interactive="activecode" language="java"> |
224 | 224 | <code> |
225 | 225 | import java.io.File; |
226 | 226 | import java.io.FileNotFoundException; |
|
248 | 248 | The next lines consists of a Python code example that reads each line of the file passed to the Scanner object.: |
249 | 249 | </p> |
250 | 250 |
|
251 | | - <program interactive="activecode" language="python" add-files="my-file"> |
| 251 | + <program xml:id="file-read-exp3" interactive="activecode" language="python" add-files="my-file"> |
252 | 252 | <code> |
253 | 253 | with open("myfile.txt", "r") as file_reader: |
254 | 254 | for line in file_reader: |
|
260 | 260 | The equivalent Java code: |
261 | 261 | </p> |
262 | 262 |
|
263 | | - <program interactive="activecode" language="java" add-files= "my-file"> |
| 263 | + <program xml:id="file-read-exp4" interactive="activecode" language="java" add-files= "my-file"> |
264 | 264 | <code> |
265 | 265 | import java.io.File; |
266 | 266 | import java.io.FileNotFoundException; |
|
289 | 289 | Alternatively, the following code can be used to store the all lines of myfile.txt to one variable: |
290 | 290 | </p> |
291 | 291 |
|
292 | | - <program interactive="activecode" language="java" add-files="my-file"> |
| 292 | + <program xml:id="file-read-exp5" interactive="activecode" language="java" add-files="my-file"> |
293 | 293 | <code> |
294 | 294 | import java.io.File; |
295 | 295 | import java.io.FileNotFoundException; |
|
321 | 321 | Using the second method of storing all file contents to one file, the resulting full code including try/catch blocks (this time using FileNotFoundException instead of IOException) will look something like this. First, the Python code: |
322 | 322 | </p> |
323 | 323 |
|
324 | | - <program interactive="activecode" language="python"> |
| 324 | + <program xml:id="file-read-exp6" interactive="activecode" language="python"> |
325 | 325 | <code> |
326 | 326 | try: |
327 | 327 | with open("myfile.txt", "r") as file_reader: |
|
341 | 341 | And the Java equivalent: |
342 | 342 | </p> |
343 | 343 |
|
344 | | - <program interactive="activecode" language="java" add-files="my-file"> |
| 344 | + <program xml:id="file-read-exp7" interactive="activecode" language="java" add-files="my-file"> |
345 | 345 | <code> |
346 | 346 | import java.io.File; |
347 | 347 | import java.io.FileNotFoundException; |
|
382 | 382 | Let us create the framework for a class that will write to a file. Let's call this class <c>WriteFile</c>: |
383 | 383 | </p> |
384 | 384 |
|
385 | | - <program interactive="activecode" language="java"> |
| 385 | + <program xml:id="file-write-exp1" interactive="activecode" language="java"> |
386 | 386 | <code> |
387 | 387 | import java.io.File; |
388 | 388 | import java.io.FileWriter; |
|
444 | 444 | 3 |
445 | 445 | </pre> |
446 | 446 | </datafile> |
447 | | - <program interactive="activecode" language="python" add-files = "my-file-8-4-2"> |
| 447 | + <program xml:id="file-write-exp2" interactive="activecode" language="python" add-files = "my-file-8-4-2"> |
448 | 448 | <code> |
449 | 449 | with open("myfile8-4-2.txt", "r") as file_reader: |
450 | 450 | while True: |
|
463 | 463 |
|
464 | 464 | </pre> |
465 | 465 | </datafile> |
466 | | - <program interactive="activecode" language="java" add-files = "my-file-8-4-3"> |
| 466 | + <program xml:id="file-write-exp3" interactive="activecode" language="java" add-files = "my-file-8-4-3"> |
467 | 467 | <code> |
468 | 468 | import java.io.File; |
469 | 469 | import java.io.IOException; |
|
491 | 491 |
|
492 | 492 | </pre> |
493 | 493 | </datafile> |
494 | | - <program interactive="activecode" language="python" add-files = "my-file-8-4-4"> |
| 494 | + <program xml:id="file-write-exp4" interactive="activecode" language="python" add-files = "my-file-8-4-4"> |
495 | 495 | <code> |
496 | 496 | try: |
497 | 497 | with open("myfile.txt", "w") as my_writer: |
|
512 | 512 |
|
513 | 513 | </pre> |
514 | 514 | </datafile> |
515 | | - <program interactive="activecode" language="java" add-files = "my-file-8-4-5"> |
| 515 | + <program xml:id="file-write-exp5" interactive="activecode" language="java" add-files = "my-file-8-4-5"> |
516 | 516 | <code> |
517 | 517 | import java.io.FileWriter; |
518 | 518 | import java.io.IOException; |
|
559 | 559 |
|
560 | 560 | </pre> |
561 | 561 | </datafile> |
562 | | - <program interactive="activecode" language="java" add-files = "my-file-8-4-6"> |
| 562 | + <program xml:id="file-write-exp6" interactive="activecode" language="java" add-files = "my-file-8-4-6"> |
563 | 563 | <code> |
564 | 564 | import java.io.FileWriter; |
565 | 565 | import java.io.IOException; |
|
0 commit comments