Skip to content

Instantly share code, notes, and snippets.

@seamusabshere
Created September 18, 2012 23:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save seamusabshere/3746658 to your computer and use it in GitHub Desktop.
Save seamusabshere/3746658 to your computer and use it in GitHub Desktop.
package java112.labs1;
import java.io.*;
public class LabSix {
public static void main(String[] arguments) {
if (arguments.length != 2) {
System.out.println("Please enter two arguments on the command line, an input file name and an output file name");
} else {
LabSix lab = new LabSix();
lab.run(arguments[0], arguments[1]);
}
}
public void run(String inputFilePath, String outputFilePath) {
BufferedReader in = null;
PrintWriter out = null;
try {
// this requires catching FileNotFoundException
in = new BufferedReader(new FileReader(inputFilePath));
// this requires catching IOException
out = new PrintWriter(new BufferedWriter(new FileWriter(outputFilePath)));
mirrorFile(in, out);
} catch (FileNotFoundException fileNotFoundException) {
fileNotFoundException.printStackTrace();
} catch (IOException ioException) {
ioException.printStackTrace();
} finally {
if (in != null) {
try {
// this requires catching IOException
in.close();
} catch (IOException ioException) {
ioException.printStackTrace();
}
}
if (out != null) {
out.close();
}
}
}
public void mirrorFile(BufferedReader in, PrintWriter out) {
try {
while (in.ready()) {
out.println(in.readLine());
}
} catch (IOException ioException) {
ioException.printStackTrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment