命令行参数

发布于 2024-08-20 16:07:20 字数 117 浏览 3 评论 0原文

我无法理解这些句子之间的区别!您可以为这些句子写一些代码片段吗?谢谢

  • 该程序将接收一个目录路径作为第一个命令行参数。
  • 该程序将接收文件路径作为第二个命令行参数。

I can not get the difference between these sentences! would you please write some snippet code for these sentences?thanks

  • The program will receive a path to a directory as the first command-line argument.
  • The program will receive a path to a file as the second command-line argument.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

南城追梦 2024-08-27 16:07:20

就这么简单:

public static void main(String[] args)
{
    // args[0] is the directory path
    // args[1] is the file path
}

那你不明白什么?

It's as simple as that:

public static void main(String[] args)
{
    // args[0] is the directory path
    // args[1] is the file path
}

So what don't you understand?

云仙小弟 2024-08-27 16:07:20

想象一下您使用的命令行复制程序:

copy <destination-dir> <source-file>

Java 中的一个简单实现是(作为片段提供):

package com.example;
import java.io.File;
public class Copy {

  public static void main(String[] args) {

    if (args.length != 2) {
      exitWithErrorCode(); // to be implemented
    }

    File destinationDir = new File(args[0]);
    File sourceFile = new File(args[1]);

    copyFileToDir(sourceFile, destinationDir); 
  }

  private static void copyFileToDir(File sourceFile, File destDir) {
    // to be implemented
  }
}

并且您可以像这样调用它

java com.example.Copy /tmp /home/me/example.txt

Imagine a command line copy program that you use like that:

copy <destination-dir> <source-file>

A simple implementation in Java would be (provided as a fragment):

package com.example;
import java.io.File;
public class Copy {

  public static void main(String[] args) {

    if (args.length != 2) {
      exitWithErrorCode(); // to be implemented
    }

    File destinationDir = new File(args[0]);
    File sourceFile = new File(args[1]);

    copyFileToDir(sourceFile, destinationDir); 
  }

  private static void copyFileToDir(File sourceFile, File destDir) {
    // to be implemented
  }
}

and you would call it like

java com.example.Copy /tmp /home/me/example.txt
爱,才寂寞 2024-08-27 16:07:20

这意味着程序将像这样运行:

java some.package.YourProgram /some/directory /some/file/name

It means that the program will be run like this:

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