I'm trying to get my basic coding principles right but there's still a long way to go. This is an exercise for CodeGym. They logic itself isn't very hard and I passed the exercise easily but I'd like my code reviewed with the following in mind:
In terms of the general architecture, I wasn't quite sure if I needed two methods. It felt tidier to me to have a method for the first task (copy) and one for the second (append) but I couldn't figure out a way to avoid calling the second method from the first.
Should read the inputs from the console in the main method and pass the result to the class? Or it totally depends on the context of the application I'm working for?
About error handling, I got completely lost. After doing some research I just decided to let the caller handle the error but I'm sure there's some stuff missing. I wasn't sure for example if I should have two try blocks on the first method but it also depended on the answer to the first point (architecture), so I gave up.
It's my first time using Javadocs, so I'm not sure what I wrote is enough
I know "Solution" is not the ideal name for the class, but I used the class created by CodeGym.
Please feel free to tear it apart :)
Thanks in advance!
public class Solution {
public static void main(String[] args) {
FileProcessor fileProcessor = new FileProcessor();
try {
fileProcessor.copyFile();
} catch (IOException e) {
System.out.println("General I/O exception: " + e.getMessage());
e.printStackTrace();
}
}
import java.io.*;
/**
* This class reads 3 filenames from console,
* copies content from the second file to the first and subsequently
* appends content from third file to first file.
*/
public class FileProcessor {
// Study comment: Solution using "var" didn't work in this IntelliJ
/**
* Reads inputs, copies second file to first and calls method to append file
* @throws IOException
*/
public void copyFile() throws IOException {
String outputFile;
String sourceToCopy;
String sourceToAppend;
try (BufferedReader reader = new BufferedReader(new InputStreamReader(System.in))) {
System.out.println("Enter a filename for the output:");
outputFile = reader.readLine();
System.out.println("Enter the path of the file to be copied");
sourceToCopy = reader.readLine();
System.out.println("Enter the path of the file to be appended");
sourceToAppend = reader.readLine();
}
try (FileOutputStream out = new FileOutputStream(outputFile);
FileInputStream copy = new FileInputStream(sourceToCopy)) {
byte[] buffer = new byte[1024];
while (copy.available() > 0) {
int count = copy.read(buffer);
out.write(buffer, 0, count);
}
}
appendFile(sourceToAppend, outputFile);
}
/**
*
* @param source file to append
* @param output final result file
* @throws IOException
*/
public void appendFile(String source, String output) throws IOException {
try (FileOutputStream appended = new FileOutputStream(output, true);
FileInputStream toAppend = new FileInputStream(source)
) {
byte[] buffer = new byte[1024];
while (toAppend.available() > 0) {
int count = toAppend.read(buffer);
appended.write(buffer, 0, count);
}
}
}
}