|
34 | 34 | Note the use of <c>import java.lang.Math;</c> in the above to import the <c>Math</c> class. Unlike Python, Java requires explicit <c>import</c> for most libraries, including the <c>Math</c> class and many classes related to file handling. |
35 | 35 | </p> |
36 | 36 |
|
37 | | - <p> |
38 | | - Much like the <c>Math</c> class, in order for your program to work with files you need use <c>import</c>. Java includes a class called <c>File</c> in the <c>io</c> library. This class allows you to create <c>File</c> objects, and use its public methods. the following code imports the <c>File</c> class and creates a <c>File</c> object called myFile. for now focus on how the class is imported and used in the program; We will cover the <c>IOException</c> class and <c>createNewFile</c> method later. |
39 | | - </p> |
40 | | - |
41 | 37 | <p> |
42 | 38 | Much like the <c>Math</c> class, in order for your program to work with files you need use <c>import</c>. Java includes a class called <c>File</c> in the <c>io</c> library. This class allows you to create <c>File</c> objects, and use its public methods. |
43 | 39 | </p> |
|
85 | 81 | import java.io.File; |
86 | 82 | public class CreateFile { |
87 | 83 | public static void main(String[] args) { |
88 | | - |
| 84 | + import java.io.File; |
| 85 | + File myFile = new File("myfile.txt"); |
| 86 | + System.out.println(myFile); |
89 | 87 | } |
90 | 88 | } |
91 | 89 | </input> |
|
99 | 97 |
|
100 | 98 |
|
101 | 99 | <p> |
102 | | - Now let's learn how to make a file in Java. In Python. files can be made using the <c>open()</c> function on a file path that doesn't exist yet. Similarly, in Java you create a file by using the <c>createNewFile()</c> method on a <c>File</c> object. This method actually does the work of creating a file and saving it in the current working directory, and returns a boolean value of either true or false if the file is successfully created. 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. |
| 100 | + Now let's learn how to make a file in Java. In Python. files can be made using the <c>open()</c> function on a file path that doesn't exist yet. Similarly, in Java you create a file by using the <c>createNewFile()</c> method on a <c>File</c> object. This method actually does the work of creating a file and saving it in the current working directory, and returns a boolean value of either true or false if the file is successfully created. We can use this method's possible return values in tandem with an if/else selection to determine if the file was created. Finally, we encase this code within try/catch blocks. This step is required in the Java code to be compiled. If try/catch blocks using <c>IOException</c> are not included, there will be compilation errors. |
103 | 101 | </p> |
104 | 102 |
|
105 | 103 | <p> |
|
263 | 261 | </program> |
264 | 262 |
|
265 | 263 | <p> |
266 | | - In this next step, we will use the <c>write()</c> method from the FileWriter class. This Method will take any data within the parenthesis and write that data to the file selected. The <c>write()</c> method takes most standard data types. First, how this step is completed with Python: |
| 264 | + In this next step, we will use the <c>write()</c> method from the FileWriter class. This method will take any data within the parenthesis and write that data to the file selected. The <c>write()</c> method takes most standard data types. First, how this step is completed with Python: |
267 | 265 | </p> |
268 | 266 |
|
269 | 267 | <program xml:id="write-and-close-python" language="python"> |
|
281 | 279 |
|
282 | 280 | <note> |
283 | 281 | <p> |
284 | | - You may have noticed the <c>close()</c> function being used after writing to the file. This is a very important step and must be included when working with files! Without using this method, the file may remain active in system resources even after the program is closed. This can lead file corruption or other terrible problems that are best avoided! |
| 282 | + The <c>close()</c> method is being used after writing to the file. This is a very important step and must be included when working with files! Without using this method, the file may remain active in system resources even after the program is closed. This can lead file corruption or other terrible problems that are best avoided! |
285 | 283 | </p> |
286 | 284 | </note> |
287 | 285 |
|
288 | 286 | <p> |
289 | 287 | 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: |
290 | 288 | </p> |
291 | | - <datafile label="my-file-8-4-2" filename="myfile8-4-2.txt" xml:id= "my-file-8-4-2" editable="yes"> |
292 | | - <pre> |
293 | | - 1 |
294 | | - 2 |
295 | | - 3 |
296 | | - </pre> |
297 | | - </datafile> |
| 289 | + |
298 | 290 | <program xml:id="file-try-catch-python" language="python" add-files = "my-file-8-4-2"> |
299 | 291 | try: |
300 | 292 | with open("myfile.txt", "w") as my_writer: |
|
325 | 317 | <p> |
326 | 318 | And that's it! We will add our code to the foundational code for a complete program. First, an example of equivalent Python code: |
327 | 319 | </p> |
328 | | - <datafile label="my-file-8-4-4" filename="my-file-8-4-4" xml:id= "my-file-8-4-4" editable="yes"> |
| 320 | + <datafile label="my-file-8-4-4" filename="myfile.txt" xml:id= "my-file-8-4-4" editable="yes"> |
329 | 321 | <pre> |
330 | 322 |
|
331 | 323 | </pre> |
|
359 | 351 | public class WriteFile { |
360 | 352 | public static void main(String[] args) { |
361 | 353 | try { |
362 | | - FileWriter myWriter = new FileWriter("newfile.txt"); |
| 354 | + FileWriter myWriter = new FileWriter("myfile.txt"); |
363 | 355 | myWriter.write("File successfully updated!"); |
364 | 356 | myWriter.close(); |
365 | 357 | System.out.println("File successfully written to."); |
|
389 | 381 | <p> |
390 | 382 | Now, when we use <c>write()</c> method like before, the text will be appended if there is already tSext in the document. If we were to update our code to include the <c>boolean</c> argument: |
391 | 383 | </p> |
392 | | - <datafile label="my-file-8-4-6" filename="my-file-8-4-6" xml:id= "my-file-8-4-6" editable="yes"> |
| 384 | + <datafile label="my-file-8-4-6" filename="myfile" xml:id= "my-file-8-4-6" editable="yes"> |
393 | 385 | <pre> |
394 | 386 |
|
395 | 387 | </pre> |
|
402 | 394 | public class WriteFile { |
403 | 395 | public static void main(String[] args) { |
404 | 396 | try { |
405 | | - FileWriter myWriter = new FileWriter("newfile.txt", true); // true enables append mode |
| 397 | + FileWriter myWriter = new FileWriter("myfile.txt", true); // true enables append mode |
406 | 398 | myWriter.write("File successfully updated!"); |
407 | 399 | myWriter.close(); |
408 | 400 | System.out.println("File successfully written to."); |
|
0 commit comments