{"record":{"id":"c070c03ced0f1540","repo":"spring-projects/spring-ai","slug":"file-already-exists","errorCode":null,"errorMessage":"File already exists: ","messagePattern":"File already exists: ","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"spring-ai-vector-store/src/main/java/org/springframework/ai/vectorstore/SimpleVectorStore.java","lineNumber":185,"sourceCode":"\t\treturn document -> this.filterExpressionEvaluator.evaluate(filterExpression, document.getMetadata());\n\t}\n\n\t/**\n\t * Serialize the vector store content into a file in JSON format.\n\t * @param file the file to save the vector store content\n\t */\n\tpublic void save(File file) {\n\t\tString json = getVectorDbAsJson();\n\t\ttry {\n\t\t\tif (!file.exists()) {\n\t\t\t\tif (logger.isInfoEnabled()) {\n\t\t\t\t\tlogger.info(\"Creating new vector store file: \" + file);\n\t\t\t\t}\n\t\t\t\ttry {\n\t\t\t\t\tFiles.createFile(file.toPath());\n\t\t\t\t}\n\t\t\t\tcatch (FileAlreadyExistsException e) {\n\t\t\t\t\tthrow new RuntimeException(\"File already exists: \" + file, e);\n\t\t\t\t}\n\t\t\t\tcatch (IOException e) {\n\t\t\t\t\tthrow new RuntimeException(\"Failed to create new file: \" + file + \". Reason: \" + e.getMessage(), e);\n\t\t\t\t}\n\t\t\t}\n\t\t\telse if (logger.isInfoEnabled()) {\n\t\t\t\tlogger.info(\"Overwriting existing vector store file: \" + file);\n\t\t\t}\n\t\t\ttry (OutputStream stream = new FileOutputStream(file);\n\t\t\t\t\tWriter writer = new OutputStreamWriter(stream, StandardCharsets.UTF_8)) {\n\t\t\t\twriter.write(json);\n\t\t\t\twriter.flush();\n\t\t\t}\n\t\t}\n\t\tcatch (IOException ex) {\n\t\t\tlogger.error(\"IOException occurred while saving vector store file.\", ex);\n\t\t\tthrow new RuntimeException(ex);\n\t\t}","sourceCodeStart":167,"sourceCodeEnd":203,"githubUrl":"https://github.com/spring-projects/spring-ai/blob/98a7beda4f29d80a71c5837eb4053b03a93a46f7/spring-ai-vector-store/src/main/java/org/springframework/ai/vectorstore/SimpleVectorStore.java#L167-L203","documentation":"SimpleVectorStore.save() wraps java.nio.file.FileAlreadyExistsException in a RuntimeException when it tries to create the persistence file with Files.createFile() and the file is already on disk. This happens only on the 'create new file' path, i.e. when the store believes the file should not exist yet.","triggerScenarios":"Calling persist() on a SimpleVectorStore whose persistence file was configured but already exists, while the code path takes the Files.createFile() branch (file absent check raced or was bypassed).","commonSituations":"Two store instances sharing the same persistence path; a previous run crashed after creating the file; restarting an application without clearing its data directory.","solutions":["Delete the existing file or point the store at a new path before persisting.","Check Files.exists(path) before calling persist and handle reuse explicitly.","Wrap persist() in try-catch for RuntimeException and recover by removing the file and retrying."],"exampleFix":"// before\nvectorStore.save(file);\n\n// after\nPath path = file.toPath();\nif (Files.exists(path)) {\n    Files.delete(path);\n}\nvectorStore.save(file);","handlingStrategy":"validation","validationCode":"Path path = file.toPath();\nif (Files.exists(path)) { Files.delete(path); }\nvectorStore.save(file);","typeGuard":null,"tryCatchPattern":"try {\n    vectorStore.save(file);\n} catch (RuntimeException e) {\n    if (e.getCause() instanceof FileAlreadyExistsException) {\n        Files.delete(file.toPath());\n        vectorStore.save(file);\n    } else { throw e; }\n}","preventionTips":["Give each store instance a unique persistence path.","Clean the data directory on startup if rebuilding.","Avoid running two writers against the same file."],"tags":["file-io","file-already-exists","vector-store"],"backgroundTag":"file-already-exists","analyzedSha":"98a7beda4f29d80a71c5837eb4053b03a93a46f7","analyzedAt":"2026-09-11T14:15:49.441Z","contentChangedAt":"2026-09-11T14:15:49.441Z","schemaVersion":2},"datasetVersion":"2026-09-14T11:17:12.474Z"}