这是方案..
我正在通过ssh(JSCH)远程运行应用程序。现在,我想在应用程序启动时直到应用程序结束时从文件中读取数据(不断更新某些值)。 我开始将数据从该文件复制到另一个目标文件,然后转到启动应用程序的“channel.connect”...
现在,我的问题是如何知道应用程序何时完成,以便我可以停止将数据从文件复制到文件?
我尝试过channel.isClosed和isEOF等选项来检查应用程序是否已经结束!但似乎两者都不起作用,代码连续运行(由于while循环)
[我已经读过,即使命令被执行,该频道也不需要自己关闭..] ......所以我该怎么办?任何人都可以建议我一些方法来完成这个!!?
String fileNameToRead = "fileNameToRead";
File file =new File(fileNameToRead );
File statText =
new File(destinationFile);
FileOutputStream is = new FileOutputStream(statText);
OutputStreamWriter osw = new OutputStreamWriter(is);
Writer w = new BufferedWriter(osw);
String lastLine="--check--";
InputStream in=channelForApplication.getInputStream();
channelForApplication.connect();
byte[] tmp=new byte[1024];
while(true){
lastLine=getLastLine(file); // method to get the last line of the Constantly updating file.
w.write(lastLine);
w.write("\n");
while(in.available()>0){
int i=in.read(tmp, 0, 1024);
System.out.println(new String(tmp, 0, i));
if(i<0)break;
}
// System.out.printf("Get the EOF FUNCTION To CHECK-->"+channelForApplication.isEOF());
if(channelForApplication.isEOF()){
System.out.println("exit-status: "+channelForApplication.getExitStatus()+" > ");
if (channelForApplication.getExitStatus() < 0) {
System.out.println("Done, but exit status not set!");
} else if (channelForApplication.getExitStatus() > 0) {
System.out.println("Done, but with error!");
} else {
System.out.println("Done!");
}
channelForAverageCPU.disconnect();
System.out.println("the channel for av disconnected from init appl");
channelForApplication.disconnect();
w.close();
break;
}
}