Eclipse reading stdin (System.in) from a file

后端 未结 11 1955
轮回少年
轮回少年 2020-11-27 03:40

Is it possible for Eclipse to read stdin from a file?

11条回答
  •  轮回少年
    2020-11-27 04:09

    You will need to tweak your code some to get this working within eclipse. The other answers above did not work when I tried. Another I've seen saying to change Run..>Common tab>Standard Input and Output>File only changed the the stdout behavior to the file, not the input.

    The solution was to take the "-d" option to a workable solution. If you put anything in the program arguments it's just going to be passed into your main. So what you can do at the beginning of main is something like this:

        Scanner in;
        if (args!=null && args.length>0 && args[0].equals("-d")){
            in = new Scanner(new File(args[1]));
        } else {
            in = new Scanner(System.in);
        }
    

    You will still need to set your program arguments in the Run.. menu as described in this answer.

提交回复
热议问题