import java.io.*;
class BS{
public void pStr(){
try{
String command="cat /usr/share/doc/bash/rbash.pdf";
Process ps=Runtime.getRuntime().exec(command);
InputStream in = ps.getInputStream();
int c;
while((c=in.read())!=-1){
System.out.print((char)c);
}
}catch(Exception e){
e.printStackTrace();
}
}
public static void main(String args[]){
new BS().pStr();
}
}
jabira-whosechild-lm。本地23:54:00 % java BS|wc384 2003 43885
jabira-whosechild-lm。本地23:54:05 % wc/usr/share/doc/bash/rbash.pdf384 2153 43885/usr/share/doc/bash/rbash.pdf
为什么我看到读取的字符数不同并打印到控制台
方法InputStream.read()
只读取一个字节。
你的源代码行System.out.print((char)c);
是错误的。方法PrintStream.print(char c)
被调用,该方法为一些非ascii字符值写入两个字节。
你需要调用一个总是写一个字节值的方法。正确的方法是System.out.write(c);
是不是字符数相同,但单词数不同?
我猜在你的c=in.read()
和print((char)c)
代码的某个地方有一些编码问题正在发生。
你能将输出保存到另一个PDF文件并对它们进行二进制比较吗?如果它们是一样的,那就太奇怪了!如果它们不是,那么您可能会在差异中找到线索。