簡體   English   中英

如何讀取從CMD傳遞的文件並通過Java調用使用此文件位置?

[英]How to read a file passed from CMD and use this files location from java call?

我正在創建一個由.bat文件運行的項目。 此Java程序需要從同一目錄或特定目錄中讀取txt文件。 但是運行bat文件時會傳遞txt文件的名稱

例子.. java類位於我的桌面上,我將如何獲取也在桌面上的txt文件,但是我不知道它是哪個txt文件,直到在CMD中傳遞文件名。

我不認為這是一個令人困惑的解釋,但是任何幫助將不勝感激。

您想要這樣的東西:

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());
    }
  }

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM