Java ProcessBuilder directory()方法及示例

本文详细介绍了Java中ProcessBuilder类的directory()方法,该方法用于获取和设置流程生成器的工作目录。文章解释了如何使用directory()方法返回当前工作目录,以及如何使用directory(Filedir)方法设置新的工作目录。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值