I'm writing a Java class to create a HDFS directory, but want to check whether the directory exists before creating the directory. I'm not sure whether to use Path.getFileSystem() or FileSystem.get():
Configuration conf = new Configuration();
Path path = new Path(args[1]);
FileSystem fs = path.getFileSystem(conf);
if(fs.exists(path)) {
System.err.println("Dir already exists");
}
boolean status = fs.mkdirs(path);
Or should I use the FileSystem.get() method:
Configuration conf = new Configuration();
Path path = new Path(args[1]);
FileSystem fs = FileSystem.get(conf);
boolean status = fs.mkdirs(path);
if(!status) {
System.err.println("Dir already exists");
}
When is it appropriate to use either of these: Path.getFileSystem() or FileSystem.get()?
CodePudding user response:
It depends if you already have a non-null Path
object.
The null-safe approach would be to use a Configuration
object with static method FileSystem.get(conf)