|
118 | 118 | </note> |
119 | 119 |
|
120 | 120 | <p> |
121 | | - Now that we have created a new <c>File</c> object, we can create a file using the <c>createNewFile()</c> method from the <c>File</c> class. While the previous line of code creates an object within the program for the file, this method actually does the work of creating a file and saving it in the current working directory. This method returns a boolean value. If the method returns <c>true</c>, the file was successfully created. If the method returns <c>false</c>, there is already a file using the chosen file name. We can use this method's possible return values in tandem with an if/else selection to determine if the file was created, or if a file with that file name already exists in the directory. |
| 121 | + Now that we have created a new <c>File</c> object, we can create a file using the <c>createNewFile()</c> method from the <c>File</c> class. While the previous line of code creates an object within the program for the file, this method actually does the work of creating a file and saving it in the current working directory. This method returns a boolean value. If the method returns <c>true</c>, the file was successfully created. If the method returns <c>false</c>, there is already a file using the chosen file name. We can use this method's possible return values in tandem with an try/catch structure to determine if the file was created, or catch the error if a file with that file name already exists in the directory. |
122 | 122 | </p> |
123 | 123 |
|
124 | 124 | <p> |
|
142 | 142 | <p> |
143 | 143 | Now, let's look at Java code that accomplishes the same task: |
144 | 144 | </p> |
145 | | - <datafile label="my-file-8-2-3" filename="myfile.txt" xml:id= "my-file-8-2-3" editable="yes"> |
146 | | - <pre> |
147 | | - empty file |
148 | | - </pre> |
149 | | - </datafile> |
150 | | - <program xml:id="create-file-exp3" interactive="activecode" language="java" add-files="my-file-8-2-3"> |
| 145 | + |
| 146 | + <program xml:id="create-file-exp3" interactive="activecode" language="java"> |
151 | 147 | <code> |
152 | 148 | import java.io.File; |
153 | 149 | import java.io.IOException; |
154 | 150 |
|
155 | 151 | public class CreateFile { |
156 | 152 | public static void main(String[] args) { |
157 | | - if (myFile.createNewFile()) { // If the file was created successfully |
158 | | - System.out.println("The file " + myFile.getName() + " was created sucessfully."); |
159 | | - } else { // If a file with the file name chosen already exists |
160 | | - System.out.println("The file " + myFile.getName() + " already exists."); |
| 153 | + File myFile = new File("newfile.txt"); |
| 154 | + try { |
| 155 | + if (myFile.createNewFile()) { |
| 156 | + System.out.println("The file " + myFile.getName() + " was created successfully."); |
| 157 | + } else { |
| 158 | + System.out.println("The file " + myFile.getName() + " already exists."); |
| 159 | + } |
| 160 | + } catch (IOException e) { |
| 161 | + // This code runs if an IOException occurs |
| 162 | + System.out.println("An error occurred while creating the file."); |
| 163 | + e.printStackTrace(); // This prints the stack trace for more detailed error info |
161 | 164 | } |
162 | 165 | } |
163 | 166 | } |
|
220 | 223 | 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 | 224 | </p> |
222 | 225 |
|
223 | | - <program xml:id="file-read-exp2" interactive="activecode" language="java"> |
| 226 | + <program xml:id="file-read-exp2" interactive="activecode" language="java" add-files="my-file"> |
224 | 227 | <code> |
225 | 228 | import java.io.File; |
226 | 229 | import java.io.FileNotFoundException; |
|
329 | 332 | for line in file_reader: |
330 | 333 | data += line # line already includes the newline character |
331 | 334 | print(data) |
332 | | - except FileNotFoundError as e: |
| 335 | + except FileNotFoundError: |
333 | 336 | print("An error occurred.") |
334 | 337 | import traceback |
335 | 338 | traceback.print_exc() |
|
341 | 344 | And the Java equivalent: |
342 | 345 | </p> |
343 | 346 |
|
344 | | - <program xml:id="file-read-exp7" interactive="activecode" language="java" add-files="my-file"> |
| 347 | + <program xml:id="file-read-exp" interactive="activecode" language="java" add-files="my-file"> |
345 | 348 | <code> |
346 | 349 | import java.io.File; |
347 | 350 | import java.io.FileNotFoundException; |
|
437 | 440 | <p> |
438 | 441 | Next, we will again add the required try/catch blocks utilizing the <c>IOException</c> class. Just like with creating files, the program will not compile without these crucial additions! We will also add some print statements to inform us of the success of the file write operation. First, a Python example: |
439 | 442 | </p> |
440 | | - <datafile label="my-file-8-4-2" filename="myfile8-4-2.txt" xml:id= "my-file-8-4-2" editable="yes" include-source="yes"> |
| 443 | + <datafile label="my-file-8-4-2" filename="myfile8-4-2.txt" xml:id= "my-file-8-4-2" editable="yes"> |
441 | 444 | <pre> |
442 | 445 | 1 |
443 | 446 | 2 |
|
458 | 461 | <p> |
459 | 462 | And the equivalent Java code: |
460 | 463 | </p> |
461 | | - <datafile label="my-file-8-4-3" filename="myfile8-4-3.txt" xml:id= "my-file-8-4-3" editable="yes" include-source="yes"> |
| 464 | + <datafile label="my-file-8-4-3" filename="myfile8-4-3.txt" xml:id= "my-file-8-4-3" editable="yes"> |
462 | 465 | <pre> |
463 | 466 |
|
464 | 467 | </pre> |
|
486 | 489 | <p> |
487 | 490 | And that's it! We will add our code to the foundational code for a complete program. First, an example of equivalent Python code: |
488 | 491 | </p> |
489 | | - <datafile label="my-file-8-4-4" filename="myfile.txt" xml:id= "my-file-8-4-4" editable="yes" include-source="yes"> |
| 492 | + <datafile label="my-file-8-4-4" filename="myfile.txt" xml:id= "my-file-8-4-4" editable="yes"> |
490 | 493 | <pre> |
491 | 494 |
|
492 | 495 | </pre> |
|
507 | 510 | <p> |
508 | 511 | The completed Java code: |
509 | 512 | </p> |
510 | | - <datafile label="my-file-8-4-5" filename="myfile.txt" xml:id= "my-file-8-4-5" editable="yes" include-source="yes"> |
| 513 | + <datafile label="my-file-8-4-5" filename="myfile.txt" xml:id= "my-file-8-4-5" editable="yes"> |
511 | 514 | <pre> |
512 | 515 |
|
513 | 516 | </pre> |
|
554 | 557 | <p> |
555 | 558 | Now, when we use <c>write()</c> method like before, the text will be appended if there is already text in the document. If we were to update our code to include the boolean argument: |
556 | 559 | </p> |
557 | | - <datafile label="my-file-8-4-6" filename="myfile.txt" xml:id= "my-file-8-4-6" editable="yes" include-source="yes"> |
| 560 | + <datafile label="my-file-8-4-6" filename="myfile.txt" xml:id= "my-file-8-4-6" editable="yes"> |
558 | 561 | <pre> |
559 | 562 |
|
560 | 563 | </pre> |
|
0 commit comments