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;
}

猜你喜欢

转载自www.cnblogs.com/YuyuanNo1/p/9184129.html