Skip to content

Commit eaa69a4

Browse files
committed
improve ch8 with clarifications
1 parent ccdbe27 commit eaa69a4

1 file changed

Lines changed: 18 additions & 18 deletions

File tree

source/ch8_filehandling.ptx

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
<?xml version="1.0"?>
22

33
<!-- Generated by Docutils 0.19 -->
4-
<chapter xml:id="filemmanipulation">
4+
<chapter xml:id="file-handling">
55
<title>File Handling</title>
66

7-
<section xml:id="file-class-import">
7+
<section xml:id="class-imports">
88
<title>Class Imports</title>
99
<p>
10-
File handling is an integral part of programming. Most programming languages have the ability to read from, write to, create, delete, move, and copy files. In Python, most built-in libraries are available without needing to explicitly import additional packages, but some libraries like <c>math</c> do need to be imported. Consider the following.
10+
File handling is an integral part of programming. Most programming languages have the ability to create, read from, write to, and delete, files. In Python, most built-in libraries are available without needing to explicitly import additional packages, but some libraries like <c>math</c> do need to be imported. Consider the following.
1111
</p>
1212
<program xml:id = "file-class-import-Python-example" interactive="activecode" language="python">
1313
<code>
@@ -17,7 +17,7 @@
1717
</program>
1818

1919
<p>
20-
The same program in Java would look like this:
20+
Delete the first line that says <c>import math</c> and see what happens. The <c>import math</c> is needed. The same program in Java would look like this:
2121
</p>
2222
<program xml:id = "file-class-import-Java-example" interactive="activecode" language="java">
2323
<code>
@@ -31,11 +31,11 @@
3131
</code>
3232
</program>
3333
<p>
34-
Note the use of <c>import java.lang.Math;</c> in the above to import the Math class. Unlike Python, Java requires explicit imports for most libraries, including the <c>Math</c> class and many different classes for file handling.
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.
3535
</p>
3636

3737
<p>
38-
Much like the Math class, in order for your program to work with files you need to import classes from libraries. Java includes a class called <c>File</c> in the <c>io</c> library. This class allows you to create File 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.
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.
3939
</p>
4040
<program xml:id = "file-class-import-io-example" interactive="activecode" language="java">
4141
<code>
@@ -82,18 +82,18 @@
8282

8383
</section>
8484

85-
<section xml:id="create-file">
85+
<section xml:id="creating-files">
8686
<title>Creating Files</title>
8787

8888
<p>
8989
We will now 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>.
9090
</p>
91-
<datafile label="my-file-8-2-1" filename="myfile.txt" xml:id= "my-file-8-2-1" editable="yes">
91+
<datafile label="myfile-created" filename="myfile.txt" xml:id= "myfile-created" editable="yes">
9292
<pre>
9393
empty file
9494
</pre>
9595
</datafile>
96-
<program xml:id="create-file-exp-1" interactive="activecode" language="java" add-files="my-file-8-2-1">
96+
<program xml:id="create-file-java" interactive="activecode" language="java" add-files="myfile-created">
9797
<code>
9898
import java.io.File;
9999
public class Main {
@@ -119,7 +119,7 @@
119119
First, lets look at the equivalent Python code:
120120
</p>
121121

122-
<program xml:id="creat-file-exp-2" interactive="activecode" language="python">
122+
<program xml:id="create-file-python" interactive="activecode" language="python">
123123
<code>
124124
filename = "newfile.txt"
125125
print("Attempting to write to '" + filename + "' using 'w' mode...")
@@ -169,16 +169,16 @@
169169
</note>
170170
</section>
171171

172-
<section xml:id="file-read">
172+
<section xml:id="reading-files">
173173
<title>Reading Files</title>
174174

175175
<p>
176-
Let’s take a look at how we can use Python to understand how read file contents in Java.In order to read files generally you iterate through each line in the file and read its content. In Java you read files in a very similar way, however in java we will use a <c>Scanner</c> object in order to iterate through lines.
176+
Let’s take a look at how we can use Python to understand how read file contents in Java. In order to read files generally you iterate through each line in the file and read the line's content. In Java, you read files in a very similar way, however in Java we will use the <c>Scanner</c> class in order to iterate through the lines.
177177
</p>
178178
<p>
179-
The next lines consists of a Python code example that reads each line of the file and prints it to the console.
179+
Consider the following Python code example that reads each line of the file and prints it to the console.
180180
</p>
181-
<datafile label="my-file-8-3" filename="myfile.txt" xml:id= "my-file-8-3" editable="no">
181+
<datafile label="myfile-read" filename="myfile.txt" xml:id= "myfile-read" editable="no">
182182
<pre>
183183
1
184184
2
@@ -191,7 +191,7 @@
191191
</pre>
192192
</datafile>
193193

194-
<program xml:id="file-read-exp1" interactive="activecode" language="python" add-files="my-file-8-3">
194+
<program xml:id="file-read-py" interactive="activecode" language="python" add-files="myfile-read">
195195
<code>
196196
filename = "myfile.txt"
197197
try:
@@ -210,7 +210,7 @@
210210
The following Java code functions very similarly to the previous Python. The main difference here is that unlike Python, In Java we use the <c>Scanner</c> object to iterate through and read lines in the file. You will notice that the structure of the Java code is still similar to the Python; Both use a try and catch statement to read the file and catch any errors.
211211
</p>
212212

213-
<program xml:id="file-read-exp2" interactive="activecode" language="java" add-files= "my-file-8-3">
213+
<program xml:id="file-read-java" interactive="activecode" language="java" add-files= "myfile-read">
214214
<code>
215215
import java.io.File;
216216
import java.io.FileNotFoundException;
@@ -231,11 +231,11 @@
231231
</code>
232232
</program>
233233
<p>
234-
You may have noticed that there are some new methods you haven't seen yet. The <c>hasNextLine()</c> method checks if there is a next line in the file, and returns false if there isn't. This method allows us to iterate over every line till there is no next line. The <c>nextLine()</c> method of the Scanner object returns the next line in the file as a string.
234+
You may have noticed that there are some new methods you haven't seen yet. The <c>hasNextLine()</c> method checks if there is a next line in the file, and returns <c>false</c> if there isn't. This method allows us to iterate over every line till there is no next line. The <c>nextLine()</c> method of the Scanner object returns the next line in the file as a string.
235235
</p>
236236
</section>
237237

238-
<section xml:id="file-write">
238+
<section xml:id="writing-to-files">
239239
<title>Writing to Files</title>
240240

241241
<p>

0 commit comments

Comments
 (0)