我已经为这个问题苦苦挣扎了几个小时,不知道如何最好地进行。
代码应该使用凯撒密码读取文件并对其进行编码,然后将其写入磁盘并附加到新文件中 _encoded 。
它当然会创建文件,但每次都是空白的。当我查看文档时,它说要确保冲洗并关闭打印机。我已经做到了。
我也想可能是因为我在绕道而行;我想知道是不是使用了 FileOutputstream。
Scanner sc=new Scanner(inFile);
//File outFile=new File("caesar_encoded.txt");
//FileOutputStream outFileStream=new FileOutputStream(outFile);
PrintWriter outStream=new PrintWriter("caesar_encoded.txt");
while(sc.hasNext())
{
String phrase = sc.nextLine().toUpperCase();
for (int i = 0; i < phrase.length(); i++) {
if (Character.isLetter(phrase.charAt(i))) {
for (int j = 0; j < alpha.length; j++) {
if (phrase.charAt(i) == alpha[j]) {
if (j == alpha.length - 1) {
outStream.print(alpha[0]);
} else {
outStream.print(alpha[j + 1]);
}
}
}
} else {
outStream.print(phrase.charAt(i));
}
}
outStream.println();
}
outStream.flush();
outStream.close();
sc.close();
我尝试这样做,但结果相同:
Scanner sc=new Scanner(inFile);
File outFile=new File("caesar_encoded.txt");
FileOutputStream outFileStream=new FileOutputStream(outFile);
PrintWriter outStream=new PrintWriter(outFileStream);