gpt4 book ai didi

java - 跳过从 PDF 读取的字节

转载 作者:行者123 更新时间:2023-12-01 05:39:52 25 4
gpt4 key购买 nike

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.local 23:54:00 % java BS|wc 384 2003 43885

jabira-whosechild-lm.local 23:54:05 % wc /usr/share/doc/bash/rbash.pdf 384 2153 43885 /usr/share/doc/bash/rbash.pdf

Why do i see the difference in the number of characters that are read and printed to the console

最佳答案

方法InputStream.read()仅读取一个字节。

您的源代码行System.out.print((char)c);是错误的。调用方法 PrintStream.print(char c),此方法为某些非 ASCII 字符值写入两个字节。

您需要调用一种始终写入一个字节值的方法。正确的方法是System.out.write(c);

关于java - 跳过从 PDF 读取的字节,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7435636/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com