{"record":{"id":"581f7260d9d27c4f","repo":"apache/hadoop","slug":"file-already-exists-f","errorCode":null,"errorMessage":"File already exists: \" + f","messagePattern":"File already exists: \" \\+ f","errorType":"exception","errorClass":"FileAlreadyExistsException","httpStatus":null,"severity":"error","filePath":"hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/fs/RawLocalFileSystem.java","lineNumber":641,"sourceCode":"    }\n    return new FSDataOutputStream(new BufferedOutputStream(\n        createOutputStreamWithMode(f, true, null), bufferSize), statistics,\n        status.getLen());\n  }\n\n  @Override\n  public FSDataOutputStream create(Path f, boolean overwrite, int bufferSize,\n    short replication, long blockSize, Progressable progress)\n    throws IOException {\n    return create(f, overwrite, true, bufferSize, replication, blockSize,\n        progress, null);\n  }\n\n  private FSDataOutputStream create(Path f, boolean overwrite,\n      boolean createParent, int bufferSize, short replication, long blockSize,\n      Progressable progress, FsPermission permission) throws IOException {\n    if (exists(f) && !overwrite) {\n      throw new FileAlreadyExistsException(\"File already exists: \" + f);\n    }\n    Path parent = f.getParent();\n    if (parent != null && !mkdirs(parent)) {\n      throw new IOException(\"Mkdirs failed to create \" + parent.toString());\n    }\n    return new FSDataOutputStream(new BufferedIOStatisticsOutputStream(\n        createOutputStreamWithMode(f, false, permission), bufferSize, true),\n        statistics);\n  }\n  \n  protected OutputStream createOutputStream(Path f, boolean append) \n      throws IOException {\n    return createOutputStreamWithMode(f, append, null);\n  }\n\n  protected OutputStream createOutputStreamWithMode(Path f, boolean append,\n      FsPermission permission) throws IOException {\n    return new LocalFSFileOutputStream(f, append, permission);","sourceCodeStart":623,"sourceCodeEnd":659,"githubUrl":"https://github.com/apache/hadoop/blob/2add9630210752f88ceb1bb74eb65e37bf41da8e/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/fs/RawLocalFileSystem.java#L623-L659","documentation":"RawLocalFileSystem.create (private overload with createParent) throws FileAlreadyExistsException when the target exists and overwrite is false. This is the standard create-semantics guard across Hadoop filesystems: create() is destructive only when the caller opts in. On the local filesystem the existence check is a direct stat of the underlying file.","triggerScenarios":"Calling fs.create(path, false, ...) (or the short overloads that default overwrite=false) when the local file already exists: re-running a job without cleaning output, writing to a fixed temp filename, or two concurrent tasks writing the same local path.","commonSituations":"Re-running a MapReduce/local pipeline whose previous output was not removed, hard-coded scratch files (/tmp/myapp.dat) left behind by an earlier run, or dev iteration where output directories persist between runs.","solutions":["Pass overwrite=true when clobbering is intended: fs.create(path, true).","Delete the stale target before creating: if (fs.exists(path)) fs.delete(path, false);","For unique outputs use FileContext/FileSystem temp files or names with timestamps/UUIDs instead of fixed names.","If this is MapReduce output, set output.overwrite semantics or clean the output directory in the driver."],"exampleFix":"// before\nFSDataOutputStream out = fs.create(new Path(\"/tmp/report.csv\")); // exists -> throws\n\n// after\nFSDataOutputStream out = fs.create(new Path(\"/tmp/report.csv\"), true);","handlingStrategy":"validation","validationCode":"boolean overwrite = true; // or prompt/clean beforehand\nif (!overwrite && fs.exists(p)) {\n  throw new FileAlreadyExistsException(p + \" exists; delete it or enable overwrite\");\n}\nFSDataOutputStream out = fs.create(p, overwrite);","typeGuard":null,"tryCatchPattern":"try {\n  out = fs.create(p, false);\n} catch (FileAlreadyExistsException e) {\n  // decide policy: clobber, fail, or pick a new name\n  out = fs.create(p, true);\n}","preventionTips":["Make overwrite an explicit, single decision point in your writer.","Use unique output names (timestamps/UUIDs) for scratch files.","Clean previous outputs in the job driver instead of relying on runtime exceptions."],"tags":["hadoop","local-filesystem","create","file-exists","overwrite"],"backgroundTag":"file-already-exists","analyzedSha":"2add9630210752f88ceb1bb74eb65e37bf41da8e","analyzedAt":"2026-08-22T19:55:07.957Z","schemaVersion":2},"datasetVersion":"2026-08-22T20:17:22.307Z"}