Updated on 2022-07-11 GMT+08:00

Creating Directories

Function

Process of creating a directory:

  1. Call the exists method of the FileSystem instance to check whether the directory exists.
  2. If yes, the method stops.
  3. 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;
}