Wednesday, May 16, 2012

How can I read a resource relative to the classpath ?


How can I read a resource relative to the classpath ?

You have to get the applicationcontext. I have blogged on this topic.

a) Create a resource that has a reference to a path

example :

    public org.springframework.core.io.Resource getResource() {
        logger.debug("entering getResource");
        org.springframework.core.io.Resource resource = applicationContext.getResource("classpath:telemetry/telemetrieA+Cons20090101_200906112209055.csv");
        logger.debug("leaving getResource");
        return resource;
    }

b) Read the resource

example :

CSVReader reader = new CSVReader(new FileReader(getResource().getFile()));

That's all folks !

No comments:

Post a Comment