简体   繁体   中英

How to check if a file exists in Apache Camel?

I have the next code:

package camelinaction;

import org.apache.camel.CamelContext;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.impl.DefaultCamelContext;


public class MakeNotaBeneFile {

    public static void main(String args[]) throws Exception {
        // create CamelContext
        CamelContext context = new DefaultCamelContext();

        // add our route to the CamelContext
        context.addRoutes(new RouteBuilder() {
            public void configure() {
                 from("quartz://report?cron=0/2+*+*+*+*+?")
                 .setBody().simple("\n")
                 .to("file:/c:/Users/Mishin/Documents/work_line/?fileName=${date:now:ddMMyyyy}/${date:now:ddMMyyyy}_nb.txt");
            }
        });

        // start the route and let it do its work
        context.start();
        Thread.sleep(1000);

        // stop the CamelContext
        context.stop();
    }
}

I have a code that creates a directory and a file with the current date with Camel MakeNotaBeneFile.java

My question is that how can I check if my file exists? Because current code overwrites the old file and I lost my notes.

Please see the fileExist option for the camel file component under "Producer" options in camel file2 documentation .

I believe something like &fileExist=Append (or Fail ) might help you.

Also look if camel managed to rename the file that got overwritten (documentation says that camel 2.11.1 and up has this behaviour).

Alternatively if you wanted to build a logical if in your camel route, you could always build a bean containing some code like this: return new File(filename).exists();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM