Skip to content

Commit 2769ba1

Browse files
committed
Final commit for now that fixes several issues in the issues queue (listed in PR). The chapter is in a state that could be usable in a class now, though, I believe there is still work to do. 8.2 and 8.4 take different approaches to presenting the content. I'm not sure which one would work better, but I think it would be a good idea to settle on one or the other at some point and rewrite one of these sections to match the presentation of the other. Additionally, I couldn't get the data files to work in the section on writing to files. Not sure what the problem was here. Finally, I was unable to test Java code in the book. I worry that the two Java code blocks in the section on deleting files will need to be combined into one code block for the deletion to work correctly.
1 parent d02e194 commit 2769ba1

1 file changed

Lines changed: 11 additions & 19 deletions

File tree

source/ch8_filehandling.ptx

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,6 @@
3434
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.
3535
</p>
3636

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-
4137
<p>
4238
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.
4339
</p>
@@ -85,7 +81,9 @@
8581
import java.io.File;
8682
public class CreateFile {
8783
public static void main(String[] args) {
88-
84+
import java.io.File;
85+
File myFile = new File("myfile.txt");
86+
System.out.println(myFile);
8987
}
9088
}
9189
</input>
@@ -99,7 +97,7 @@
9997

10098

10199
<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.
103101
</p>
104102

105103
<p>
@@ -263,7 +261,7 @@
263261
</program>
264262

265263
<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:
267265
</p>
268266

269267
<program xml:id="write-and-close-python" language="python">
@@ -281,20 +279,14 @@
281279

282280
<note>
283281
<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!
285283
</p>
286284
</note>
287285

288286
<p>
289287
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:
290288
</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+
298290
<program xml:id="file-try-catch-python" language="python" add-files = "my-file-8-4-2">
299291
try:
300292
with open("myfile.txt", "w") as my_writer:
@@ -325,7 +317,7 @@
325317
<p>
326318
And that's it! We will add our code to the foundational code for a complete program. First, an example of equivalent Python code:
327319
</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">
329321
<pre>
330322

331323
</pre>
@@ -359,7 +351,7 @@
359351
public class WriteFile {
360352
public static void main(String[] args) {
361353
try {
362-
FileWriter myWriter = new FileWriter("newfile.txt");
354+
FileWriter myWriter = new FileWriter("myfile.txt");
363355
myWriter.write("File successfully updated!");
364356
myWriter.close();
365357
System.out.println("File successfully written to.");
@@ -389,7 +381,7 @@
389381
<p>
390382
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:
391383
</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">
393385
<pre>
394386

395387
</pre>
@@ -402,7 +394,7 @@
402394
public class WriteFile {
403395
public static void main(String[] args) {
404396
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
406398
myWriter.write("File successfully updated!");
407399
myWriter.close();
408400
System.out.println("File successfully written to.");

0 commit comments

Comments
 (0)