方法一:Paths类的get方法
Paths.get里面文件所在目录的格式为:Path path = Paths.get("src","main",.........);
默认文件所在目录为该项目根目录
public String getFIlesContent(String url) throws IOException {
Path path = Paths.get("scripts",url);
byte[] data = Files.readAllBytes(path);
String content = new String(data,"utf-8");
return content;
}
方法二: ResourceBundle类的getBundle方法
ResourceBundle类通常是用于针对不同的语言来使用的属性文件。而如果你的应用程序中的属性文件只是一些配置,并不是针对多国语言的目的。那么使用Properties类就可以了。
## project.properties
username = user
password = 123
public Map<String,String> getParmMap(){
ResourceBundle resourceBundle = ResourceBundle.getBundle("project",new Locale("zh", "CN"));
Map<String,String> map = new HashMap<>();
map.put("username",resourceBundle.getString("username"));
map.put("password",resourceBundle.getString("password"));
return map;
}