|
195 | 195 | </program> |
196 | 196 |
|
197 | 197 | <p> |
198 | | - The following Java code functions very similarly to the previous Python. The main difference here is that unlike Python, In Java we use the <c>Scanner</c> object to iterate through and read lines in the file. You will notice that the structure of the Java code is still similar to the Python; Both use a try and catch statement to read the file and catch any errors. |
| 198 | + The following Java code functions very similarly to the previous Python. The main difference here is that unlike Python, in Java we use the <c>Scanner</c> object to iterate through and read lines in the file. You will notice that the structure of the Java code is still similar to the Python; Both use a try and catch statement to read the file and catch any errors. |
199 | 199 | </p> |
200 | 200 |
|
201 | 201 | <program xml:id="file-read-java" interactive="activecode" language="java" add-files= "myfile-read"> |
|
235 | 235 | Let us create the framework for a class that will write to a file. Let's call this class <c>WriteFile</c>: |
236 | 236 | </p> |
237 | 237 |
|
238 | | - <program xml:id="file-write-exp1" interactive="activecode" language="java"> |
239 | | - <code> |
| 238 | + <pre> |
240 | 239 | import java.io.File; |
241 | 240 | import java.io.FileWriter; |
242 | 241 | import java.io.IOException; |
243 | 242 | import java.util.Scanner; |
244 | 243 | public class WriteFile { |
245 | 244 | public static void main(String[] args) { |
246 | | - String filename = "test_file.txt"; |
247 | | - try (FileWriter writer = new FileWriter(filename)) { |
248 | | - writer.write("This line was written by the program."); |
249 | | - System.out.println("Successfully wrote to the file."); |
250 | | - } |
251 | | - catch (IOException e) { |
252 | | - System.out.println("An error occurred during writing."); |
253 | | - } System.out.println("--- Reading file back ---"); |
254 | | - try (Scanner reader = new Scanner(new File(filename))) { |
255 | | - while (reader.hasNextLine()) { |
256 | | - System.out.println(reader.nextLine()); |
257 | | - } |
258 | | - } |
259 | | - catch (IOException e) { |
260 | | - System.out.println("An error occurred during reading."); |
261 | | - } |
| 245 | + |
262 | 246 | } |
263 | 247 | } |
264 | | - </code> |
265 | | - </program> |
| 248 | + </pre> |
266 | 249 |
|
267 | 250 | <p> |
268 | 251 | Next, we will create a <c>FileWriter</c> object. Let's call it <c>myWriter</c>: |
|
299 | 282 | </datafile> |
300 | 283 | <program xml:id="file-write-exp2" interactive="activecode" language="python" add-files = "my-file-8-4-2"> |
301 | 284 | <code> |
302 | | - with open("myfile8-4-2.txt", "r") as file_reader: |
303 | | - while True: |
304 | | - line = file_reader.readline() |
305 | | - if not line: # End of file |
306 | | - break |
307 | | - print(line.strip()) |
| 285 | + try: |
| 286 | + with open("myfile.txt", "w") as my_writer: |
| 287 | + my_writer.write("File successfully updated!") |
| 288 | + print("File successfully written to.") |
| 289 | + except OSError as e: |
| 290 | + print("An error occurred.") |
| 291 | + import traceback |
| 292 | + traceback.print_exc() |
308 | 293 | </code> |
309 | 294 | </program> |
310 | 295 |
|
311 | 296 | <p> |
312 | 297 | And the equivalent Java code: |
313 | 298 | </p> |
314 | 299 | <datafile label="my-file-8-4-3" filename="myfile8-4-3.txt" xml:id= "my-file-8-4-3" editable="yes"> |
315 | | - <pre> |
316 | | - |
317 | | - </pre> |
318 | | - </datafile> |
319 | | - <program xml:id="file-write-exp3" interactive="activecode" language="java" add-files = "my-file-8-4-3"> |
320 | | - <code> |
321 | | - import java.io.File; |
322 | | - import java.io.IOException; |
323 | | - import java.util.Scanner;public class WriteFile { |
324 | | - public static void main(String[] args) { |
325 | | - String filename = "myfile8-4-3.txt"; |
326 | | - try (Scanner reader = new Scanner(new File(filename))) { |
327 | | - while (reader.hasNextLine()) { |
328 | | - String line = reader.nextLine(); |
329 | | - System.out.println(line.trim()); |
330 | | - } |
331 | | - } catch (IOException e) { |
332 | | - System.out.println("An error occurred."); |
333 | | - } |
334 | | - } |
| 300 | + <pre> |
| 301 | + |
| 302 | + </pre> |
| 303 | + </datafile> |
| 304 | + <pre> |
| 305 | + try { |
| 306 | + FileWriter myWriter = new FileWriter("myfile.txt"); |
| 307 | + myWriter.write("File successfully updated!"); |
| 308 | + myWriter.close(); |
| 309 | + System.out.println("File successfully written to."); |
| 310 | + } catch (IOException e) { |
| 311 | + System.out.println("An error occurred."); |
| 312 | + e.printStackTrace(); |
335 | 313 | } |
336 | | - </code> |
337 | | - </program> |
| 314 | + </pre> |
338 | 315 |
|
339 | 316 | <p> |
340 | 317 | And that's it! We will add our code to the foundational code for a complete program. First, an example of equivalent Python code: |
|
0 commit comments