Java Eclipse 从文件中读取标准输入 (System.in)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/188547/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Eclipse reading stdin (System.in) from a file
提问by
Is it possible for Eclipse to read stdin from a file?
Eclipse 是否可以从文件中读取标准输入?
回答by Michael Myers
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
日食配置
In Eclipse 4.5 or later, the launch configuration dialog can set System.in to read from a file. See the announcement here.
在 Eclipse 4.5 或更高版本中,启动配置对话框可以将 System.in 设置为从文件中读取。请参阅此处的公告。
回答by KC Baltz
I don't see a nice way to do it using the standard Eclipse Run dialog. 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.
我没有看到使用标准 Eclipse 运行对话框的好方法。但是,您可能可以使用外部工具启动器来执行此操作,并且可以使用标准的“< 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 控制台。
回答by John Gardner
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 个文件字段,一个用于标准输入,一个用于带有附加选项的标准输出。
回答by lanoxx
[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. 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.
不幸的是,在 Eclipse 中不可能达到相同的结果,因为在 Eclipse 中没有将标准输入绑定到文件的选项。相反,您必须在代码中手动打开文件流,然后从中读取。一种方法是使用程序参数来启用它,另一种方法是使用文件参数。请参阅 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. 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.
resource_loc 参数指的是您的工作区。那是存储所有 eclipse 项目的文件夹。从那里您仍然必须引用您的项目文件夹,然后是要加载的文件。
You may have to tweak the slash direction, depending if you use Linux or Winodws.
您可能需要调整斜线方向,具体取决于您使用的是 Linux 还是 Winodws。
回答by Stewart Stevens
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 questionfor an answer involving launching a Java program in a suspended state and then attaching the debugger using Eclipse.
还要检查这个其他问题以获取涉及在挂起状态下启动 Java 程序然后使用 Eclipse 附加调试器的答案。
回答by Vlad
< ${workspace_loc:/MyProject/file}
in debug configuration/arguments/program arguments:
在调试配置/参数/程序参数中:
works well for me.
对我来说效果很好。
回答by Scott
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.
您需要对代码进行一些调整才能使其在 eclipse 中工作。当我尝试时,上面的其他答案不起作用。我见过的另一个说要更改 Run..>Common tab>Standard Input and Output>File 只更改了文件的 stdout 行为,而不是输入。
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:
解决方案是将“-d”选项用于可行的解决方案。如果你在程序参数中放入任何东西,它就会被传递到你的 main.js 中。所以你可以在 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 thisanswer.
您仍然需要在 Run.. 菜单中设置您的程序参数,如本答案所述。
回答by studgeek
This is surprisingly not supported in Eclipse and it seems there are no current plans to add it. See https://bugs.eclipse.org/bugs/show_bug.cgi?id=155411to follow and vote.
这在 Eclipse 中令人惊讶地不受支持,而且目前似乎没有添加它的计划。请参阅https://bugs.eclipse.org/bugs/show_bug.cgi?id=155411以关注和投票。
I will update here if it they do implement it.
如果他们确实实施了,我会在这里更新。
回答by drverboten
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
Create a
build.xml
with the targets you require (this is just an example):<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:
- 我有一个要读取的输入文件:
res\in.txt
一个用于输出:res\out.txt
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>
在 Eclipse 中转到:
Run->External Tools->External Tools Configurations->Ant Build-> New Launch Configuration
使用以下配置:
Section Main
Section Main
Buildfile: ${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
*注意:Tests 是我的 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 工具栏按钮来运行您的项目,并根据需要更改输入或输出文件
回答by Ruslan Mavlyutov
For linux: I think, the easiest way is to write an sh-script.
对于 linux:我认为,最简单的方法是编写 sh-script。
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++ 运行应用程序 = /bin/sh
[tab Arguments] Program Arguments = run.sh
[选项卡参数] 程序参数 = run.sh
That's it. When you will run your project, you will see the script's output in the eclipse console. You can do something similar in Windows.
就是这样。当您运行项目时,您将在 eclipse 控制台中看到脚本的输出。你可以在 Windows 中做类似的事情。