val hdfs : FileSystem = FileSystem.get(newConfiguration) val path=newPath(bmNBPath) if(hdfs.exists(path)) { if(hdfs.isDirectory(path)) hdfs.delete(path, true) //true代表递归删除,当path是目录时使用 else hdfs.delete(path,false) //非递归删除, }
原型:
1 2 3 4 5 6 7 8 9 10 11
publicabstractbooleandelete(Path f, boolean recursive) throws IOException Delete a file. Parameters: f - the path to delete. recursive - if path is a directory and set to true, the directory is deleted elsethrows an exception. In case of a file the recursive can be set to either true or false. Returns: trueif delete is successful elsefalse. Throws: IOException
exists操作:检查路径是否存在
原型:
1 2 3 4 5 6 7 8 9
publicbooleanexists(Path f) throws IOException Check if a path exists. It is highly discouraged to call this method back to back with other getFileStatus(Path) calls, as this will involve multiple redundant RPC calls in HDFS. Parameters: f - source path Returns: trueif the path exists Throws: IOException - IO failure