|
85 | 85 | import java.io.File; |
86 | 86 | public class CreateFile { |
87 | 87 | public static void main(String[] args) { |
88 | | - File myFile = new File("myfile.txt"); |
89 | | - System.out.println(myFile); |
| 88 | + |
90 | 89 | } |
91 | 90 | } |
92 | 91 | </input> |
|
248 | 247 | </program> |
249 | 248 |
|
250 | 249 | <p> |
251 | | - Next, we will create a <c>FileWriter</c> object. Let's call it <c>myWriter</c>: |
| 250 | + Next, we will create a <c>FileWriter</c> object. Let's call it <c>myWriter</c>. The equivalent Python code to this operation is: |
| 251 | + </p> |
| 252 | + |
| 253 | + <program xml:id="write-and-close-python" language="python"> |
| 254 | + with open("myfile.txt", "w") as myWriter: |
| 255 | + </program> |
| 256 | + |
| 257 | + <p> |
| 258 | + The Java code to create a <c>FileWriter</c> object is: |
252 | 259 | </p> |
253 | 260 |
|
254 | 261 | <program xml:id="filerwriter-object-java" language="java"> |
255 | 262 | FileWriter myWriter = new FileWriter("myfile.txt"); |
256 | 263 | </program> |
257 | 264 |
|
258 | 265 | <p> |
259 | | - In this next step, we will use the <c>write()</c> method from the FileWriter class. This Method will take any data within the parenthesis and write that data to the file selected. The <c>write()</c> method takes most standard data types: |
| 266 | + In this next step, we will use the <c>write()</c> method from the FileWriter class. This Method will take any data within the parenthesis and write that data to the file selected. The <c>write()</c> method takes most standard data types. First, how this step is completed with Python: |
260 | 267 | </p> |
| 268 | + |
| 269 | + <program xml:id="write-and-close-python" language="python"> |
| 270 | + my_writer.write("File successfully updated!") |
| 271 | + </program> |
261 | 272 |
|
262 | | - <program xml:id="write-and-clase-java" language="java"> |
| 273 | + <p> |
| 274 | + And the Java equivalent. This is almost completely identical except for the second line, which is very important! |
| 275 | + </p> |
| 276 | + |
| 277 | + <program xml:id="write-and-close-java" language="java"> |
263 | 278 | myWriter.write("File successfully updated!"); |
264 | 279 | myWriter.close(); |
265 | 280 | </program> |
|
280 | 295 | 3 |
281 | 296 | </pre> |
282 | 297 | </datafile> |
283 | | - <program xml:id="file-try-catch-python" interactive="activecode" language="python" add-files = "my-file-8-4-2"> |
284 | | - <input> |
| 298 | + <program xml:id="file-try-catch-python" language="python" add-files = "my-file-8-4-2"> |
285 | 299 | try: |
286 | 300 | with open("myfile.txt", "w") as my_writer: |
287 | 301 | my_writer.write("File successfully updated!") |
|
290 | 304 | print("An error occurred.") |
291 | 305 | import traceback |
292 | 306 | traceback.print_exc() |
293 | | - </input> |
294 | 307 | </program> |
295 | 308 |
|
296 | 309 | <p> |
297 | 310 | And the equivalent Java code: |
298 | 311 | </p> |
299 | | - <datafile label="my-file-8-4-3" filename="myfile8-4-3.txt" xml:id= "my-file-8-4-3" editable="yes"> |
300 | | - <pre> |
301 | | - |
302 | | - </pre> |
303 | | - </datafile> |
| 312 | + |
304 | 313 | <program language="java" xml:id="file-try-catch-java"> |
305 | 314 | try { |
306 | 315 | FileWriter myWriter = new FileWriter("myfile.txt"); |
|
316 | 325 | <p> |
317 | 326 | And that's it! We will add our code to the foundational code for a complete program. First, an example of equivalent Python code: |
318 | 327 | </p> |
319 | | - <datafile label="my-file-8-4-4" filename="myfile.txt" xml:id= "my-file-8-4-4" editable="yes"> |
| 328 | + <datafile label="my-file-8-4-4" filename="my-file-8-4-4" xml:id= "my-file-8-4-4" editable="yes"> |
320 | 329 | <pre> |
321 | 330 |
|
322 | 331 | </pre> |
323 | | - </datafile> |
| 332 | + </datafile> |
324 | 333 | <program xml:id="file-write-full-python" interactive="activecode" language="python" add-files="my-file-8-4-4"> |
325 | 334 | <input> |
326 | 335 | try: |
|
380 | 389 | <p> |
381 | 390 | Now, when we use <c>write()</c> 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 <c>boolean</c> argument: |
382 | 391 | </p> |
383 | | - <datafile label="my-file-8-4-6" filename="myfile.txt" xml:id= "my-file-8-4-6" editable="yes"> |
| 392 | + <datafile label="my-file-8-4-6" filename="my-file-8-4-6" xml:id= "my-file-8-4-6" editable="yes"> |
384 | 393 | <pre> |
385 | 394 |
|
386 | 395 | </pre> |
|
0 commit comments