From a4c38c33173d4f8b9b45b87d14538ac8ae2f3336 Mon Sep 17 00:00:00 2001 From: Alex Date: Sun, 27 Apr 2025 23:11:37 -0500 Subject: [PATCH] mostly done --- src/input.txt => input.txt | 2 +- output.txt | 3 ++ src/Main.java | 62 ++++++++++++++++++++++++++------------ 3 files changed, 46 insertions(+), 21 deletions(-) rename src/input.txt => input.txt (56%) create mode 100644 output.txt diff --git a/src/input.txt b/input.txt similarity index 56% rename from src/input.txt rename to input.txt index e7b5412..e3dcc46 100644 --- a/src/input.txt +++ b/input.txt @@ -1,3 +1,3 @@ Hello World! This is a file, that -Has words and stuff in it \ No newline at end of file +Has words and stuff in it diff --git a/output.txt b/output.txt new file mode 100644 index 0000000..6ed3a69 --- /dev/null +++ b/output.txt @@ -0,0 +1,3 @@ +HELLO WORLD! +THIS IS A FILE, THAT +HAS WORDS AND STUFF IN IT diff --git a/src/Main.java b/src/Main.java index 7b77074..ca6fe08 100644 --- a/src/Main.java +++ b/src/Main.java @@ -4,21 +4,27 @@ import java.io.*; public class Main { // Read contents of inFile, convert it all to uppercase, write to outFile - // Do line-by-line - + // Will save each line of inFile in a string, convert string to upper, + // then write it to outFile public static void upperCaseFileConverter(String inFile, String outFile) { try { + // Make the input and output reader, for inFile and outFile respectively BufferedReader in = new BufferedReader(new FileReader(inFile)); BufferedWriter out = new BufferedWriter(new FileWriter(outFile)); - String line; + String line; // String to store the line we will read and convert - while((line = in.readLine()) != null) { + // Read inFile until its null input + while ((line = in.readLine()) != null) { + // WRite out the string, converted to uppercase out.write(line.toUpperCase()); - //out.newLine(); + out.newLine(); // This will ensure new lines come correctly, platform independent } - + // Closing the writing streams + in.close(); + out.close(); } + // In case inFile doesnt exist, or program is unable to write outFile catch (IOException e) { System.out.println("File not found:"); System.out.println(e.getMessage()); @@ -28,9 +34,6 @@ public class Main { public static void main(String[] args) { - - - // Main for Q2 // Create the array of 5 testScore objects to be serialized later TestScores[] testScoresArray = new TestScores[5]; @@ -40,46 +43,65 @@ public class Main { testScoresArray[3] = new TestScores(new int[]{72, 87, 92, 92, 96}); testScoresArray[4] = new TestScores(new int[]{100,100,100,100,100,100,100}); + // Print out the averages of each testScores so we can compare it after for(TestScores testScore : testScoresArray) { System.out.println(testScore.getAverage()); } System.out.println("\n"); + + // File to store the serilized classes String filename = "testScores.txt"; // Serialize array to file - try{ + try { + // Create file output stream to write to file FileOutputStream fileOut = new FileOutputStream(filename); + // Create objectOutputStream to be able to write the class to file ObjectOutputStream out = new ObjectOutputStream(fileOut); out.writeObject(testScoresArray); System.out.println("Test Scores written to " + filename); + // Close the streams + fileOut.close(); + out.close(); } catch (IOException e) { System.out.println(e.getMessage()); } // Deserialize objects from file + // Array to store deserialized files TestScores[] deserializedScores = null; - try{ FileInputStream fileIn = new FileInputStream(filename); ObjectInputStream in = new ObjectInputStream(fileIn); deserializedScores = (TestScores[]) in.readObject(); System.out.println("Test Scores deserialized from " + filename); + // Close the streams + in.close(); + fileIn.close(); } - //TODO: FIX THESE CATCH CLAUSES - catch(IOException e){ + catch(FileNotFoundException e){ System.out.println(e.getMessage()); - } catch (ClassNotFoundException e) { + } catch (IOException e) { throw new RuntimeException(e); } - try { - for (TestScores testScore : deserializedScores) { - System.out.println(testScore.getAverage()); - } - } - catch(NullPointerException e){ + catch(ClassNotFoundException e){ System.out.println(e.getMessage()); } + if(deserializedScores != null) { + try { + for (TestScores testScore : deserializedScores) { + // Print out averages of deserialized class to compare to original values + System.out.println(testScore.getAverage()); + } + } + catch(NullPointerException e){ + System.out.println(e.getMessage()); + } + } + + + // Running function for Q3 upperCaseFileConverter("input.txt", "output.txt");