【问题标题】:How to read a file passed from CMD and use this files location from java call?如何读取从 CMD 传递的文件并从 java 调用中使用此文件位置?
【发布时间】:2016-05-16 14:34:20
【问题描述】:

我正在创建一个由 .bat 文件运行的项目。该 Java 程序需要从同一目录或特定目录中读取一个 txt 文件。但是运行bat文件的时候传的是txt文件的名字

示例.. java 类位于我的桌面上,我如何获得一个也在桌面上的 txt 文件,但我不知道它是哪个 txt 文件,直到文件名在 CMD 中传递..

我不,这有点令人困惑,但我们将不胜感激。

【问题讨论】:

标签: java file batch-file input cmd


【解决方案1】:

你想要这样的东西:

public class MyClass {

  public static void main(String[] args) {
    if (args.length > 0) {
      File file = new File(args[0]);
      System.out.println("My file is " + file.getPath());
    }
  }

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-04-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多