对文件类型和操作使用命令行参数的最佳方法

发布于 2024-12-04 04:57:37 字数 427 浏览 1 评论 0原文

我正在开发一个小型实用程序类,该类最初要读取我正在使用的四种文件类型之一。然后我发现,在知道我正在处理哪种类型的文件后,我必须知道要执行哪个操作,所以明显的选择是使用两个参数。问题是我还没有使用过命令行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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

世界如花海般美丽 2024-12-11 04:57:37

有许多 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, therefore length 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.

百变从容 2024-12-11 04:57:37
if(args.length > 1){
   // you can get the values for args[0] and args[1] here
}
else{
//Sets args here.
}

命令行参数可以在 eclipse 的运行配置下设置。右键单击该类,选择“运行”菜单下的“运行配置”。

在此处输入图像描述

在配置窗口中,在“Java Application”下创建一个新配置,您可以设置命令行参数在“程序参数”部分

在此处输入图像描述

if(args.length > 1){
   // you can get the values for args[0] and args[1] here
}
else{
//Sets args here.
}

Commandline args can be set in eclipse under the run configurations. Right click on the class, select "run configurations" under "run" menu.

enter image description here

In the configurations window, create a new configuration under "Java Application" and you can set the command line args in the "program arguments" section

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文