From 9ece034cc6203086c798a7871a453e63fc3e75f3 Mon Sep 17 00:00:00 2001 From: mahboobe <mahboobe.shakerinadr@ucalgary.ca> Date: Fri, 18 Oct 2024 13:34:37 -0600 Subject: [PATCH] changes in reading and writng to another folder --- src/Main.java | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/src/Main.java b/src/Main.java index 4f49a45..384b5e1 100644 --- a/src/Main.java +++ b/src/Main.java @@ -8,14 +8,14 @@ import java.io.*; public class Main { public static void main(String[] args) { - // Exception Examples +// Exception Examples // try { // int[] grades = {97,78,80}; // System.out.print(grades[5]); // } catch (Exception e) { // e.printStackTrace(); // } -// + // try { // int[] numbers = {1, 2, 3}; // System.out.println(numbers[5]); // This will throw ArrayIndexOutOfBoundsException @@ -24,20 +24,21 @@ public class Main { // } String filename = "example.txt"; - String data = "This is an example of writing to and reading from a file.\n the second line"; + String data = "This is an example of writing to and reading from a file.\n the second line modified"; // Step 1: Create and write to the file try (BufferedWriter writer = new BufferedWriter(new FileWriter(filename))) { writer.write(data); System.out.println("Data written to file: " + filename); } catch (IOException e) { + e.printStackTrace(); System.out.println("Error writing to file: " + e.getMessage()); } - - // -// // Step 2: Close the file (automatically closed with try-with-resources) // +//// +//// // Step 2: Close the file (automatically closed with try-with-resources) + filename ="C:\\Users\\mahbo\\OneDrive - University of Calgary\\CPSC219\\example2.txt"; // Step 3: Reopen and read from the file try (BufferedReader reader = new BufferedReader(new FileReader(filename))) { String line; @@ -46,9 +47,10 @@ public class Main { System.out.println(line); } } catch (IOException e) { + e.printStackTrace(); System.out.println("Error reading from file: " + e.getMessage()); } - +// data = "\n Writing this massage as an append"; // Step 1: Create and write to the file @@ -58,7 +60,7 @@ public class Main { } catch (IOException e) { System.out.println("Error writing to file: " + e.getMessage()); } -// + -- GitLab