mostly done

This commit is contained in:
2025-04-27 23:11:37 -05:00
parent dc58e008b1
commit a4c38c3317
3 changed files with 46 additions and 21 deletions

3
output.txt Normal file
View File

@@ -0,0 +1,3 @@
HELLO WORLD!
THIS IS A FILE, THAT
HAS WORDS AND STUFF IN IT

View File

@@ -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);
}
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");