The general way to read a Resource file in Spring is something like this
However, this will cache the contents of the file (stream) in the class loader. Although this is not a major problem, there may be cases where reading the file fresh everytime (Hot deployable file) is required.
That can be achieved with the following code
This will gives cache free control on the file and its content.
(Important) Note:
* Doing the AbsoluteFile way, will always read the content of the file. So if it is being done on a High frequency, A Managed Caching layer must be considered
* AbsoluteFile call will NOT work for resource files inside jars, they have to be in classpath folders (e.g.WEB-INF/classes)
new ClassPathResource(pathToFile).getInputStream()
However, this will cache the contents of the file (stream) in the class loader. Although this is not a major problem, there may be cases where reading the file fresh everytime (Hot deployable file) is required.
That can be achieved with the following code
new FileInputStream(new ClassPathResource(templateFile) .getFile().getAbsoluteFile());
This will gives cache free control on the file and its content.
(Important) Note:
* Doing the AbsoluteFile way, will always read the content of the file. So if it is being done on a High frequency, A Managed Caching layer must be considered
* AbsoluteFile call will NOT work for resource files inside jars, they have to be in classpath folders (e.g.WEB-INF/classes)