java读取项目资源文件的方法

1、把资源文件放在项目的Resource文件夹下,并使其设置成为资源文件夹(通过idea或者eclise)
2、
Thread.currentThread().getContextClassLoader().getResourceAsStream("")方法。具体如下:

public static String getGeoJson(String fileName) {
BufferedReader reader = null;
String laststr = "";
try {
InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream("geo/" + fileName);
InputStreamReader inputStreamReader = new InputStreamReader(is, "UTF-8");
reader = new BufferedReader(inputStreamReader);
String tempString = null;
while ((tempString = reader.readLine()) != null) {
laststr += tempString;
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return laststr;
}

posted on 2018-06-14 17:33  小甜瓜安东泥  阅读(566)  评论(0编辑  收藏  举报