java讀取resources文件詳解及實(shí)現(xiàn)代碼
Java項(xiàng)目中,經(jīng)常需要將資源文件打包放在項(xiàng)目中,然后在項(xiàng)目中去讀取對應(yīng)的文件。
實(shí)現(xiàn)代碼:
1
|
String str = ReadFile.read(getClass().getResourceAsStream( "sence/" +file)); |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
public static String read(InputStream inputStream) { BufferedReader reader = null ; String laststr = "" ; try { InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "UTF-8" ); reader = new BufferedReader(inputStreamReader); String tempString = null ; while ((tempString = reader.readLine()) != null ){ laststr += tempString; } } catch (Exception e){ e.printStackTrace(); } finally { if (reader != null ){ try { reader.close(); } catch (IOException e) { e.printStackTrace(); } } } return laststr; } |
如有疑問請留言或者到本站社區(qū)交流討論,本站關(guān)于java的文章很多,還希望大家搜索查閱,感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
原文鏈接:http://www.jianshu.com/p/0596b28f2836