实例化和初始化 java.nio.files.Path?

Instantiating and Initializing java.nio.files.Path?

我想在 class 中使用 java.nio.files.Path 我正在创建名为 "DocumentGenerator" 但我不确定如何在使用构造函数时实例化和初始化它不要传入另一个 Path 对象。这是我的 class 变量和两个构造函数:

private ArrayList<String> totalOutput;
private ArrayList<String> programInput;
private Scanner in;
private String savePath, fileName;
private Path file;

public DocumentGenerator(Path file) {
    this.programInput = new ArrayList<String>();
    this.totalOutput = new ArrayList<String>();
    this.in = new Scanner(System.in);
    this.file = file;
    this.savePath = "";
    this.fileName = "";
}

public DocumentGenerator(String savePath, String fileName) {
    this.programInput = new ArrayList<String>();
    this.totalOutput = new ArrayList<String>();
    this.in = new Scanner(System.in);
    this.savePath = savePath;
    this.fileName = fileName;
    this.file = 
}

在第二个构造函数中,savePath 和 fileName 在我将它们放入我的 Paths 对象之前需要一些操作,所以我现在不想将它们传递给它。相反,我想尝试实例化和初始化 "file" 以符合良好的编程习惯。我的问题是,根据 ,Path 没有构造函数。在这种情况下,好的编程习惯是什么? 可以它可以在我的构造函数中实例化和初始化没有给定路径吗?

我的问题是不是"How do I use java.nio.files.Path?",我可以从Java API.

中找到

编辑: 您不必在构造函数中实例化对象的每个属性,如果您不实例化它们,它们将等于 null。

创建一个新的 nio Path 对象:

import java.nio.file.Path;
import java.nio.file.Paths;

Path p = Paths.get("/tmp/myfile");