Skip to content

Commit 5649c7d

Browse files
authored
Merge branch 'master' into Issue-#131-Fix
2 parents 5d3dfe4 + b3310f6 commit 5649c7d

1 file changed

Lines changed: 5 additions & 31 deletions

File tree

source/ch8_filehandling.ptx

Lines changed: 5 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -84,35 +84,8 @@
8484

8585
<section xml:id="create-file">
8686
<title>Creating Files</title>
87-
8887
<p>
89-
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>.
90-
</p>
91-
<datafile label="my-file-8-2-1" filename="myfile.txt" xml:id= "my-file-8-2-1" editable="yes">
92-
<pre>
93-
empty file
94-
</pre>
95-
</datafile>
96-
<program xml:id="create-file-exp-1" interactive="activecode" language="java" add-files="my-file-8-2-1">
97-
<code>
98-
import java.io.File;
99-
public class Main {
100-
public static void main(String[] args) {
101-
File myFile = new File("myfile.txt");
102-
System.out.println(myFile);
103-
}
104-
}
105-
</code>
106-
</program>
107-
108-
<note>
109-
<p>
110-
<c>myFile</c> is the name of the object within the program, while <c>myfile.txt</c> is the name of the file itself and will be the file name if the operation that creates the file is successful.
111-
</p>
112-
</note>
113-
114-
<p>
115-
Now that we have created a new <c>File</c> object, we can create a file using the <c>createNewFile()</c> method from the <c>File</c> class. While the previous line of code creates an object within the program for the file, this method actually does the work of creating a file and saving it in the current working directory. This method returns a <c>boolean</c> value. If the method returns <c>true</c>, the file was successfully created. If the method returns <c>false</c>, there is already a file using the chosen file name. We can use this method's possible return values in tandem with an try/catch structure to determine if the file was created, or catch the error if a file with that file name already exists in the directory.
88+
Now let's learn how to make a file in Java. In Python. files can be made using the <c>open()</c> function on a file path that doesn't exist yet. Similarly, in Java you create a file by using the <c>createNewFile()</c> method on a <c>File</c> object. This method actually does the work of creating a file and saving it in the current working directory, and returns a boolean value of either true or false if the file is successfully created. We can use this method's possible return values in tandem with an try/catch structure to determine if the file was created, or catch the error if a file with that file name already exists in the directory.
11689
</p>
11790

11891
<p>
@@ -122,14 +95,15 @@
12295
<program xml:id="creat-file-exp-2" interactive="activecode" language="python">
12396
<code>
12497
filename = "newfile.txt"
125-
print(f"Attempting to write to '{filename}' using 'w' mode...")
98+
print("Attempting to write to '" + filename + "' using 'w' mode...")
12699
try:
127100
with open(filename, 'w') as f:
128101
f.write("This file was created using 'w' mode.")
129-
print(f"SUCCESS: The file '{filename}' was created or overwritten.")
102+
print("SUCCESS: The file '" + filename + "' was created or overwritten.")
130103
except Exception as e:
131104
# This would only catch other unexpected errors
132-
print(f"An unexpected error occurred during write: {e}")
105+
print("An unexpected error occurred during write: " + str(e))
106+
133107
</code>
134108
</program>
135109

0 commit comments

Comments
 (0)