ClassPathResource读取classpath路径下的⽂件内容
在做项⽬的过程中,需要将⼀些参数写⼊properties⽂件的配置中,如何读取到properties的⽂件内容呢,我⽤到了spring core提供的类io.ClassPathResource,通过这个类,可以读取到指定classpath下路径的⽂件内容。
⽤来读取properties配置的⼯具类如下所⽰:
ificate.properties;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.Properties;
import io.ClassPathResource;
/**
* 读取properties⽂件的配置
* @author xuhaojin
* @version [版本号, 2020年3⽉23⽇]
*/
public class PropertiesReader {
public static String getConfig(String pathInDemo, String key) throws IOException {
return getProperties(pathInDemo).getProperty(key);
}
public static Properties getProperties(String pathInDemo) throws IOException {
Properties properties = new Properties();
ClassPathResource classPathResource = new ClassPathResource(pathInDemo);
File file = File();
BufferedReader bufferedReader = null;
try {
bufferedReader = new BufferedReader(new FileReader(file));
properties.load(bufferedReader);
} catch (IOException e) {
e.printStackTrace();
}
return properties;
}
public static void main(String[] args) throws IOException {
System.out.println("libreoffice.path=" + getConfig("config\\certificate.properties", "libreoffice.path"));
System.out.println(
spring怎么读取properties"certificate.image.suffix=" + getConfig("config\\certificate.properties", "certificate.image.suffix"));
}
}
其中下⾯这两⾏代码获取到配置⽂件:
ClassPathResource classPathResource = new ClassPathResource(pathInDemo);
File file = File()
配置⽂件的实际路径在src/test/resources的config⽂件夹下:

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。