TextReader is a Spring AI document reader for plain-text files. It reads a configured Resource and returns document objects that can enter a retrieval-augmented generation pipeline. section 4.3 section 4.3.3
Load and split a text file
A basic pipeline constructs TextReader with my-document.txt, obtains its documents, splits them with TokenTextSplitter, and passes the resulting chunks to a VectorStore. TextReader produces a single-entry List<Document> before splitting. section 4.3
Use TextReader when:
- The source is a plain-text file and a simple document reader is sufficient. section 4.3.3
- The resulting documents will be split into chunks before storage or downstream processing. section 4.3
- Another reader is needed for JSON, PDF, or other formats; Spring AI also provides JsonReader and PDF readers. section 4.3.3
@Value("file://${HOME}/documents/my-document.txt")
private Resource documentResource;
public void loadDocument(VectorStore vectorStore) {
DocumentReader reader = new TextReader(documentResource);
TextSplitter splitter = TokenTextSplitter.builder().build();
vectorStore.accept(splitter.apply(reader.get()));
}
Inspect the minimal Java pipeline from Resource through TextReader and TokenTextSplitter to VectorStore. section 4.3
