Search code examples
javabooleaninputstreamstringbuildertasklist

Java Loop InputStream until boolean = false


I have this input stream that checks if I have a certain CAD file open or not. I am doing this by using an input stream to run a tasklist command with the name I want to check. I currently have a boolean that returns true if the specific CAD file isn't open. If the CAD file is open, it returns false. However, I want it to be able to loop this until the CAD file is open because as of right now I have to keep running it in order for it to work. I also need to be able to check this boolean from a separate class. I have it in my main right now so i could test it. My code looks like this...

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();
}
}

My other class is going to have an 'if statement' that checks whether my boolean "logical" is false, and if so, print something. I have tried every possible method I could think of, but I cannot get it to function the way I want it to. Every other thing I found involving looping an inputstream didn't really apply to my situation. So hopefully someone can help me out in achieving what I want to do.


Solution

  • I would start by moving everything out of main and into a different class. This will make retrieving values and calling specific functions easier. Then create an object of that class in main. Once that is done, I'd create a get method for the boolean variable. Now to focus on the loop. Once the object is created in main, create a conditional loop inside of main which calls the function you need until a different condition is met. This condition might be met once the file is open. After the condition is met, it exits to another loop that relies on another conditional, such as user input.

    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){
    
            //}
        //}
    }
    }
    

    Other class

    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;
    }
    }