ユーザーに入力ファイルを入力して読み取らせ、出力ファイルで入力ファイルを「エンコード」させようとしています。しかし、Java で迷子になり始めたのはファイル処理であるため、出力ファイルに問題があり、おそらく入力ファイルにも問題があります。
これは、入力ファイルを取得するために使用されるメソッドである必要があります。
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;
}
これは、出力ファイルを取得するために使用されるメソッドである必要があります。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;
}