dermoritz
dermoritz

Reputation: 13038

How to split to files and add number to output file names (not overwrite)

I have a route that splits large files into smaller ones. All split files should be written to same directory. The default behavior of file endpoint (producer) seem to overwrite always. Is there an easy way to let the file producer do a simple rename by adding a counter to file name? Like in OS if you do copy/paste on a file and OS does auto rename by adding (copy-1...).

Upvotes: 0

Views: 1433

Answers (1)

ltsallas
ltsallas

Reputation: 1948

You can use the spliter CamelSplitIndex property and add the split index using a simple expression in the fileName option of the producer like in this route below.

 from("file:data/inbox?")
    .split(body(String.class).tokenize("\n"))
    .to("file:data/outbox/?fileName=${file:name.noext}-${property.CamelSplitIndex}.csv");

So for a 4 line input csv test.csv, the produced output would be

test-0.csv test-1.csv test-2.csv test-3.csv

For details regarding the split index take a look at https://camel.apache.org/splitter.html

and for simple expressions in https://camel.apache.org/simple.html

Upvotes: 1

Related Questions