Skip to content

Commit c090273

Browse files
committed
fixed remaining issues after testing, and removed sourcecode for data files.
1 parent 370f9d8 commit c090273

1 file changed

Lines changed: 22 additions & 19 deletions

File tree

source/ch8_filehandling.ptx

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@
118118
</note>
119119

120120
<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.
122122
</p>
123123

124124
<p>
@@ -142,22 +142,25 @@
142142
<p>
143143
Now, let's look at Java code that accomplishes the same task:
144144
</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">
151147
<code>
152148
import java.io.File;
153149
import java.io.IOException;
154150

155151
public class CreateFile {
156152
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
161164
}
162165
}
163166
}
@@ -220,7 +223,7 @@
220223
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:
221224
</p>
222225

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">
224227
<code>
225228
import java.io.File;
226229
import java.io.FileNotFoundException;
@@ -329,7 +332,7 @@
329332
for line in file_reader:
330333
data += line # line already includes the newline character
331334
print(data)
332-
except FileNotFoundError as e:
335+
except FileNotFoundError:
333336
print("An error occurred.")
334337
import traceback
335338
traceback.print_exc()
@@ -341,7 +344,7 @@
341344
And the Java equivalent:
342345
</p>
343346

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">
345348
<code>
346349
import java.io.File;
347350
import java.io.FileNotFoundException;
@@ -437,7 +440,7 @@
437440
<p>
438441
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:
439442
</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">
441444
<pre>
442445
1
443446
2
@@ -458,7 +461,7 @@
458461
<p>
459462
And the equivalent Java code:
460463
</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">
462465
<pre>
463466

464467
</pre>
@@ -486,7 +489,7 @@
486489
<p>
487490
And that's it! We will add our code to the foundational code for a complete program. First, an example of equivalent Python code:
488491
</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">
490493
<pre>
491494

492495
</pre>
@@ -507,7 +510,7 @@
507510
<p>
508511
The completed Java code:
509512
</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">
511514
<pre>
512515

513516
</pre>
@@ -554,7 +557,7 @@
554557
<p>
555558
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:
556559
</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">
558561
<pre>
559562

560563
</pre>

0 commit comments

Comments
 (0)