spring-projects/spring-ai · error · RuntimeException

File already exists:

Error message

File already exists: 

What it means

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.

Source

Thrown at spring-ai-vector-store/src/main/java/org/springframework/ai/vectorstore/SimpleVectorStore.java:185

		return document -> this.filterExpressionEvaluator.evaluate(filterExpression, document.getMetadata());
	}

	/**
	 * Serialize the vector store content into a file in JSON format.
	 * @param file the file to save the vector store content
	 */
	public void save(File file) {
		String json = getVectorDbAsJson();
		try {
			if (!file.exists()) {
				if (logger.isInfoEnabled()) {
					logger.info("Creating new vector store file: " + file);
				}
				try {
					Files.createFile(file.toPath());
				}
				catch (FileAlreadyExistsException e) {
					throw new RuntimeException("File already exists: " + file, e);
				}
				catch (IOException e) {
					throw new RuntimeException("Failed to create new file: " + file + ". Reason: " + e.getMessage(), e);
				}
			}
			else if (logger.isInfoEnabled()) {
				logger.info("Overwriting existing vector store file: " + file);
			}
			try (OutputStream stream = new FileOutputStream(file);
					Writer writer = new OutputStreamWriter(stream, StandardCharsets.UTF_8)) {
				writer.write(json);
				writer.flush();
			}
		}
		catch (IOException ex) {
			logger.error("IOException occurred while saving vector store file.", ex);
			throw new RuntimeException(ex);
		}

View on GitHub (pinned to 98a7beda4f)

Solutions

  1. Delete the existing file or point the store at a new path before persisting.
  2. Check Files.exists(path) before calling persist and handle reuse explicitly.
  3. Wrap persist() in try-catch for RuntimeException and recover by removing the file and retrying.

Example fix

// before
vectorStore.save(file);

// after
Path path = file.toPath();
if (Files.exists(path)) {
    Files.delete(path);
}
vectorStore.save(file);
Defensive patterns

Strategy: validation

Validate before calling

Path path = file.toPath();
if (Files.exists(path)) { Files.delete(path); }
vectorStore.save(file);

Try / catch

try {
    vectorStore.save(file);
} catch (RuntimeException e) {
    if (e.getCause() instanceof FileAlreadyExistsException) {
        Files.delete(file.toPath());
        vectorStore.save(file);
    } else { throw e; }
}

Prevention

When it happens

Trigger: 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).

Common situations: Two store instances sharing the same persistence path; a previous run crashed after creating the file; restarting an application without clearing its data directory.

Understand the failure class

Background: "already exists" / EEXIST / FileAlreadyExistsException: what the 'file already exists' error means and how to fix it — this error's family across 37 libraries.

Related errors


AI-assisted analysis of spring-projects/spring-ai@98a7beda4f (2026-09-11). Data as JSON: /api/errors/c070c03ced0f1540. Report an issue: GitHub.