简体   繁体   English

Eclipse从文件中读取stdin(System.in)

[英]Eclipse reading stdin (System.in) from a file

Eclipse是否可以从文件中读取stdin?

Pure Java 纯Java

You can redirect System.in with a single line of code: 您可以使用一行代码重定向System.in:

System.setIn(new FileInputStream(filename));

See System.setIn() . 请参见System.setIn()

Eclipse config Eclipse配置

In Eclipse 4.5 or later, the launch configuration dialog can set System.in to read from a file. 在Eclipse 4.5或更高版本中,启动配置对话框可以将System.in设置为从文件中读取。 See the announcement here . 请参阅此处的公告

启动配置对话框的常用选项卡

[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) 在bash或命令提示符下,您可以执行以下操作: C:\\myprogramm < file.txt (Windows)或./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. 不幸的是,在Eclipse中不可能实现相同的结果,因为没有选项将stdin绑定到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. 一种方法是使用程序参数启用它,另一个使用file参数。 See Scott's answer on what kind of code you need to add to parse the -d option from the program arguments array. 请参阅Scott的答案,了解需要添加哪种代码来解析程序参数数组中的-d选项。

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: 当您想要指向Eclipse项目中的某个文件时,您需要小心地获取正确的位置并使用$ {resource_loc:}变量:

-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. resource_loc参数指向您的工作区。 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. 这是存储所有eclipse项目的文件夹。从那里你仍然需要引用你的项目文件夹,然后是你想要加载的文件。

You may have to tweak the slash direction, depending if you use Linux or Winodws. 您可能需要调整斜杠方向,具体取决于您是使用Linux还是Winodws。

2015 update 2015年更新

There is a new Eclipse version, Eclipse 4.5 (Mars) which has fixed the issue ! 有一个新的Eclipse版本Eclipse 4.5(Mars)修复了这个问题

This is the announcement and description of the feature: http://eclipse.org/mars/noteworthy/#_assigning_stdin_to_a_file 这是该功能的公告和说明: 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. 现在可以将Stdin分配给启动配置对话框的“ 公用”选项卡中的文件。 在此输入图像描述

As expected, this is a new feature of the just released eclipse 4.5 and will therefore not work in an older version. 正如所料,这是刚刚发布的eclipse 4.5的一个新功能,因此不适用于旧版本。

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. 我希望在那里看到2个文件字段,一个用于标准输入,一个用于标准输出和附加选项。

You will need to tweak your code some to get this working within eclipse. 您将需要调整一些代码以使其在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. 另一个我见过要更改Run ..> Common选项卡> Standard Input and Output> File只将stdout行为更改为文件,而不是输入。

The solution was to take the "-d" option to a workable solution. 解决方案是将“-d”选项用于可行的解决方案。 If you put anything in the program arguments it's just going to be passed into your main. 如果你在程序参数中放入任何东西,它就会被传递到你的main中。 So what you can do at the beginning of main is something like this: 那么你在main的开头可以做的是这样的:

    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. 您仍然需要在Run ..菜单中设置程序参数,如答案中所述。

The solution for me (running on a Mac, using Eclipse CDT for a C application) was to add "< path/to/somefile" at the end of other arguments in the "Arguments" tab of the "Run Configuration" dialog. 对我来说(在Mac上运行,使用Eclipse CDT作为C应用程序)的解决方案是在“运行配置”对话框的“参数”选项卡中的其他参数的末尾添加“<path / to / somefile”。

Also check this other question for an answer involving launching a Java program in a suspended state and then attaching the debugger using Eclipse. 另请查看另一个问题,以获得有关在挂起状态下启动Java程序然后使用Eclipse附加调试器的答案。

I don't see a nice way to do it using the standard Eclipse Run dialog. 我没有看到使用标准Eclipse Run对话框执行此操作的好方法。 However, you might be able to do it with an External tool launcher and either use the standard "< infile" syntax or call a .bat/.sh script to do it for you. 但是,您可以使用外部工具启动程序执行此操作,并使用标准的“<infile”语法或调用.bat / .sh脚本为您执行此操作。

It's not automated, which is what I'm guessing you want, but you can copy & paste the contents of your infile to the Eclipse console when you launch your program. 它不是自动化的,这是我猜你想要的,但你可以在启动程序时将infile的内容复制并粘贴到Eclipse控制台。

< ${workspace_loc:/MyProject/file}

in debug configuration/arguments/program arguments: 在调试配置/参数/程序参数中:

works well for me. 适合我。

For linux: I think, the easiest way is to write an sh-script. 对于linux:我认为,最简单的方法是编写一个sh脚本。

1) write run.sh like this: 1)像这样写run.sh:

your program with params < input.txt 你的程序使用params <input.txt

2) In the Eclipse profile configuration specify 2)在Eclipse配置文件配置中指定

[tab Main] C++ Run Application = /bin/sh [tab Main] C ++ Run Application = / bin / sh

[tab Arguments] Program Arguments = run.sh [tab Arguments] Program Arguments = run.sh

That's it. 而已。 When you will run your project, you will see the script's output in the eclipse console. 当您运行项目时,您将在eclipse控制台中看到脚本的输出。 You can do something similar in Windows. 您可以在Windows中执行类似的操作。

This is surprisingly not supported in Eclipse and it seems there are no current plans to add it. 令人惊讶的是,这在Eclipse中并不受支持,似乎目前没有计划添加它。 See https://bugs.eclipse.org/bugs/show_bug.cgi?id=155411 to follow and vote. 请参阅https://bugs.eclipse.org/bugs/show_bug.cgi?id=155411进行跟进并投票。

I will update here if it they do implement it. 我会在这里更新,如果他们确实实现了它。

What I did was to create an Ant target and launch it as "Run External" from Eclipse, here are the steps: 我做的是创建一个Ant目标并从Eclipse启动它作为“Run External”,以下是步骤:

  • I have one input file to read from: res\\in.txt and one for the output: res\\out.txt 我有一个输入文件可供读取: res\\in.txt和一个输出: res\\out.txt
  • Create a build.xml with the targets you require (this is just an example): 使用您需要的目标创建build.xml (这只是一个示例):

     <project basedir="." default="run" name="Tests"> <target name="clean"> <delete dir="bin"/> </target> <target name="compile"> <mkdir dir="bin"/> <javac srcdir="src" destdir="bin" includeantruntime="false"/> </target> <target name="build" depends="clean,compile"/> <target name="run" depends="build"> <java classname="Main" input="res\\in.txt" output="res\\out.txt" classpath="bin" /> </target> </project> 
  • In Eclipse go to: Run->External Tools->External Tools Configurations->Ant Build-> New Launch Configuration use the following configuration: 在Eclipse中转到: Run->External Tools->External Tools Configurations->Ant Build-> New Launch Configuration使用以下配置:

Section Main

Buildfile: ${workspace_loc:/Tests/build.xml} ${workspace_loc:/Tests/build.xml}${workspace_loc:/Tests/build.xml}

Base Directory: ${workspace_loc:/Tests} 基本目录: ${workspace_loc:/Tests}

*Note: Tests is the name of my Eclipse project *注意:测试是我的Eclipse项目的名称

Now you can run your project by clicking in the Run Extenal tool bar button and change the input or output files as you needed 现在,您可以通过单击Run Extenal工具栏按钮来运行项目,并根据需要更改输入或输出文件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM