Creating Directories
Function
Process of creating a directory:
- Call the exists method of the FileSystem instance to check whether the directory exists.
- If yes, the method stops.
- If no, call the mkdirs method in the FileSystem instance to create a directory.
Example Codes
The following is a code snippet. For complete codes, see the HdfsExample class in com.huawei.bigdata.hdfs.examples.
/**
* Create a directory.
*
* @throws java.io.IOException
*/
private void mkdir() throws IOException {
Path destPath = new Path(DEST_PATH);
if (!createPath(destPath)) {
LOG.error("failed to create destPath " + DEST_PATH);
return;
}
LOG.info("success to create path " + DEST_PATH);
}
/**
* create file path
*
* @param filePath
* @return
* @throws java.io.IOException
*/
private boolean createPath(final Path filePath) throws IOException {
if (!fSystem.exists(filePath)) {
fSystem.mkdirs(filePath);
}
return true;
}
Feedback
Was this page helpful?
Provide feedbackThank you very much for your feedback. We will continue working to improve the documentation.