From ffaf7dda5f5408bcc44f6b2211e65cfdc77b0ec1 Mon Sep 17 00:00:00 2001
From: Habiba Sorour
Date: Tue, 4 Aug 2026 20:23:36 +0300
Subject: [PATCH 1/7] added listing tags
---
source/ch8_filehandling.ptx | 63 +++++++++++++++++++++++++++----------
1 file changed, 47 insertions(+), 16 deletions(-)
diff --git a/source/ch8_filehandling.ptx b/source/ch8_filehandling.ptx
index aa86bf3..1117858 100644
--- a/source/ch8_filehandling.ptx
+++ b/source/ch8_filehandling.ptx
@@ -242,8 +242,9 @@ public class CreateFile {
Let us create the framework for a class that will write to a file. Let's call this class WriteFile:
-
-
+
+
+
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
@@ -254,39 +255,46 @@ public class CreateFile {
}
}
+
Next, we will create a FileWriter object. Let's call it myWriter. The equivalent Python code to this operation is:
-
-
+
+
with open("myfile.txt", "w") as myWriter:
+
The Java code to create a FileWriter object is:
-
+
+
FileWriter myWriter = new FileWriter("myfile.txt");
+
In this next step, we will use the write() method from the FileWriter class. This method will take any data within the parenthesis and write that data to the file selected. The write() method takes most standard data types. First, how this step is completed with Python:
-
-
+
+
my_writer.write("File successfully updated!")
+
And the Java equivalent. This is almost completely identical except for the second line, which is very important!
-
+
+
myWriter.write("File successfully updated!");
myWriter.close();
+
@@ -298,7 +306,8 @@ public class CreateFile {
Next, we will again add the required try/catch blocks utilizing the IOException 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:
-
+
+
try:
with open("myfile.txt", "w") as my_writer:
my_writer.write("File successfully updated!")
@@ -308,12 +317,14 @@ public class CreateFile {
import traceback
traceback.print_exc()
+
And the equivalent Java code:
-
+
+
try {
FileWriter myWriter = new FileWriter("myfile.txt");
myWriter.write("File successfully updated!");
@@ -324,16 +335,22 @@ public class CreateFile {
e.printStackTrace();
}
+
And that's it! We will add our code to the foundational code for a complete program. First, an example of equivalent Python code:
+
+ Data file for writing example
-
+
+
+
+
try:
with open("myfile.txt", "w") as my_writer:
@@ -345,10 +362,13 @@ public class CreateFile {
traceback.print_exc()
+
The completed Java code:
+
+ Data file for writing example
@@ -374,6 +394,7 @@ public class CreateFile {
}
+
@@ -385,19 +406,26 @@ public class CreateFile {
Speaking of overwriting data, what if we want to append text to the end of any text already in myfile.txt? To accomplish this, we can pass a boolean argument along with the file name when creating a new data argument:
-
+
+
FileWriter myWriter = new FileWriter("myfile.txt", true); // true enables append mode
+
Now, when we use write() 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 boolean argument:
-
+
+ Data file for writing example
+
-
+
+
+
+
import java.io.FileWriter;
import java.io.IOException;
@@ -417,6 +445,7 @@ public class CreateFile {
}
+
Then if we run the program twice, the contents of myfile.txt would be:
@@ -430,15 +459,17 @@ public class CreateFile {
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 \n newline character:
-
+
+
myWriter.write("File successfully updated!\n"); // Added newline character
myWriter.close();
+
Running the code with the newline character twice will result in the following contents in myfile.txt:
-
+
File successfully updated!
File successfully updated!
From 54e5c7153b625f04052079a4cdfac70d0135a558 Mon Sep 17 00:00:00 2001
From: Habiba Sorour
Date: Tue, 4 Aug 2026 20:25:06 +0300
Subject: [PATCH 2/7] fixed pre tags, to add linking
---
source/ch8_filehandling.ptx | 15 ++++++++++-----
1 file changed, 10 insertions(+), 5 deletions(-)
diff --git a/source/ch8_filehandling.ptx b/source/ch8_filehandling.ptx
index 1117858..8cfca92 100644
--- a/source/ch8_filehandling.ptx
+++ b/source/ch8_filehandling.ptx
@@ -451,9 +451,11 @@ public class CreateFile {
Then if we run the program twice, the contents of myfile.txt would be:
-
+
+
File successfully updated!File successfully updated!
-
+
+
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 \n newline character:
@@ -469,11 +471,14 @@ public class CreateFile {
Running the code with the newline character twice will result in the following contents in myfile.txt:
-
-
+
+
+
File successfully updated!
File successfully updated!
-
+
+
+
From c0fb145c036c81c6005e5761c23a5aaa87e61c86 Mon Sep 17 00:00:00 2001
From: Habiba Sorour
Date: Tue, 4 Aug 2026 21:04:24 +0300
Subject: [PATCH 3/7] added listing to text
---
source/ch8_filehandling.ptx | 30 +++++++++++++++---------------
1 file changed, 15 insertions(+), 15 deletions(-)
diff --git a/source/ch8_filehandling.ptx b/source/ch8_filehandling.ptx
index 8cfca92..32d2b8b 100644
--- a/source/ch8_filehandling.ptx
+++ b/source/ch8_filehandling.ptx
@@ -240,7 +240,7 @@ public class CreateFile {
- Let us create the framework for a class that will write to a file. Let's call this class WriteFile:
+ shows the framework for a class that will write to a file. Let's call this class WriteFile.
@@ -258,7 +258,7 @@ public class CreateFile {
- Next, we will create a FileWriter object. Let's call it myWriter. The equivalent Python code to this operation is:
+ Next, we will create a FileWriter object. Let's call it myWriter. shows the code to create a myWriter object in Python.
@@ -267,7 +267,7 @@ public class CreateFile {
- The Java code to create a FileWriter object is:
+ shows the Java code to create a FileWriter object. Note that the FileWriter object is created with the name of the file to write to as an argument. If the file does not exist, it will be created. If it does exist, it will be overwritten.:
@@ -277,7 +277,7 @@ public class CreateFile {
- In this next step, we will use the write() method from the FileWriter class. This method will take any data within the parenthesis and write that data to the file selected. The write() method takes most standard data types. First, how this step is completed with Python:
+ In this next step, we will use the write() method from the FileWriter class. This method will take any data within the parenthesis and write that data to the file selected. The write() method takes most standard data types. The shows the code to write to a file in Python.
@@ -286,7 +286,7 @@ public class CreateFile {
- And the Java equivalent. This is almost completely identical except for the second line, which is very important!
+ shows the Java equivalent. This is almost completely identical except for the second line, which is very important!
@@ -303,7 +303,7 @@ public class CreateFile {
- Next, we will again add the required try/catch blocks utilizing the IOException 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:
+ Next, we will again add the required try/catch blocks utilizing the IOException 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. shows the code to write to a file in Python.
@@ -320,7 +320,7 @@ public class CreateFile {
- And the equivalent Java code:
+ shows the Java equivalent.
@@ -338,7 +338,7 @@ public class CreateFile {
- And that's it! We will add our code to the foundational code for a complete program. First, an example of equivalent Python code:
+ And that's it! We will add our code to the foundational code for a complete program. First, shows the completed Python code.
Data file for writing example
@@ -365,7 +365,7 @@ public class CreateFile {
- The completed Java code:
+ shows the completed Java code.
Data file for writing example
@@ -403,7 +403,7 @@ public class CreateFile {
- Speaking of overwriting data, what if we want to append text to the end of any text already in myfile.txt? To accomplish this, we can pass a boolean argument along with the file name when creating a new data argument:
+ Speaking of overwriting data, what if we want to append text to the end of any text already in myfile.txt? To accomplish this, we can pass a boolean argument along with the file name when creating a new data argument as shown in .
@@ -413,8 +413,8 @@ public class CreateFile {
- Now, when we use write() 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 boolean argument:
-
+ Now, when we use write() 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 boolean argument as shown in .
+
Data file for writing example
@@ -448,7 +448,7 @@ public class CreateFile {
- Then if we run the program twice, the contents of myfile.txt would be:
+ Then if we run the program twice, the contents of myfile.txt would be as shows.
@@ -458,7 +458,7 @@ public class CreateFile {
- 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 \n newline character:
+ 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 \n newline character as shows.
@@ -469,7 +469,7 @@ public class CreateFile {
- Running the code with the newline character twice will result in the following contents in myfile.txt:
+ Running the code with the newline character twice will result in the following contents in myfile.txt as shows.
From 79437de7b191274c043e954591161bbac52ef31d Mon Sep 17 00:00:00 2001
From: Habiba Sorour
Date: Tue, 4 Aug 2026 21:18:19 +0300
Subject: [PATCH 4/7] fixed xml:id tag
---
source/ch8_filehandling.ptx | 18 +++++++++---------
1 file changed, 9 insertions(+), 9 deletions(-)
diff --git a/source/ch8_filehandling.ptx b/source/ch8_filehandling.ptx
index 32d2b8b..167c6d3 100644
--- a/source/ch8_filehandling.ptx
+++ b/source/ch8_filehandling.ptx
@@ -340,9 +340,9 @@ public class CreateFile {
And that's it! We will add our code to the foundational code for a complete program. First, shows the completed Python code.
-
+
Data file for writing example
-
+
@@ -350,7 +350,7 @@ public class CreateFile {
-
+
try:
with open("myfile.txt", "w") as my_writer:
@@ -367,14 +367,14 @@ public class CreateFile {
shows the completed Java code.
-
+
Data file for writing example
-
+
-
+
import java.io.FileWriter;
import java.io.IOException;
@@ -415,9 +415,9 @@ public class CreateFile {
Now, when we use write() 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 boolean argument as shown in .
-
+
Data file for writing example
-
+
@@ -425,7 +425,7 @@ public class CreateFile {
-
+
import java.io.FileWriter;
import java.io.IOException;
From 47d278a30309ab553dd9bb9a8e394e58cef11ab0 Mon Sep 17 00:00:00 2001
From: Habiba Sorour
Date: Tue, 4 Aug 2026 21:19:08 +0300
Subject: [PATCH 5/7] added a missing listing
---
source/ch8_filehandling.ptx | 8 ++++++--
1 file changed, 6 insertions(+), 2 deletions(-)
diff --git a/source/ch8_filehandling.ptx b/source/ch8_filehandling.ptx
index 167c6d3..7df8f92 100644
--- a/source/ch8_filehandling.ptx
+++ b/source/ch8_filehandling.ptx
@@ -374,7 +374,10 @@ public class CreateFile {
-
+
+
+
+
import java.io.FileWriter;
import java.io.IOException;
@@ -394,7 +397,8 @@ public class CreateFile {
}
-
+
+
From 13467a6ff31c9f698f29d44c240b3351cacd78d6 Mon Sep 17 00:00:00 2001
From: Habiba Sorour
Date: Tue, 4 Aug 2026 21:20:11 +0300
Subject: [PATCH 6/7] fixed a build error
---
source/ch8_filehandling.ptx | 1 +
1 file changed, 1 insertion(+)
diff --git a/source/ch8_filehandling.ptx b/source/ch8_filehandling.ptx
index 7df8f92..d86d6e5 100644
--- a/source/ch8_filehandling.ptx
+++ b/source/ch8_filehandling.ptx
@@ -418,6 +418,7 @@ public class CreateFile {
Now, when we use write() 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 boolean argument as shown in .
+
Data file for writing example
From b56a824b429b495b0e1a80a4ed896c6c7cf11c81 Mon Sep 17 00:00:00 2001
From: Habiba Sorour
Date: Tue, 4 Aug 2026 22:00:33 +0300
Subject: [PATCH 7/7] fixed issue with datafile block
---
source/ch8_filehandling.ptx | 17 +++++++++--------
1 file changed, 9 insertions(+), 8 deletions(-)
diff --git a/source/ch8_filehandling.ptx b/source/ch8_filehandling.ptx
index d86d6e5..eba0f10 100644
--- a/source/ch8_filehandling.ptx
+++ b/source/ch8_filehandling.ptx
@@ -340,9 +340,10 @@ public class CreateFile {
And that's it! We will add our code to the foundational code for a complete program. First, shows the completed Python code.
-
+
+
Data file for writing example
-
+
@@ -367,9 +368,9 @@ public class CreateFile {
shows the completed Java code.
-
+
Data file for writing example
-
+
@@ -395,7 +396,7 @@ public class CreateFile {
}
}
}
-
+ ~
@@ -420,9 +421,9 @@ public class CreateFile {
Now, when we use write() 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 boolean argument as shown in .
-
+
Data file for writing example
-
+
@@ -467,7 +468,7 @@ public class CreateFile {
-
+
myWriter.write("File successfully updated!\n"); // Added newline character
myWriter.close();