|
84 | 84 |
|
85 | 85 | <section xml:id="create-file"> |
86 | 86 | <title>Creating Files</title> |
87 | | - |
88 | 87 | <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. |
116 | 89 | </p> |
117 | 90 |
|
118 | 91 | <p> |
|
122 | 95 | <program xml:id="creat-file-exp-2" interactive="activecode" language="python"> |
123 | 96 | <code> |
124 | 97 | filename = "newfile.txt" |
125 | | - print(f"Attempting to write to '{filename}' using 'w' mode...") |
| 98 | + print("Attempting to write to '" + filename + "' using 'w' mode...") |
126 | 99 | try: |
127 | 100 | with open(filename, 'w') as f: |
128 | 101 | 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.") |
130 | 103 | except Exception as e: |
131 | 104 | # 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 | + |
133 | 107 | </code> |
134 | 108 | </program> |
135 | 109 |
|
|
0 commit comments