Eclipse reading stdin (System.in) from a file

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

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

相关标签:
11条回答
  • 2020-11-27 04:03

    On the "common" tab of the run dialog, under "Standard Input and Output" there's a checkbox for "file". but it appears to be only for output...

    I'd expect to see 2 file fields there, one for standard in, one for standard out with the append options.

    0 讨论(0)
  • 2020-11-27 04:03
    < ${workspace_loc:/MyProject/file}
    

    in debug configuration/arguments/program arguments:

    works well for me.

    0 讨论(0)
  • 2020-11-27 04:06

    [Update] As it has been pointed out in the comments, this answer was misleading so I have updated it to correct the errors. [/Update]

    On bash or command prompt you can do: C:\myprogramm < file.txt (Windows) or ./myprogramm < file.txt (Linux)

    Unfortunately in Eclipse it is not possible to achieve the same result, since there is no option to bind the stdin to a file in eclipse. Instead you will have to manually open a file stream in your code, and then read from it instead. One way to do this is by using a program argument to enable it and another one with the file parameter. See Scott's answer on what kind of code you need to add to parse the -d option from the program arguments array.

    When you want to point to some file inside your Eclipse Project, you need to be careful to get the location right and use the ${resource_loc:} variable:

    -d ${resource_loc:/MyProject/file}
    

    of course you can also put an absolute path:

    -d path/to/file or -d C:\path\to\file
    

    The resource_loc parameter refers to your workspace. That is the folder where all you eclipse projects are stored in. From there you still have to reference your project folder and then the file that you want to load.

    You may have to tweak the slash direction, depending if you use Linux or Winodws.

    0 讨论(0)
  • 2020-11-27 04:06

    2015 update

    There is a new Eclipse version, Eclipse 4.5 (Mars) which has fixed the issue!

    This is the announcement and description of the feature: http://eclipse.org/mars/noteworthy/#_assigning_stdin_to_a_file

    Stdin can now be assigned to a file in the Common tab of launch configuration dialogs. enter image description here

    As expected, this is a new feature of the just released eclipse 4.5 and will therefore not work in an older version.

    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题