1.在SpringBoot项目的resources目录下新建一个data.properties文件,内容:
name=zhangsan
age=18
#中国北京 base64编码后为:5Lit5Zu95YyX5Lqs
address=5Lit5Zu95YyX5Lqs
如果内容是中文的“中国北京”,则需要base64编码
2.准备读取文件
@Component
public class PropertiesReader {
private final Resource resource;
public PropertiesReader(ResourceLoader resourceLoader) {
this.resource = resourceLoader.getResource("classpath:data.properties");
}
public Properties loadProperties() throws IOException {
return PropertiesLoaderUtils.loadProperties(resource);
}
}
3.获取文件中的name参数
import cn.hutool.core.codec.Base64;
import java.util.Properties;
@Autowired
private PropertiesReader propertiesReader;
public String getData() throws IOException {
Properties properties = propertiesReader.loadProperties();
String data = Base64.decodeStr(properties.getProperty("data"));
return data;
}