Java ProcessBuilder directory() 方法



描述

Java ProcessBuilder directory() 方法返回此程序構建器的當前工作目錄。隨後由此物件的 start() 方法啟動的子程序將使用此目錄作為其工作目錄。返回值可能為 null,這意味著使用當前 Java 程序的工作目錄(通常是系統屬性 user.dir 指定的目錄)作為子程序的工作目錄。

宣告

以下是 java.lang.ProcessBuilder.directory() 方法的宣告

public File directory()

引數

返回值

此方法返回此程序構建器的當前工作目錄。

異常

從 Process Builder 示例中獲取記事本的目錄詳細資訊

以下示例演示了 ProcessBuilder directory() 方法的使用。在這個程式中,我們建立了一個字串陣列,並將 notepad.exe 和 test.txt 新增到其中。使用該列表,我們初始化了一個 ProcessBuilder 例項。現在使用 directory() 方法,我們列印了底層目錄的詳細資訊。

package com.tutorialspoint;

public class ProcessBuilderDemo {

   public static void main(String[] args) {

      // create a new list of arguments for our process
      String[] list = {"notepad.exe", "test.txt"};

      // create the process builder
      ProcessBuilder pb = new ProcessBuilder(list);

      // get the working directory of the process
      System.out.println(pb.directory());
   }
}

輸出

讓我們編譯並執行上面的程式,這將產生以下結果:

null

從 Process Builder 示例中獲取計算器的目錄詳細資訊

以下示例演示了 ProcessBuilder directory() 方法的使用。在這個程式中,我們建立了一個字串陣列,並將 calc.exe 新增到其中。使用該列表,我們初始化了一個 ProcessBuilder 例項。現在使用 directory() 方法,我們列印了底層目錄的詳細資訊。

package com.tutorialspoint;

public class ProcessBuilderDemo {

   public static void main(String[] args) {

      // create a new list of arguments for our process
      String[] list = {"calc.exe"};

      // create the process builder
      ProcessBuilder pb = new ProcessBuilder(list);

      // get the working directory of the process
      System.out.println(pb.directory());
   }
}

輸出

讓我們編譯並執行上面的程式,這將產生以下結果:

null

從 Process Builder 示例中獲取 Windows 資源管理器的目錄詳細資訊

以下示例演示了 ProcessBuilder directory() 方法的使用。在這個程式中,我們建立了一個字串陣列,並將 explorer.exe 新增到其中。使用該列表,我們初始化了一個 ProcessBuilder 例項。現在使用 directory() 方法,我們列印了底層目錄的詳細資訊。

package com.tutorialspoint;

public class ProcessBuilderDemo {

   public static void main(String[] args) {

      // create a new list of arguments for our process
      String[] list = {"explorer.exe"};

      // create the process builder
      ProcessBuilder pb = new ProcessBuilder(list);

      // get the working directory of the process
      System.out.println(pb.directory());
   }
}

輸出

讓我們編譯並執行上面的程式,這將產生以下結果:

null
java_lang_processbuilder.htm
廣告