|
44 | 44 | <p> |
45 | 45 | Java includes a class called <c>File</c> in the <c>io</c> library. The class can be imported with the following line. Be sure to capitalize <c>File</c>. |
46 | 46 | </p> |
47 | | - <program xml:id = "file-class-import-example" interactive="activecode" language="java"> |
| 47 | + <program xml:id = "file-class-import-io-example" interactive="activecode" language="java"> |
48 | 48 | <code> |
49 | 49 | import java.io.File; |
50 | 50 | import java.io.IOException;public class Main { |
|
94 | 94 | <p> |
95 | 95 | We will now create a <c>File</c> object. It is important to create a meaningful name for the <c>File</c> object. We will call ours <c>myFile</c>. |
96 | 96 | </p> |
97 | | - <datafile label="my-file-8-2-1" filename="myfile.txt" xml:id= "my-file-8-2-1" editable="no" include-source="yes"> |
| 97 | + <datafile label="my-file-8-2-1" filename="myfile.txt" xml:id= "my-file-8-2-1" editable="yes"> |
98 | 98 | <pre> |
99 | 99 | empty file |
100 | 100 | </pre> |
101 | 101 | </datafile> |
102 | | - <program interactive="activecode" language="java"> |
| 102 | + <program xml:id="create-file-exp-1" interactive="activecode" language="java" add-files="my-file-8-2-1"> |
103 | 103 | <code> |
104 | | - import java.io.File;public class Main { |
| 104 | + import java.io.File; |
| 105 | + public class Main { |
105 | 106 | public static void main(String[] args) { |
106 | 107 | File myFile = new File("myfile.txt"); |
107 | 108 | System.out.println(myFile); |
|
123 | 124 | <p> |
124 | 125 | First, lets look at the equivalent Python code: |
125 | 126 | </p> |
126 | | - <datafile label="my-file-8-2-2" filename="myfile.txt" xml:id= "my-file-8-2-2" editable="no" include-source="yes"> |
127 | | - <pre> |
128 | | - empty file |
129 | | - </pre> |
130 | | - </datafile> |
131 | | - <program interactive="activecode" language="python"> |
| 127 | + |
| 128 | + <program xml:id="creat-file-exp-2" interactive="activecode" language="python"> |
132 | 129 | <code> |
133 | 130 | filename = "newfile.txt" |
134 | 131 | print(f"Attempting to write to '{filename}' using 'w' mode...") |
|
145 | 142 | <p> |
146 | 143 | Now, let's look at Java code that accomplishes the same task: |
147 | 144 | </p> |
148 | | - <datafile label="my-file-8-2-3" filename="myfile.txt" xml:id= "my-file-8-2-3" editable="no" include-source="yes"> |
| 145 | + <datafile label="my-file-8-2-3" filename="myfile.txt" xml:id= "my-file-8-2-3" editable="yes"> |
149 | 146 | <pre> |
150 | 147 | empty file |
151 | 148 | </pre> |
152 | 149 | </datafile> |
153 | | - <program interactive="activecode" language="java"> |
| 150 | + <program xml:id="create-file-exp3" interactive="activecode" language="java" add-files="my-file-8-2-3"> |
154 | 151 | <code> |
155 | 152 | import java.io.File; |
156 | 153 | import java.io.IOException; |
|
181 | 178 | <p> |
182 | 179 | Let's take a look at how we can use Java to read file contents. We'll start again with library imports and building a class, this time importing the Scanner and FileNotFoundException classes. We will call this class ReadFile: |
183 | 180 | </p> |
184 | | - <datafile label="my-file" filename="myfile.txt" xml:id= "my-file" editable="no" include-source="yes"> |
| 181 | + <datafile label="my-file" filename="myfile.txt" xml:id= "my-file" editable="no"> |
185 | 182 | <pre> |
186 | 183 | 1 |
187 | 184 | 2 |
|
195 | 192 | </datafile> |
196 | 193 |
|
197 | 194 |
|
198 | | - <program interactive="activecode" language="java" add-files= "my-file"> |
| 195 | + <program xml:id="file-read-exp1" interactive="activecode" language="java" add-files= "my-file"> |
199 | 196 | <code> |
200 | 197 | import java.io.File; |
201 | 198 | import java.io.FileNotFoundException; |
|
223 | 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: |
224 | 221 | </p> |
225 | 222 |
|
226 | | - <program interactive="activecode" language="java"> |
| 223 | + <program xml:id="file-read-exp2" interactive="activecode" language="java"> |
227 | 224 | <code> |
228 | 225 | import java.io.File; |
229 | 226 | import java.io.FileNotFoundException; |
|
251 | 248 | The next lines consists of a Python code example that reads each line of the file passed to the Scanner object.: |
252 | 249 | </p> |
253 | 250 |
|
254 | | - <program interactive="activecode" language="python"> |
| 251 | + <program xml:id="file-read-exp3" interactive="activecode" language="python" add-files="my-file"> |
255 | 252 | <code> |
256 | | - with open("filename.txt", "r") as file_reader: |
| 253 | + with open("myfile.txt", "r") as file_reader: |
257 | 254 | for line in file_reader: |
258 | 255 | print(line.strip()) |
259 | 256 | </code> |
|
263 | 260 | The equivalent Java code: |
264 | 261 | </p> |
265 | 262 |
|
266 | | - <program interactive="activecode" language="java" add-files= "my-file"> |
| 263 | + <program xml:id="file-read-exp4" interactive="activecode" language="java" add-files= "my-file"> |
267 | 264 | <code> |
268 | 265 | import java.io.File; |
269 | 266 | import java.io.FileNotFoundException; |
|
292 | 289 | Alternatively, the following code can be used to store the all lines of myfile.txt to one variable: |
293 | 290 | </p> |
294 | 291 |
|
295 | | - <program interactive="activecode" language="java"> |
| 292 | + <program xml:id="file-read-exp5" interactive="activecode" language="java" add-files="my-file"> |
296 | 293 | <code> |
297 | 294 | import java.io.File; |
298 | 295 | import java.io.FileNotFoundException; |
|
324 | 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: |
325 | 322 | </p> |
326 | 323 |
|
327 | | - <program interactive="activecode" language="python"> |
| 324 | + <program xml:id="file-read-exp6" interactive="activecode" language="python"> |
328 | 325 | <code> |
329 | 326 | try: |
330 | 327 | with open("myfile.txt", "r") as file_reader: |
|
344 | 341 | And the Java equivalent: |
345 | 342 | </p> |
346 | 343 |
|
347 | | - <program interactive="activecode" language="java"> |
| 344 | + <program xml:id="file-read-exp7" interactive="activecode" language="java" add-files="my-file"> |
348 | 345 | <code> |
349 | 346 | import java.io.File; |
350 | 347 | import java.io.FileNotFoundException; |
|
385 | 382 | Let us create the framework for a class that will write to a file. Let's call this class <c>WriteFile</c>: |
386 | 383 | </p> |
387 | 384 |
|
388 | | - <program interactive="activecode" language="java"> |
| 385 | + <program xml:id="file-write-exp1" interactive="activecode" language="java"> |
389 | 386 | <code> |
390 | 387 | import java.io.File; |
391 | 388 | import java.io.FileWriter; |
|
447 | 444 | 3 |
448 | 445 | </pre> |
449 | 446 | </datafile> |
450 | | - <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"> |
451 | 448 | <code> |
452 | 449 | with open("myfile8-4-2.txt", "r") as file_reader: |
453 | 450 | while True: |
|
466 | 463 |
|
467 | 464 | </pre> |
468 | 465 | </datafile> |
469 | | - <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"> |
470 | 467 | <code> |
471 | 468 | import java.io.File; |
472 | 469 | import java.io.IOException; |
|
494 | 491 |
|
495 | 492 | </pre> |
496 | 493 | </datafile> |
497 | | - <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"> |
498 | 495 | <code> |
499 | 496 | try: |
500 | 497 | with open("myfile.txt", "w") as my_writer: |
|
515 | 512 |
|
516 | 513 | </pre> |
517 | 514 | </datafile> |
518 | | - <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"> |
519 | 516 | <code> |
520 | 517 | import java.io.FileWriter; |
521 | 518 | import java.io.IOException; |
|
562 | 559 |
|
563 | 560 | </pre> |
564 | 561 | </datafile> |
565 | | - <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"> |
566 | 563 | <code> |
567 | 564 | import java.io.FileWriter; |
568 | 565 | import java.io.IOException; |
|
0 commit comments