简体   繁体   English

command_line参数

[英]command_line argument

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. 该程序将收到文件的路径作为第二个命令行参数。

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? 那你不明白吗?

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): 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
  }
}

and you would call it like 你会这样称呼它

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

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

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

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

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