|
15 | 15 | <title>Class Imports</title> |
16 | 16 |
|
17 | 17 | <p> |
18 | | - Java has several libraries included for file handling, though, they must be imported. Java includes a class called File in the io library. The class can be imported with the following line. Be sure to capitalize File. |
| 18 | + Java has several libraries included for file handling, though, they must be imported. 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>. |
19 | 19 | </p> |
20 | 20 |
|
21 | 21 | <program interactive="activecode" language="java"> |
|
25 | 25 | </program> |
26 | 26 |
|
27 | 27 | <p> |
28 | | - The Scanner class from the util library will need to be imported if there is any need for a program to read a file. It should be noted that this library is unnecessary if the program will not be reading any data from a file. |
| 28 | + The <c>Scanner</c> class from the <c>util</c> library will need to be imported if there is any need for a program to read a file. It should be noted that this library is unnecessary if the program will not be reading any data from a file. |
29 | 29 | </p> |
30 | 30 |
|
31 | 31 | <program interactive="activecode" language="java"> |
|
35 | 35 | </program> |
36 | 36 |
|
37 | 37 | <p> |
38 | | - The FileWriter class can be used to write to files. In the same way that the Scanner class isn't needed unless the program will read from a file, the FileWriter class isn't needed unless the program will write to a file. |
| 38 | + The <c>FileWriter</c> class can be used to write to files. In the same way that the <c>Scanner</c> class isn't needed unless the program will read from a file, the <c>FileWriter</c> class isn't needed unless the program will write to a file. |
39 | 39 | </p> |
40 | 40 |
|
41 | 41 | <program interactive="activecode" language="java"> |
|
45 | 45 | </program> |
46 | 46 |
|
47 | 47 | <p> |
48 | | - Finally, these last two classes provide error handling and must be used in tandem with the File class when reading from or writing to files. IOException handles file creation and writing errors, while FileNotFoundException handles errors when trying to read files. |
| 48 | + Finally, these last two classes provide error handling and must be used in tandem with the <c>File</c> class when reading from or writing to files. <c>IOException</c> handles file creation and writing errors, while <c>FileNotFoundException</c> handles errors when trying to read files. |
49 | 49 | </p> |
50 | 50 |
|
51 | 51 | <program interactive="activecode" language="java"> |
|
66 | 66 | <title>Creating Files</title> |
67 | 67 |
|
68 | 68 | <p> |
69 | | - Before we can write code that creates a file, we must first import the necessary classes mentioned in the previous section (Scanner and FileWriter are not needed for a class that only creates files) and create a class. We will call this class CreateFile. |
| 69 | + Before we can write code that creates a file, we must first import the necessary classes mentioned in the previous section (<c>Scanner</c> and <c>FileWriter</c> are not needed for a class that only creates files) and create a class. We will call this class <c>CreateFile</c>. |
70 | 70 | </p> |
71 | 71 |
|
72 | 72 | <program interactive="activecode" language="java"> |
|
83 | 83 | </program> |
84 | 84 |
|
85 | 85 | <p> |
86 | | - Next, within the main function, we will create a File object. It is important to create a meaningful name for the File object. We will call ours <c>myFile</c>. |
| 86 | + Next, within the main function, we will 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>. |
87 | 87 | </p> |
88 | 88 |
|
89 | 89 | <program interactive="activecode" language="java"> |
|
94 | 94 |
|
95 | 95 | <note> |
96 | 96 | <p> |
97 | | - myFile is the name of the object within the program, while "myfile.txt" is the name of the file itself and will be the file name if the operation that creates the file is successful. |
| 97 | + <c>myFile</c> is the name of the object within the program, while <c>myfile.txt</c> is the name of the file itself and will be the file name if the operation that creates the file is successful. |
98 | 98 | </p> |
99 | 99 | </note> |
100 | 100 |
|
101 | 101 | <p> |
102 | | - Now that we have created a new File object, we can create a file using the <c>createNewFile()</c> method from the File 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. |
| 102 | + 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. |
103 | 103 | </p> |
104 | 104 |
|
105 | 105 | <p> |
106 | 106 | First, lets look at the equivalent Python code: |
107 | 107 | </p> |
108 | 108 |
|
109 | | - <program language="python"> |
| 109 | + <program interactive="activecode" language="python"> |
110 | 110 | <code> |
111 | 111 | import os |
112 | 112 |
|
|
149 | 149 | </note> |
150 | 150 |
|
151 | 151 | <p> |
152 | | - The code may seem complete at this point, but if you remember from the previous section, error handling using the IOException is required for program to compile. Let's utilize best practices and add in try/catch blocks to handle exceptions thrown by the IOException class. |
| 152 | + The code may seem complete at this point, but if you remember from the previous section, error handling using the <c>IOException</c> is required for program to compile. Let's utilize best practices and add in try/catch blocks to handle exceptions thrown by the <c>IOException</c> class. |
153 | 153 | </p> |
154 | 154 |
|
155 | 155 | <program interactive="activecode" language="java"> |
|
169 | 169 |
|
170 | 170 | <note> |
171 | 171 | <p> |
172 | | - The <c>IOException e</c> part in the parenthesis next to the catch. This creates a variable called <c>e</c> that refers to an IOException object. In other words, <c>e</c> refers to the error created if the try block fails. The line <c>e.printStackTrace();</c> prints the stack trace to the console. This is what the console may output if the program tries to create a file, but is blocked by the Operating System due to insufficient permissions: |
| 172 | + The <c>IOException e</c> part in the parenthesis next to the catch. This creates a variable called <c>e</c> that refers to an IOException object. In other words, <c>e</c> refers to the error created if the try block fails. The line <c>e.printStackTrace();</c> prints the stack trace to the console. This is what the console may output if the program tries to create a file, but is blocked by the operating system due to insufficient permissions: |
173 | 173 | </p> |
174 | 174 | </note> |
175 | 175 |
|
|
191 | 191 | First, the equivalent Python code: |
192 | 192 | </p> |
193 | 193 |
|
194 | | - <program language="python"> |
| 194 | + <program interactive="activecode" language="python"> |
195 | 195 | <code> |
196 | 196 | import os |
197 | 197 |
|
|
240 | 240 | </program> |
241 | 241 |
|
242 | 242 | <p> |
243 | | - You may be wondering: "What if I don't want to create a file in the current working directory?" Good question! In Windows environments, you can specify the file path using two back slashes for each back slash in the file path. For each pair of back slashes, the first backslash acts as an escape character. So, if you want to save a file to this directory: |
| 243 | + You may be wondering: "What if I don't want to create a file in the current working directory?" Good question! In Windows environments, you can specify the file path using two back slashes for each back slash in the file path. For each pair of back slashes, the first back slash acts as an escape character. So, if you want to save a file to this directory: |
244 | 244 | </p> |
245 | 245 |
|
246 | 246 | <pre> |
|
272 | 272 | <title>Writing to Files</title> |
273 | 273 |
|
274 | 274 | <p> |
275 | | - The <c>createNewFile()</c> method is useful for attempting to create files and reporting if the operation was successful, however, <c>createNewFile()</c> does not write anything to files it creates. In fact, if you use <c>createNewFile()</c> to create a .txt file and then open the file, the file will be blank. |
| 275 | + The <c>createNewFile()</c> method is useful for attempting to create files and reporting if the operation was successful, however, <c>createNewFile()</c> does not write anything to files it creates. In fact, if you use <c>createNewFile()</c> to create a <c>.txt</c> file and then open the file, the file will be blank. |
276 | 276 | </p> |
277 | 277 |
|
278 | 278 | <p> |
279 | | - To write to a file, we will need to create a different class. We will do the same setup as the previous section. First, we will import the classes (File and Scanner are not needed) and create the framework for a class that will write to a file. Let's call this class WriteFile: |
| 279 | + To write to a file, we will need to create a different class. We will do the same setup as the previous section. First, we will import the classes (<c>File</c> and <c>Scanner</c> are not needed) and create the framework for a class that will write to a file. Let's call this class <c>WriteFile</c>: |
280 | 280 | </p> |
281 | 281 |
|
282 | 282 | <program interactive="activecode" language="java"> |
|
293 | 293 | </program> |
294 | 294 |
|
295 | 295 | <p> |
296 | | - Next, we will create a FileWriter object. Let's call it myWriter: |
| 296 | + Next, we will create a <c>FileWriter</c> object. Let's call it <c>myWriter</c>: |
297 | 297 | </p> |
298 | 298 |
|
299 | 299 | <program interactive="activecode" language="java"> |
|
303 | 303 | </program> |
304 | 304 |
|
305 | 305 | <p> |
306 | | - 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 write() method takes most standard data types: |
| 306 | + 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: |
307 | 307 | </p> |
308 | 308 |
|
309 | 309 | <program interactive="activecode" language="java"> |
|
320 | 320 | </note> |
321 | 321 |
|
322 | 322 | <p> |
323 | | - Next, we will again add the required try/catch blocks utilizing the IOException 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: |
| 323 | + 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: |
324 | 324 | </p> |
325 | 325 |
|
326 | | - <program language="python"> |
| 326 | + <program interactive="activecode" language="python"> |
327 | 327 | <code> |
328 | 328 | try: |
329 | 329 | with open("myfile.txt", "w") as my_writer: |
|
358 | 358 | And that's it! We will add our code to the foundational code for a complete program. First, an example of equivalent Python code: |
359 | 359 | </p> |
360 | 360 |
|
361 | | - <program language="python"> |
| 361 | + <program interactive="activecode" language="python"> |
362 | 362 | <code> |
363 | 363 | try: |
364 | 364 | with open("myfile.txt", "w") as my_writer: |
|
402 | 402 |
|
403 | 403 | <note> |
404 | 404 | <p> |
405 | | - If a file does not already exist (for example, myfile.txt does not exist), the <c>write()</c> method will create the file. Despite this, it is still a good idea to create separate methods or classes for creating and writing to files. Not only is it good practice to ensure methods only accomplish one thing, but the <c>createNewFile()</c> method avoids overwriting files that already exist. Imagine a file with the name myfile.txt already exists and contains important information. Attempting to create a file using the <c>write()</c> method will delete that data forever. |
| 405 | + If a file does not already exist (for example, <c>myfile.txt</c> does not exist), the <c>write()</c> method will create the file. Despite this, it is still a good idea to create separate methods or classes for creating and writing to files. Not only is it good practice to ensure methods only accomplish one thing, but the <c>createNewFile()</c> method avoids overwriting files that already exist. Imagine a file with the name <c>myfile.txt</c> already exists and contains important information. Attempting to create a file using the <c>write()</c> method will delete that data forever. |
406 | 406 | </p> |
407 | 407 | </note> |
408 | 408 |
|
409 | 409 | <p> |
410 | | - Speaking of overwriting data, what if we want to append text to the end of any text already in myfile.txt? To accomplish this, we can pass a boolean argument along with the file name when creating a new data argument: |
| 410 | + Speaking of overwriting data, what if we want to append text to the end of any text already in <c>myfile.txt</c>? To accomplish this, we can pass a boolean argument along with the file name when creating a new data argument: |
411 | 411 | </p> |
412 | 412 |
|
413 | 413 | <program interactive="activecode" language="java"> |
|
442 | 442 | </program> |
443 | 443 |
|
444 | 444 | <p> |
445 | | - and then run the program twice, the contents of myfile.txt would be: |
| 445 | + and then run the program twice, the contents of <c>myfile.txt</c> would be: |
446 | 446 | </p> |
447 | 447 |
|
448 | 448 | <program language="$"> |
|
538 | 538 | The next lines consist of a while loop that reads each line of the file passed to the Scanner object and reads them. First, a Python code example. A for loop is used instead in the Python example: |
539 | 539 | </p> |
540 | 540 |
|
541 | | - <program language="python"> |
| 541 | + <program interactive="activecode" language="python"> |
542 | 542 | <code> |
543 | 543 | with open("filename.txt", "r") as file_reader: |
544 | 544 | for line in file_reader: |
|
589 | 589 | 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: |
590 | 590 | </p> |
591 | 591 |
|
592 | | - <program language="python"> |
| 592 | + <program interactive="activecode" language="python"> |
593 | 593 | <code> |
594 | 594 | try: |
595 | 595 | with open("myfile.txt", "r") as file_reader: |
|
0 commit comments