Syntax:
句法:
public File directory ();
public ProcessBuilder directory (File dir);
ProcessBuilder类directory()方法 (ProcessBuilder Class directory() method)
directory() method is available in java.lang package.
directory()方法在java.lang包中可用。
directory() method is used to return the working directory of this process builder. If it returns null to indicate the current working directory of the current process so the name of the directory will be assigned by using system property "user.dir" assign.
directory()方法用于返回此流程生成器的工作目录。 如果返回null指示当前进程的当前工作目录,那么将使用系统属性“ user.dir” assign来分配目录名称。
directory(File dir) method is used to return the working directory of this process builder. If it sets argument null to indicate the current working directory of the current process so the name of the directory will be assigned by using system property "user.dir".
directory(File dir)方法用于返回此流程生成器的工作目录。 如果将参数null设置为指示当前进程的当前工作目录,那么将使用系统属性“ user.dir”来分配目录的名称。
These methods don't throw an exception at the time of returning working directory of this process builder.
在返回此流程生成器的工作目录时,这些方法不会引发异常。
These are non-static methods, it is accessible with the class object only and, if we try to access these methods with the class name then we will get an error.
这些是非静态方法,只能通过类对象访问,如果尝试使用类名称访问这些方法,则会收到错误消息。
Parameter(s):
参数:
In the first case, this method accepts none parameters.
在第一种情况下,此方法不接受任何参数。
In the second case, File dir - This parameter represents the new working directory.
在第二种情况下, File dir-此参数表示新的工作目录。
Return value:
返回值:
In the first case, the return type of the method is File directory() – This parameter represents the working directory of this process builder.
在第一种情况下,方法的返回类型为File directory() –此参数表示此流程构建器的工作目录。
In the second case, the return type of the method is ProcessBuilder, it returns this process builder.
在第二种情况下,方法的返回类型为ProcessBuilder ,它将返回此流程生成器。
Example:
例:
// Java program to demonstrate the example
// of directory () method of ProcessBuilder class
import java.io.*;
import java.util.*;
public class Directory {
public static void main(String[] args) throws Exception {
// Creating an object of File and List
File fi = new File("E://Programs");
List l = new LinkedList();
// By using add() method to add elements
l.add("TextPad.exe");
l.add("notepad.exe");
// Instantiating ProcessBuilder object
ProcessBuilder pr_bu = new ProcessBuilder(l);
// By using directory() method is to return the working directory
System.out.println("pr_bu.directory() = " + pr_bu.directory());
// By using directory(File dir) method is to set the path of
// the working directory
pr_bu.directory(fi);
System.out.println("pr_bu.directory(fi) = " + pr_bu.directory());
}
}
Output
输出量
pr_bu.directory() = null
pr_bu.directory(fi) = E:/Programs
翻译自: https://www.includehelp.com/java/processbuilder-directory-method-with-example.aspx