Java 如果满足条件,但跳过到下一个条件
我不知道为什么,但当我尝试调试时,我发现这很奇怪: 如图所示,in.readLine的值为null,in.readLine==null为true。但是为什么它会跳过if in.readLine==null{…行呢?但是当我试图将断点放在第266行和第267行时,它会在这个条件下输入代码 守则:Java 如果满足条件,但跳过到下一个条件,java,if-statement,Java,If Statement,我不知道为什么,但当我尝试调试时,我发现这很奇怪: 如图所示,in.readLine的值为null,in.readLine==null为true。但是为什么它会跳过if in.readLine==null{…行呢?但是当我试图将断点放在第266行和第267行时,它会在这个条件下输入代码 守则: private void startSOfficeService() throws InterruptedException, IOException { if (System.getProper
private void startSOfficeService() throws InterruptedException, IOException {
if (System.getProperty("os.name").matches(("(?i).*Windows.*"))) {
try {
//Check if the soffice process is running
Process process = Runtime.getRuntime().exec("tasklist /FI \"IMAGENAME eq soffice.exe\"");
//Need to wait for this command to execute
int code = process.waitFor();
//If we get anything back from readLine, then we know the process is running
BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));
if (in.readLine() == null) {
//Nothing back, then we should execute the process
String[] SOFFICE_CMD = { SOFFICE_SERVICE_PATH,
"-accept=socket,host=" + SOFFICE_SERVICE_HOST + ",port=" + SOFFICE_SERVICE_PORT + ";urp;",
"-invisible",
"-nologo"};
process = Runtime.getRuntime().exec(SOFFICE_CMD);
code = process.waitFor();
System.out.println("soffice script started");
} else {
System.out.println("soffice script is already running");
}
in.close();
in = null;
System.gc();
} catch(Exception e) {
e.printStackTrace();
}
}
}
当您的调试器在.readLine中求值时,它会从读取器中消耗。因此,如果您在所读取内容的最后一行,则in.readLine将为非null,将控件置于else中,但当您在.readLine中求值以在调试器中显示时,它会再次读取,发现没有更多行,并将null作为值返回给s如何在调试器中进行调试
要查看真实情况,请首先将in.readLine分配给变量,并观察该变量的值,该值不会通过简单地读取而改变。当调试器在in.readLine中进行计算时,它会从读取器中消耗。因此,如果您在所读取内容的最后一行,则in.readLine将不为null,并将控件置于else中,但当您计算in.readLine以在调试器中显示时,它会再次读取,发现没有更多行,并返回null作为要在调试器中显示的值 要查看真实情况,请先将in.readLine指定给一个变量,然后观察该变量的值,该值不会通过简单的读取而改变