java的url方式、本地方式獲取json文件內(nèi)容
更新時間:2018年07月19日 08:34:39 作者:飲罷千樽雪已老
這篇文章給大家分享了java的url方式、本地方式獲取json文件內(nèi)容的實例代碼,有需要的朋友參考學(xué)習(xí)下。
因為工作原因需要讀取json文件,最先是使用url方式不符合要求pass。又使用本地方式讀取。記錄一下方便后期查看。
注:因為資料都是從網(wǎng)上摘抄,如有問題請告知我。
1.url方式
/** * 通過網(wǎng)絡(luò)訪問json并讀取文件 * @param url:http://127.0.0.1:80/dashboard/dept_uuid.json * @return:json文件的內(nèi)容 */ public static String loadJson (String url) { StringBuilder json = new StringBuilder(); try { URL urlObject = new URL(url); URLConnection uc = urlObject.openConnection(); BufferedReader in = new BufferedReader(new InputStreamReader(uc.getInputStream(),"UTF-8")); String inputLine = null; while ( (inputLine = in.readLine()) != null) { json.append(inputLine); } in.close(); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return json.toString(); }
2.本地文件讀取
/** * 通過本地文件訪問json并讀取 * @param path:E:/svn/05.Hospital/templatedept_uuid.json * @return:json文件的內(nèi)容 */ public static String ReadFile(String path){ String laststr=""; File file=new File(path);// 打開文件 BufferedReader reader=null; try{ FileInputStream in = new FileInputStream(file); reader=new BufferedReader(new InputStreamReader(in,"UTF-8"));// 讀取文件 String tempString=null; while((tempString=reader.readLine())!=null){ laststr=laststr+tempString; } reader.close(); }catch(IOException e){ e.printStackTrace(); }finally{ if(reader!=null){ try{ reader.close(); }catch(IOException el){ } } } return laststr; }
相關(guān)文章
Java整合mybatis實現(xiàn)過濾數(shù)據(jù)
這篇文章主要介紹了Java整合mybatis實現(xiàn)過濾數(shù)據(jù),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧2023-01-01Maven項目中讀取src/main/resources目錄下的配置文件的方法
本篇文章主要介紹了Maven項目中讀取src/main/resources目錄下的配置文件的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-12-12Java?SpringBoot?獲取接口實現(xiàn)類匯總
這篇文章主要介紹了Java?SpringBoot?獲取接口實現(xiàn)類匯總,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價值,需要的小伙伴可以參考一下2022-09-09SpringCloud搭建netflix-eureka微服務(wù)集群的過程詳解
這篇文章主要介紹了SpringCloud搭建netflix-eureka微服務(wù)集群的過程詳解,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-04-04