Java ProcessBuilder directory() 方法



描述

Java ProcessBuilder directory(File directory) 方法設定此程序生成器的當前工作目錄。隨後由此物件的 start() 方法啟動的子程序將使用此目錄作為其工作目錄。引數可以為 null - 這表示使用當前 Java 程序的工作目錄(通常是由系統屬性 user.dir 指定的目錄)作為子程序的工作目錄。

宣告

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

public ProcessBuilder directory(File directory)

引數

directory - 新的工作目錄

返回值

此方法返回此程序生成器

異常

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

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

package com.tutorialspoint;

import java.io.File;

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

      // set the working directory of the process
      pb.directory(new File("C:/"));
      System.out.println("" + pb.directory());
   }
}

輸出

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

C:\

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

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

package com.tutorialspoint;

import java.io.File;

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

      // set the working directory of the process
      pb.directory(new File("C:/"));
      System.out.println("" + pb.directory());
   }
}

輸出

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

C:\

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

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

package com.tutorialspoint;

import java.io.File;

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

      // set the working directory of the process
      pb.directory(new File("C:/"));
      System.out.println("" + pb.directory());
   }
}

輸出

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

C:\
java_lang_processbuilder.htm
廣告