简体   繁体   中英

Access a Property file outside the classpath

I need to access property file outside the class path . The file path will be in windows C:\\Temp\\remote.txt or in linux /tmp/remote.txt . How can my spring solution can access this file and read the content when starting up the application server.

How can I do this with Spring PropertyPlaceholderConfigurer or with any other mechanism in my spring hibernate application

一个例子:

<context:property-placeholder location="file:/E:/Workspace/123.properties" />

PropertyPlaceholderConfigurer

As of Spring 3.1, PropertySourcesPlaceholderConfigurer should be used preferentially over this implementation; it is more flexible through taking advantage of the Environment and PropertySource mechanisms also made available in Spring 3.1.

Using a PropertySourcesPlaceholderConfigurer you can load properties from any path on the filesystem with:

PropertySourcesPlaceholderConfigurer configurer = new PropertySourcesPlaceholderConfigurer();

configurer.setLocations(new Resource[] {
    new FileSystemResource(fileName),
});

you can use get resource as stream like:

String filePath = "C:/Temp/remote.txt";

BufferedReader input = new BufferedReader(new InputStreamReader(this.getClass().getResourceAsStream(filePath)));

I hope it helps

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