我一直在編寫基本的位級加密代碼,並且我非常確定該算法是正確的..但我無法測試它。當我運行代碼時,第一個循環(使用System.in.read()
)堵塞代碼。當我通過終端發送EOF
信號時,代碼不會進一步發展 - 我在下一行檢查了一些原始語句print
。InputStream.read()阻止(干擾)執行
據我所知,發送EOF
應該有read()
返回-1
,退出循環。
我錯過了什麼?
謝謝。
public class BitLevel {
public static void main(String[] args) throws Exception {
FileInputStream input = new FileInputStream(args[0]);
FileOutputStream output = new FileOutputStream(args[1]);
ArrayList<Integer> key = new ArrayList<Integer>();
int i = 0;
System.out.print("Enter key: ");
System.out.flush();
int c = System.in.read();
while (c != -1) {
key.add((Integer) c);
c = System.in.read();
}
c = input.read();
while (c != -1) {
output.write(c^key.get(i).intValue());
output.flush();
i++;
i = i % key.size();
}
}
}
你可能應該用標準的方式閱讀,用'Scanner'等。 – Kayaman
謝謝,Kayaman。我確實錯過了「c =」奇怪的是,雖然我修正了這個錯誤後,錯誤仍然存在。 –
爲什麼你甚至試圖讀這種方式?這只是錯誤的。你會發現從任何基本教程閱讀輸入的一個體面的例子。 – Kayaman