虽然循环无限运行,但不明白为什么

时间:2016-08-09 17:27:40

标签: java android

while循环无限运行。有人可以告诉我为什么?我尝试过不同的方法,但它对我不起作用。

回答并帮助解决问题 怎么写错了我不打印退出状态

            JSch jsch = new JSch();
            Session session = jsch.getSession(userName, host);
            session.setPassword(passWord);
            session.setConfig("StrictHostKeyChecking", "no");
            session.connect();
            System.out.print("Conected");
            channel = session.openChannel("shell");
            ops = channel.getOutputStream();
            ps = new PrintStream(ops, true);
            channel.connect();
            //Log.e(Keyid,"Channel Connected");
            ps.println("ls");
            //  ps.println("dir");
            //give commands to be executed inside println.and can have any no of commands sent.
            ps.close();
            InputStream in = channel.getInputStream();
           // BufferedReader input = new BufferedReader(new InputStreamReader(in, "UTF-8"));
            //System.out.println(input.ready());
            //String str = input.readLine();


            BufferedReader buffer = new BufferedReader(new InputStreamReader(in));
            String line= buffer.readLine();
            while (line != null && !line.equals("")){
              //  length = length + line.length();
                line= buffer.readLine();
                System.out.println(buffer.readLine());

            }
            System.out.println("Exit");

           /* while(buffer.readLine() != null){
                System.out.println(buffer.readLine());
                System.err.println(buffer.ready());

            }*/

2 个答案:

答案 0 :(得分:0)

使用它     while(dataIn.readLine!= null)

答案 1 :(得分:0)

我认为你在这里遇到的问题是equals方法比较字符串。但是readLine方法返回逻辑null而不是“空”字符串。因此,比较失败。

尝试将其替换为:

while (dataIn.readLine() != null){