I would like to use java.nio.files.Path in a class I'm creating called "DocumentGenerator" but I'm not sure how to instantiate and initialize it when using a constructor that does not pass in another Path object. Here are my class variables and two constructors:
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 =
}
In the second constructor, savePath and fileName need some manipulation before I put them in my Paths object so I do not want to pass them into it just yet. Instead, I would like to try instantiating and initializing "file" to keep in line with good programming practice. My issue is that according to This Question, Path has no constructor. What would be good programming practice in a case like this? Can it be instantiated and initialized in my constructor without a given path?
My question is not "How do I use java.nio.files.Path?", I can find that from the Java API.
Edit : You don't have to instantiate every attributes of your object within your constructors, if you don't instantiate them they will be equal to null.
To create a new nio Path object :
import java.nio.file.Path;
import java.nio.file.Paths;
Path p = Paths.get("/tmp/myfile");