java后端开发与服务中的本地文件读取问题
java后端开发态与运行态有着一定的区别。
其中涉及到本地文件读取会出现一系列问题:
1.如何读取本地文件:
开发端可以将数据文件放在idea的resources目录下,通过FileInputStream读取相对路径下的文件。
public String getWeatherNum(String str){
try{
FileInputStream fis = new FileInputStream("src/main/resources/weatherIcons.txt");
List<String> list = IOUtils.readLines(fis);
Map<String, Integer> weatherIcons = new HashMap<>();
for(int i=1; i<list.size(); i++) {
String[] strings = list.get(i).split(",");
weatherIcons.put(strings[1], Integer.parseInt(strings[0]));
}
fis.close();
if (weatherIcons.get(str)==null){
return "-2";
}
return weatherIcons.get(str)<39 ? String.valueOf(weatherIcons.get(str)) : "-3";
} catch