Springboot 項(xiàng)目讀取Resources目錄下的文件(推薦)
需求描述:企業(yè)開發(fā)過程中,經(jīng)常需要將一些靜態(tài)文本數(shù)據(jù)放到Resources目錄下,項(xiàng)目啟動(dòng)時(shí)或者程序運(yùn)行中,需要讀取這些文件。
讀取Resources目錄下文件的方法
/** * @Description: 讀取resources 目錄下的文件 * @Author: ljj * @CreateDate: 2020/11/3 17:20 * @UpdateUser: * @UpdateDate: * @UpdateReakem * @param filePath * @Return: java.lang.String **/ public static String getContent(String filePath){ String res = ''; if(StringUtils.isEmpty(filePath)){ log.info('文件路徑不能為空'); return res; } try { Resource resource = new ClassPathResource(filePath); BufferedReader br = new BufferedReader(new InputStreamReader(resource.getInputStream(),'UTF-8')); StringBuffer sb = new StringBuffer(); String str = ''; while((str=br.readLine())!=null) {sb.append(str); } res = sb.toString(); } catch (Exception e) { log.info('讀取文件{}時(shí)發(fā)生異常',filePath); e.printStackTrace(); } return res; }
需要調(diào)用時(shí):
String Content = FileUtils.getContent('testData/網(wǎng)元拓?fù)?.json');

注意:testData 路徑前面沒有'/'
到此這篇關(guān)于Springboot 項(xiàng)目讀取Resources目錄下的文件的文章就介紹到這了,更多相關(guān)Springboot讀取Resources文件內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. php模擬實(shí)現(xiàn)斗地主發(fā)牌2. Python random庫使用方法及異常處理方案3. 理解PHP5中static和const關(guān)鍵字4. spring acegi security 1.0.0 發(fā)布5. Vue封裝一個(gè)TodoList的案例與瀏覽器本地緩存的應(yīng)用實(shí)現(xiàn)6. Docker 容器健康檢查機(jī)制7. jQuery 實(shí)現(xiàn)DOM元素拖拽交換位置的實(shí)例代碼8. Vuex localStorage的具體使用9. vue 使用localstorage實(shí)現(xiàn)面包屑的操作10. MyBatis中的JdbcType映射使用詳解

網(wǎng)公網(wǎng)安備