Skip to content

Commit 680f110

Browse files
committed
Exchanged pre tags with code tags for all code snippets.
1 parent ec29666 commit 680f110

1 file changed

Lines changed: 54 additions & 24 deletions

File tree

source/ch8_filehandling.ptx

Lines changed: 54 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -42,33 +42,33 @@
4242
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.
4343
</p>
4444

45-
<pre>
45+
<program language="java">
4646
import java.io.File;
47-
</pre>
47+
</program>
4848
<p>
4949
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.
5050
</p>
5151

52-
<pre>
52+
<program language="java">
5353
import java.util.Scanner;
54-
</pre>
54+
</program>
5555

5656
<p>
5757
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.
5858
</p>
5959

60-
<pre>
60+
<program language="java">
6161
import java.io.FileWriter;
62-
</pre>
62+
</program>
6363

6464
<p>
6565
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.
6666
</p>
6767

68-
<pre>
68+
<program language="java">
6969
import java.io.IOException;
7070
import java.io.FileNotFoundException;
71-
</pre>
71+
</program>
7272

7373
</section>
7474

@@ -235,7 +235,7 @@
235235
Let us create the framework for a class that will write to a file. Let's call this class <c>WriteFile</c>:
236236
</p>
237237

238-
<pre>
238+
<program language="java">
239239
import java.io.File;
240240
import java.io.FileWriter;
241241
import java.io.IOException;
@@ -245,24 +245,24 @@
245245

246246
}
247247
}
248-
</pre>
248+
</program>
249249

250250
<p>
251251
Next, we will create a <c>FileWriter</c> object. Let's call it <c>myWriter</c>:
252252
</p>
253253

254-
<pre>
254+
<program language="java">
255255
FileWriter myWriter = new FileWriter("myfile.txt");
256-
</pre>
256+
</program>
257257

258258
<p>
259259
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:
260260
</p>
261261

262-
<pre>
262+
<program language="java">
263263
myWriter.write("File successfully updated!");
264264
myWriter.close();
265-
</pre>
265+
</program>
266266

267267
<note>
268268
<p>
@@ -301,7 +301,7 @@
301301

302302
</pre>
303303
</datafile>
304-
<pre>
304+
<program language="java">
305305
try {
306306
FileWriter myWriter = new FileWriter("myfile.txt");
307307
myWriter.write("File successfully updated!");
@@ -311,7 +311,7 @@
311311
System.out.println("An error occurred.");
312312
e.printStackTrace();
313313
}
314-
</pre>
314+
</program>
315315

316316
<p>
317317
And that's it! We will add our code to the foundational code for a complete program. First, an example of equivalent Python code:
@@ -373,17 +373,17 @@
373373
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 <c>boolean</c> argument along with the file name when creating a new data argument:
374374
</p>
375375

376-
<pre>
376+
<program language="java">
377377
FileWriter myWriter = new FileWriter("myfile.txt", true); // true enables append mode
378-
</pre>
378+
</program>
379379

380380
<p>
381381
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:
382382
</p>
383383
<datafile label="my-file-8-4-6" filename="myfile.txt" xml:id= "my-file-8-4-6" editable="yes">
384-
<pre>
384+
<program language="java">
385385

386-
</pre>
386+
</program>
387387
</datafile>
388388
<program xml:id="file-write-exp6" interactive="activecode" language="java" add-files = "my-file-8-4-6">
389389
<code>
@@ -410,18 +410,18 @@
410410
Then if we run the program twice, the contents of <c>myfile.txt</c> would be:
411411
</p>
412412

413-
<pre>
413+
<program language="java">
414414
File successfully updated!File successfully updated!
415-
</pre>
415+
</program>
416416

417417
<p>
418418
This doesn't look very good! If we want each additional write to appear on a new line? A simple solution is to use the <c>\n</c> newline character:
419419
</p>
420420

421-
<pre>
421+
<program language="java">
422422
myWriter.write("File successfully updated!\n"); // Added newline character
423423
myWriter.close();
424-
</pre>
424+
</program>
425425

426426
<p>
427427
Running the code with the newline character twice will result in the following contents in myfile.txt:
@@ -439,6 +439,36 @@
439439
<p>
440440
Finally, we will take a look at using Java to delete files. This one is pretty straight-forward and follows the structure used to create files. This time, however, try/catch blocks are not needed for the program to compile. We will call this class DeleteFile. The completed code should look something like this.
441441
</p>
442+
443+
<datafile label="myfile-delete" filename="myfile.txt" xml:id= "myfile-delete" editable="no">
444+
<pre>
445+
1
446+
2
447+
3
448+
4
449+
5
450+
6
451+
7
452+
8
453+
</pre>
454+
</datafile>
455+
456+
<program interactive="activecode" language="python">
457+
<input>
458+
# Name of the file to delete
459+
file_name = "myfile.txt"
460+
461+
# Check if the file exists before deleting
462+
if os.path.exists(file_name):
463+
try:
464+
os.remove(file_name)
465+
print("Deleted", file_name)
466+
except Exception as e:
467+
print("File could not be deleted.")
468+
else:
469+
print("File could not be deleted.")
470+
</input>
471+
</program>
442472

443473
<program interactive="activecode" language="java">
444474
<code>

0 commit comments

Comments
 (0)