{"record":{"id":"77e48a48106366a4","repo":"laurent22/joplin","slug":"a-file-or-directory-already-exist-at-uri","errorCode":null,"errorMessage":"a file or directory already exist at: ${uri}","messagePattern":"a file or directory already exist at: (.+?)","errorType":"exception","errorClass":"IOExceptionFast","httpStatus":null,"severity":"error","filePath":"packages/react-native-saf-x/android/src/main/java/com/reactnativesafx/utils/EfficientDocumentHelper.java","lineNumber":401,"sourceCode":"    if (!renameResult.equals(newName)) {\n      throw new RenameFailedException(uri, newName, newUri, renameResult);\n    }\n    return newUri;\n  }\n\n  private Uri createFile(\n    final String unknownStr, final String mimeType, final boolean replaceIfDestinationExists\n  ) throws IOException {\n    Uri uri = null;\n\n    try {\n      uri = getDocumentUri(unknownStr, false, true);\n    } catch (FileNotFoundException ignored) {\n    }\n\n    if (uri != null) {\n      if (replaceIfDestinationExists) return uri;\n      throw new IOExceptionFast(\"a file or directory already exist at: \" + uri);\n    }\n\n    uri = UriHelper.getUnifiedUri(unknownStr);\n\n    if (uri.getScheme().equals(ContentResolver.SCHEME_FILE)) {\n      File file = new File(uri.getPath());\n      file.getParentFile().mkdirs();\n      boolean created = file.createNewFile();\n      if (!created) {\n        if (replaceIfDestinationExists && file.exists() && file.isFile()) return uri;\n        throw new IOExceptionFast(\"could not create file at: \" + unknownStr);\n      }\n      return uri;\n    }\n\n    Uri parentDirOfFile = getDocumentUri(unknownStr, true, false);\n\n    String fileName = UriHelper.getFileName(unknownStr);","sourceCodeStart":383,"sourceCodeEnd":419,"githubUrl":"https://github.com/laurent22/joplin/blob/981a03c5c9e88130bccff4db47c411d35ec7ae2c/packages/react-native-saf-x/android/src/main/java/com/reactnativesafx/utils/EfficientDocumentHelper.java#L383-L419","documentation":"EfficientDocumentHelper.createFile (react-native-saf-x) throws \"a file or directory already exist at: <uri>\" when the target SAF/document URI already resolves and the caller did not pass replaceIfDestinationExists. The helper first checks whether a document already exists at the target and refuses to overwrite it unless replacement is requested.","triggerScenarios":"Calling createFile (via the JS module's createFile) with a target URI that already exists while replaceIfDestinationExists is false.","commonSituations":"Writing a file to a folder where a same-named file exists; retrying an export after a previous partial success; users re-running a save-to-SD operation without unique filenames.","solutions":["Pass replaceIfDestinationExists: true in the createFile options to overwrite.","Generate a unique target filename (e.g. append timestamp) before creating.","Delete or move the existing document first, then create the file."],"exampleFix":"// before\nawait SafX.createFile(treeUri, 'notes.json', 'text/json');\n// after\nawait SafX.createFile(treeUri, 'notes.json', 'text/json', { replaceIfDestinationExists: true });","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try { await SafX.createFile(treeUri, name, mime); } catch (e) { if (String(e.message).includes('already exist')) await SafX.createFile(treeUri, name, mime, { replaceIfDestinationExists: true }); }","preventionTips":["Default to replaceIfDestinationExists: true for export/overwrite workflows.","Use unique filenames (timestamp/uuid) for append-style exports."],"tags":["android","saf","filesystem"],"backgroundTag":"file-already-exists","analyzedSha":"981a03c5c9e88130bccff4db47c411d35ec7ae2c","analyzedAt":"2026-09-17T14:49:40.960Z","contentChangedAt":"2026-09-17T14:49:40.960Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}