{"record":{"id":"23e20bc09239898c","repo":"apache/hadoop","slug":"file-already-exists-file","errorCode":null,"errorMessage":"File already exists: {file}","messagePattern":"File already exists: (.+?)","errorType":"exception","errorClass":"FileAlreadyExistsException","httpStatus":null,"severity":"error","filePath":"hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/fs/ftp/FTPFileSystem.java","lineNumber":330,"sourceCode":"  @Override\n  public FSDataOutputStream create(Path file, FsPermission permission,\n      boolean overwrite, int bufferSize, short replication, long blockSize,\n      Progressable progress) throws IOException {\n    final FTPClient client = connect();\n    Path workDir = new Path(client.printWorkingDirectory());\n    Path absolute = makeAbsolute(workDir, file);\n    FileStatus status;\n    try {\n      status = getFileStatus(client, file);\n    } catch (FileNotFoundException fnfe) {\n      status = null;\n    }\n    if (status != null) {\n      if (overwrite && !status.isDirectory()) {\n        delete(client, file, false);\n      } else {\n        disconnect(client);\n        throw new FileAlreadyExistsException(\"File already exists: \" + file);\n      }\n    }\n    \n    Path parent = absolute.getParent();\n    if (parent == null || !mkdirs(client, parent, FsPermission.getDirDefault())) {\n      parent = (parent == null) ? new Path(\"/\") : parent;\n      disconnect(client);\n      throw new IOException(\"create(): Mkdirs failed to create: \" + parent);\n    }\n    client.allocate(bufferSize);\n    // Change to parent directory on the server. Only then can we write to the\n    // file on the server by opening up an OutputStream. As a side effect the\n    // working directory on the server is changed to the parent directory of the\n    // file. The FTP client connection is closed when close() is called on the\n    // FSDataOutputStream.\n    client.changeWorkingDirectory(parent.toUri().getPath());\n    OutputStream outputStream = client.storeFileStream(file.getName());\n","sourceCodeStart":312,"sourceCodeEnd":348,"githubUrl":"https://github.com/apache/hadoop/blob/2add9630210752f88ceb1bb74eb65e37bf41da8e/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/fs/ftp/FTPFileSystem.java#L312-L348","documentation":"create() stats the target first. If something exists there and either overwrite is false or the existing entry is a directory (overwrite only deletes files), FileAlreadyExistsException is thrown after disconnecting the client. Directories are never overwritten by create().","triggerScenarios":"fs.create(path) or fs.create(path, false) on an existing file; fs.create(path, true) where the path is an existing directory; two writers racing to create the same output path.","commonSituations":"Job re-runs without cleaning output; part files left by a failed attempt; the overwrite flag dropped when wrapping create calls; a directory accidentally created at the intended file path.","solutions":["Pass overwrite=true when re-writing is intended: fs.create(path, true)","If the target is a directory, delete it first: fs.delete(path, true)","Catch FileAlreadyExistsException to implement idempotent create-replace (delete then retry once)","Clean output paths in job setup instead of relying on create failing"],"exampleFix":"// before\nFSDataOutputStream out = fs.create(path, false);\n// FileAlreadyExistsException\n\n// after\ntry {\n  out = fs.create(path, true);\n} catch (FileAlreadyExistsException e) {\n  fs.delete(path, false);\n  out = fs.create(path, true);\n}","handlingStrategy":"try-catch","validationCode":"if (fs.exists(path) && !overwrite) {\n  // decide before create(): fail, skip, or delete\n  fs.delete(path, false);\n}\nFSDataOutputStream out = fs.create(path, true);","typeGuard":null,"tryCatchPattern":"try {\n  out = fs.create(path, true);\n} catch (FileAlreadyExistsException e) {\n  // idempotent create: clear the conflicting entry (file or dir) and retry once\n  fs.delete(path, true);\n  out = fs.create(path, true);\n}","preventionTips":["Pass overwrite=true explicitly whenever re-running jobs is expected","Clean output paths in job initialization instead of relying on create() to fail","Remember overwrite never replaces an existing directory — delete it explicitly"],"tags":["ftp","file-already-exists","create"],"backgroundTag":"file-already-exists","analyzedSha":"2add9630210752f88ceb1bb74eb65e37bf41da8e","analyzedAt":"2026-08-22T19:55:07.957Z","schemaVersion":2},"datasetVersion":"2026-08-23T01:17:44.959Z"}