Skip to content

Commit c55c7be

Browse files
committed
Removed the paragraph on saving to a specific file path. Made some changes to the code in 8.4 so that the code is more in-line with what the text is describing for each one.
1 parent 0e6bf21 commit c55c7be

1 file changed

Lines changed: 26 additions & 49 deletions

File tree

source/ch8_filehandling.ptx

Lines changed: 26 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@
195195
</program>
196196

197197
<p>
198-
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.
198+
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.
199199
</p>
200200

201201
<program xml:id="file-read-java" interactive="activecode" language="java" add-files= "myfile-read">
@@ -235,34 +235,17 @@
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-
<program xml:id="file-write-exp1" interactive="activecode" language="java">
239-
<code>
238+
<pre>
240239
import java.io.File;
241240
import java.io.FileWriter;
242241
import java.io.IOException;
243242
import java.util.Scanner;
244243
public class WriteFile {
245244
public static void main(String[] args) {
246-
String filename = "test_file.txt";
247-
try (FileWriter writer = new FileWriter(filename)) {
248-
writer.write("This line was written by the program.");
249-
System.out.println("Successfully wrote to the file.");
250-
}
251-
catch (IOException e) {
252-
System.out.println("An error occurred during writing.");
253-
} System.out.println("--- Reading file back ---");
254-
try (Scanner reader = new Scanner(new File(filename))) {
255-
while (reader.hasNextLine()) {
256-
System.out.println(reader.nextLine());
257-
}
258-
}
259-
catch (IOException e) {
260-
System.out.println("An error occurred during reading.");
261-
}
245+
262246
}
263247
}
264-
</code>
265-
</program>
248+
</pre>
266249

267250
<p>
268251
Next, we will create a <c>FileWriter</c> object. Let's call it <c>myWriter</c>:
@@ -299,42 +282,36 @@
299282
</datafile>
300283
<program xml:id="file-write-exp2" interactive="activecode" language="python" add-files = "my-file-8-4-2">
301284
<code>
302-
with open("myfile8-4-2.txt", "r") as file_reader:
303-
while True:
304-
line = file_reader.readline()
305-
if not line: # End of file
306-
break
307-
print(line.strip())
285+
try:
286+
with open("myfile.txt", "w") as my_writer:
287+
my_writer.write("File successfully updated!")
288+
print("File successfully written to.")
289+
except OSError as e:
290+
print("An error occurred.")
291+
import traceback
292+
traceback.print_exc()
308293
</code>
309294
</program>
310295

311296
<p>
312297
And the equivalent Java code:
313298
</p>
314299
<datafile label="my-file-8-4-3" filename="myfile8-4-3.txt" xml:id= "my-file-8-4-3" editable="yes">
315-
<pre>
316-
317-
</pre>
318-
</datafile>
319-
<program xml:id="file-write-exp3" interactive="activecode" language="java" add-files = "my-file-8-4-3">
320-
<code>
321-
import java.io.File;
322-
import java.io.IOException;
323-
import java.util.Scanner;public class WriteFile {
324-
public static void main(String[] args) {
325-
String filename = "myfile8-4-3.txt";
326-
try (Scanner reader = new Scanner(new File(filename))) {
327-
while (reader.hasNextLine()) {
328-
String line = reader.nextLine();
329-
System.out.println(line.trim());
330-
}
331-
} catch (IOException e) {
332-
System.out.println("An error occurred.");
333-
}
334-
}
300+
<pre>
301+
302+
</pre>
303+
</datafile>
304+
<pre>
305+
try {
306+
FileWriter myWriter = new FileWriter("myfile.txt");
307+
myWriter.write("File successfully updated!");
308+
myWriter.close();
309+
System.out.println("File successfully written to.");
310+
} catch (IOException e) {
311+
System.out.println("An error occurred.");
312+
e.printStackTrace();
335313
}
336-
</code>
337-
</program>
314+
</pre>
338315

339316
<p>
340317
And that's it! We will add our code to the foundational code for a complete program. First, an example of equivalent Python code:

0 commit comments

Comments
 (0)