Is there a way to determine if Java System.in is “interactive”?

前端 未结 2 497
醉酒成梦
醉酒成梦 2021-01-06 00:18

If the input is interactive, i.e. from the console, I want to print a command prompt e.g. \">\"

But if it is redirected e.g. from a file, then I do not want to promp

2条回答
  •  失恋的感觉
    2021-01-06 00:56

    You can make it interactive:

    public static void main(String args[]) throws Exception {
        String action = null;
        Scanner in = new Scanner(System.in);
    
        while (!((action = in.nextLine()).equalsIgnoreCase("exit"))) {
            if (action.equalsIgnoreCase("open")) {
                openConnections();
            } else if (action.equalsIgnoreCase("close")) {
                closeConnections();
            }
        }
    }
    

提交回复
热议问题