Run Program from IntelliJ with Command Line File Input

后端 未结 6 414
青春惊慌失措
青春惊慌失措 2020-12-19 07:34

I am running OSX 10.11 with IntelliJ 14.1.15.

I have a programme which takes a txt file as an argument. I can run it from the terminal through java Searc

6条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-19 08:16

    Adding the input file name's relative path in the "Program Arguments" will allow the "main" method reading the argument in as a "String"

    However, in order to actually let the System to understand using data from the file as standard input, it seems we have to specifically read the file and set the input stream as the system input :

        try {
            FileInputStream inputStream = new FileInputStream(new File(args[0]));
            System.setIn(inputStream);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    

提交回复
热议问题