diff --git a/source/ch8_filehandling.ptx b/source/ch8_filehandling.ptx index aa86bf3..eba0f10 100644 --- a/source/ch8_filehandling.ptx +++ b/source/ch8_filehandling.ptx @@ -240,10 +240,11 @@ 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.

- - + + + 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: + Next, we will create a FileWriter object. Let's call it myWriter. shows the code to create a myWriter object in Python.

- - + + with open("myfile.txt", "w") as myWriter: +

- 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.:

- + + 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: + 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.

- - + + my_writer.write("File successfully updated!") +

- 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!

- + + myWriter.write("File successfully updated!"); myWriter.close(); +

@@ -295,10 +303,11 @@ 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.

- + + 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: + shows the Java equivalent.

- + + try { FileWriter myWriter = new FileWriter("myfile.txt"); myWriter.write("File successfully updated!"); @@ -324,16 +335,23 @@ 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: + 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 +
                     
                 
- +
+ + + try: with open("myfile.txt", "w") as my_writer: @@ -345,16 +363,22 @@ public class CreateFile { traceback.print_exc() +

- The completed Java code: + shows the completed Java code.

- + + Data file for writing example +
                     
                 
- +
+ + + import java.io.FileWriter; import java.io.IOException; @@ -372,8 +396,10 @@ public class CreateFile { } } } - + ~ + +

@@ -382,22 +408,30 @@ 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 .

- + + 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: + 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 +
                     
                 
- +
+ + + import java.io.FileWriter; import java.io.IOException; @@ -417,32 +451,40 @@ 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.

-
+        +        
             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: + 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.

- + + 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: + Running the code with the newline character twice will result in the following contents in myfile.txt as shows.

-
+        +        
             File successfully updated!
             File successfully updated!
-        
+
+ +