Trying to have users enter an input file to be read, and then have an output file to "encode" the input file. However, I'm having trouble with the output files, and probably the input files as well, as file processing is where I started to get lost with Java.
This should be the method used to get the input file.
public static Scanner getInputScanner(Scanner console){
Scanner input = null;
while (input == null) {
System.out.print("Enter input file: ");
String filename = console.next();
try {
input = new Scanner(new File(filename));
}
catch (FileNotFoundException e) {
System.out.println(e.getMessage());
}
}
return input;
}
This should be the method used to get the output file. Is there something that I am doing wrong concerning the usage of the PrintStream?
//Returns PrintStream for output file
//Use a try/catch block to handle a FileNotFoundException
public static PrintStream getOutputPrintStream(Scanner console){
Scanner output = null;
while (output == null) {
System.out.print("Enter output file: ");
String filename = console.next();
try {
File file = new File(filename);
PrintStream fileout = new PrintStream(file);
if (file.exists()){
System.out.print("OK to overwrite file? (y/n): ");
String answer = console.next();
if (answer.equals("y")){
}
else if (answer.equals("n")){
System.exit(1);
}
}
}
catch (FileNotFoundException e) {
System.out.println(e.getMessage());
}
}
return fileout;
}