对文件类型和操作使用命令行参数的最佳方法
我正在开发一个小型实用程序类,该类最初要读取我正在使用的四种文件类型之一。然后我发现,在知道我正在处理哪种类型的文件后,我必须知道要执行哪个操作,所以明显的选择是使用两个参数。问题是我还没有使用过命令行args 太多,我从来没有在命令行上执行多个 args
所以我的问题是:是否有一些示例代码可供查看,或者我是否只是编写一些专门在 args[0] 和 处查找某些内容的程序代码。那么在 ags[1] 处?
另外,我想知道如何在 Eclipse 下运行它时设置 args 值,这样我就不会传递任何内容?就像当 args 为 null 时,因为我没有在命令行上运行它,所以我硬编码了一些值。程序本身在其他条件下如下:
if(args.length() > 1){
}
else{
//Sets args here.
}
谢谢, 詹姆斯
I'm working on a small utility class that originally was going to read in one of four file types, which I had working. Then I found out that after I know which type of file I"m dealing with, I had to know which operation was to be performed, so the obvious choice was to use two arguments. The problem is I haven't worked with command line args much and I've never had to do multiple args on the command line.
So my questions are: is there some sample code to look at or do I just hack out some procedural code that specifically looks for something at args[0] and then at ags[1]?
Also, I wanted to know how to set the args values when running it under Eclipse so I don't pass anything in? Like when args is null because I'm not running it on the command line. So I hard code some values in the program itself under an else condition as in:
if(args.length() > 1){
}
else{
//Sets args here.
}
Thanks,
James
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
有许多 Java 库提供命令行处理,请参阅 SO:用于解析的 Java 库命令行参数。然而,您描述的场景似乎很简单,只需直接使用 args 即可。
请注意,
args
是一个数组,因此length
不是一个方法:args.length > 1
..(无括号)为了在 Eclipse 中设置命令行参数,请查看 SO:如何让 Eclipse 提示我输入命令行参数。
There are many Java libraries that offer command line handling, see SO:Java library for parsing command-line parameters. You scenario you described however seems arguably simple enough to just use
args
directly.Note that
args
is an array, thereforelength
is not a method:args.length > 1
.. (no brackets)In order to set command line arguments in Eclipse have a look at SO:How to make Eclipse prompt me for command line arguments.
命令行参数可以在 eclipse 的运行配置下设置。右键单击该类,选择“运行”菜单下的“运行配置”。
在配置窗口中,在“Java Application”下创建一个新配置,您可以设置命令行参数在“程序参数”部分
Commandline args can be set in eclipse under the run configurations. Right click on the class, select "run configurations" under "run" menu.
In the configurations window, create a new configuration under "Java Application" and you can set the command line args in the "program arguments" section