java在无限循环运行时,不明白为什么
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());
}*/
# 1 楼答案
我认为这里的问题是^{} 方法比较字符串。但是^{} 方法返回的是逻辑
null
,而不是“Null”字符串。因此,这种比较是失败的尝试将其替换为以下内容:
# 2 楼答案
用这个 while(dataIn.readLine!=null)