java读取项目资源文件的方法
生活随笔
收集整理的這篇文章主要介紹了
java读取项目资源文件的方法
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
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;
}
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;
}
轉(zhuǎn)載于:https://www.cnblogs.com/YuyuanNo1/p/9184129.html
總結(jié)
以上是生活随笔為你收集整理的java读取项目资源文件的方法的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: MySql的安装、配置(转)
- 下一篇: 管人、管团队、管项目,如何让团队管理者成