我一直在编写基本的位级加密代码,并且我非常确定该算法是正确的..但我无法测试它。当我运行代码时,第一个循环(使用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