Java循环InputStream直到布尔值=false
我有一个输入流,它检查我是否打开了某个CAD文件。我是通过使用输入流运行一个名为我要检查的任务列表命令来实现这一点的。我目前有一个布尔值,如果特定的CAD文件未打开,它将返回true。如果CAD文件已打开,则返回false。然而,我希望它能够循环,直到CAD文件被打开,因为现在我必须继续运行它,以使其工作。我还需要能够从一个单独的类中检查这个布尔值。我现在把它放在我的主机里,这样我就可以测试它了。我的代码看起来像这样Java循环InputStream直到布尔值=false,java,boolean,inputstream,stringbuilder,tasklist,Java,Boolean,Inputstream,Stringbuilder,Tasklist,我有一个输入流,它检查我是否打开了某个CAD文件。我是通过使用输入流运行一个名为我要检查的任务列表命令来实现这一点的。我目前有一个布尔值,如果特定的CAD文件未打开,它将返回true。如果CAD文件已打开,则返回false。然而,我希望它能够循环,直到CAD文件被打开,因为现在我必须继续运行它,以使其工作。我还需要能够从一个单独的类中检查这个布尔值。我现在把它放在我的主机里,这样我就可以测试它了。我的代码看起来像这样 public class AutoCadCheck { public sta
public class AutoCadCheck {
public static void main(String[] argv) throws Exception {
String notOpen = "INFO: No tasks are running which match the specified criteria";
StringBuilder textBuilder = new StringBuilder();
String command = "tasklist /fi \"windowtitle eq Autodesk AutoCAD 2017 - [123-4567.dwg]";
int i;
InputStream myStream = Runtime.getRuntime().exec(command).getInputStream();
while ((i = myStream.read()) != -1) {
textBuilder.append((char) i);
}
String output = textBuilder.toString();
boolean logical = output.contains(notOpen);
if (logical) {
System.out.println("DWG Not Open");
} else {
System.out.print(output);
}
myStream.close();
}
}
我的另一个类将有一个“if语句”,用于检查布尔“logical”是否为false,如果为false,则打印一些内容。我已经尝试了我能想到的每一种可能的方法,但是我不能让它按照我想要的方式运行。我发现涉及到输入流循环的每一件事都不适用于我的情况。所以,希望有人能帮助我实现我想要做的事情。我会先把所有东西从main搬到另一个班级。这将使检索值和调用特定函数更容易。然后在main中创建该类的对象。完成后,我将为布尔变量创建一个get方法。现在来关注循环。在main中创建对象后,在main中创建一个条件循环,该循环调用所需的函数,直到满足不同的条件。打开文件后,可能会满足此条件。满足条件后,它退出到依赖于另一个条件(如用户输入)的另一个循环
public class AutoCadCheck {
public static void main(String[] argv) throws Exception {
AutoCadFile file = new AutoCadFile();
//loop 1
//Some conditional so the program will
//continue to run after the file has been found.
// while(){
//loop 2
//check to see if the file is open or not
//while(logical){
//}
//}
}
}
其他类
import java.io.IOException;
import java.io.InputStream;
public class AutoCadFile {
private String notOpen;
private StringBuilder textBuilder;
private String command;
private int i;
private InputStream myStream;
private String output;
private boolean logical;
public AutoCadFile() {
notOpen = "INFO: No tasks are running which match the specified criteria";
textBuilder = new StringBuilder();
command = "tasklist /fi \"windowtitle eq Autodesk AutoCAD 2017 - [123-4567.dwg]";
output = textBuilder.toString();
logical = output.contains(notOpen);
try {
myStream = Runtime.getRuntime().exec(command).getInputStream();
} catch (IOException e) {
e.printStackTrace();
}
}
public void checkForFileOpen() {
try {
while ((i = myStream.read()) != -1) {
textBuilder.append((char) i);
}
if (logical) {
System.out.println("DWG Not Open");
} else {
System.out.print(output);
}
myStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public boolean getFileBoolean() {
return logical;
}
}
我的另一个类将有一个if语句来检查我的booleanlogical
是否为false
嗯,logical
是方法中的局部变量。因此,其他类中的任何代码都无法看到它
对于这类事情,有两种常见的方法:
- 将变量(即
)设置为相关类的字段。(最好不要使用逻辑
字段,因为这会导致其他问题。)静态
- 将代码放入一个方法中,该方法将返回作为结果分配给
的值logical
我可以看到您的代码中还有一些其他重要的问题
exec(String)
时,您依赖于exec
方法将命令字符串拆分为命令名和参数。不幸的是,exec
不理解命令中引用等的(OS/shell/任何特定)规则。所以它会把你引用的字符串弄得一团糟。你需要自己做分裂;i、 例如:
String[] command = new String{} {
"tasklist",
"/fi",
"windowtitle eq Autodesk AutoCAD 2017 - [123-4567.dwg]"
};
try (InputStream myStream = Runtime.getRuntime().exec(command).getInputStream()) {
// do stuff
} // the stream is closed automatically ... always
您可以在每次检查文件是否打开时执行上述代码,也可以创建一个线程,该线程执行代码时会独立休眠一段时间,然后再次执行。是的,我本来打算拆分这些命令,但出于测试目的,它可以作为一行正常工作。你能详细说明一下你所说的“它泄露了一个输入流”是什么意思吗?这是我第一次使用它,所以在将它们串在一起时我不是专家。如果在打开和调用
close()
之间抛出异常,则不会执行close()
。结果-与流关联的文件描述符将泄漏。重复这样做,您的应用程序将崩溃。。。因为它用完了文件描述符。我明白了,这是有道理的。如果我需要创建另一个InputStream,我一定会记住这一点。谢谢你的帮助,我很感谢你的精心设计。我只是出于测试的目的才有了它,这样我就可以看看它是否有效了。它实际上是从我的JToolbar上的一个按钮运行的。我试图让它从另一个类循环失败,所以我决定回到它工作的时候,然后像那样发布我的问题。不管怎么说,这似乎正是我要找的!但是我要到星期一才能测试出来。不过我还是要把这个标记为答案,因为它看起来会像我想要的那样工作。如果我最后遇到麻烦,我会告诉你的。再次感谢。