Java 返回缓冲读取器

Java 返回缓冲读取器,java,bufferedreader,Java,Bufferedreader,我遇到了一个问题,我不知道会发生什么 我有一个类,它调用第二个类通过ssh发送命令并返回结果 结果是一个BufferedReader,然后在我的第一个类中处理这个结果 头等舱: . . . String command = "ping " + ip + " -c 1"; BufferedReader result = instance.sendCommand(command); // close only after all commands

我遇到了一个问题,我不知道会发生什么

我有一个类,它调用第二个类通过ssh发送命令并返回结果

结果是一个BufferedReader,然后在我的第一个类中处理这个结果

头等舱:

  .
  .
  .       
  String command = "ping " + ip + " -c 1";
  BufferedReader result = instance.sendCommand(command);
         // close only after all commands are sent
         System.out.println("out of sshManager result : "+result);
         System.out.println("out of sshManager line : "+result.readLine());
         String line = null;
         while ((line = result.readLine()) != null) {
             System.out.println("out of sshManager line: "+line);
         }
二等舱:

public BufferedReader sendCommand(String command) throws JSchException, IOException {
        //StringBuilder outputBuffer = new StringBuilder();    

        Channel channel = sesConnection.openChannel("exec");
        ((ChannelExec) channel).setCommand(command);
        channel.connect();

        InputStream commandOutput = channel.getInputStream();
        System.out.println("This is in sshmanager SSHManager: " + commandOutput);
        result = new BufferedReader(new InputStreamReader(commandOutput));            
        String line = null;
        System.out.println(" sshmanager result : " + result());
        System.out.println(" sshmanager result line : " + result.readLine());

        while ((line = result.readLine()) != null) {
            System.out.println("in sshmanager: " + line);
        }

        System.out.println("in sshmanager result : " + result);
        channel.disconnect();
        return result;        
}
结果:

This is in sshmanager SSHManager: com.jcraft.jsch.Channel$MyPipedInputStream@40d5bd18
sshmanager result : java.io.BufferedReader@10a5ae6e
sshmanager result line : PING 192.168.11.11 (192.168.11.11) 56(84) bytes of data.
in sshmanager: From 192.168.11.77 icmp_seq=1 Destination Host Unreachable
in sshmanager: 
in sshmanager: --- 192.168.11.11 ping statistics ---
in sshmanager: 1 packets transmitted, 0 received, +1 errors, 100% packet loss, time 3006ms
in sshmanager: 
in sshmanager result: java.io.BufferedReader@10a5ae6e
out of sshManager result: java.io.BufferedReader@10a5ae6e
out of sshManager line: null
这些对象在我的第二个类中创建得很好,但我不知道为什么,当我试图管理第一个类中的对象时,内容是空的


你知道怎么回事吗?

当文件返回到第一个方法时,
BufferedReader
已经读取了文件。您可能希望返回一个包含文件内容的
列表,而不是
BufferedReader

public List<String> sendCommand(String command) throws JSchException, IOException {
        List<String> lines = new LinkedList<String>();
        Channel channel = sesConnection.openChannel("exec");
        ((ChannelExec) channel).setCommand(command);
        channel.connect();

        InputStream commandOutput = channel.getInputStream();
        result = new BufferedReader(new InputStreamReader(commandOutput));            
        String line = null;

        while ((line = result.readLine()) != null) {
           lines.add(line);
        }

        channel.disconnect();
        return lines;        
}
publicsistsendcommand(String命令)抛出jscheexception、IOException{
列表行=新建LinkedList();
Channel-Channel=sesConnection.openChannel(“exec”);
((ChannelExec)channel).setCommand(command);
channel.connect();
InputStream commandOutput=channel.getInputStream();
结果=新的BufferedReader(新的InputStreamReader(commandOutput));
字符串行=null;
而((line=result.readLine())!=null){
行。添加(行);
}
通道断开();
回流线;
}

这一切都源于流媒体的概念

一旦你在“二等舱”中通过这条流,你实际上就消耗了这条流。因此,当您将其返回到“第一类”时,流被完全消耗。因此,您不能再流式传输任何内容


请注意,即使将流包装到读取器中,读取操作(例如,
readLine
)也会转发到流。

缓冲读取器就像一个字节字符串,可以从读取一次


您的基本问题是,在函数中,您已经从读卡器中提取了文本,因此当您再次尝试时,您只会得到null(因为读卡器现在是空的)。

谢谢,它可以工作,我认为这是一个好主意,因为我必须保留这些数据用于其他操作。顺便说一句,BufferedReader有一点我不明白,因为如果我在第二个类中注释了
readlince
,并删除了while,结果仍然是空的。但是,如果我保留
System.out.println(“sshmanager结果行:“+result.readLine()”)我可以在没有第一行的情况下阅读第一节课的结果,这有点奇怪,不是吗?