Java實(shí)現(xiàn)讀取項(xiàng)目中文件(.json或.properties)的方法詳解
1. 讀取json file
1.1 Json dependency
<dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.76</version> </dependency>
1.2 字節(jié)流
重點(diǎn)部分
- . /代表同級(jí)目錄
- …/ 代表上級(jí)目錄(兩個(gè)點(diǎn))
- / 代表根目錄
public class FileService { public JSONObject reader(){ JSONObject resultJson = null; String file = "src/main/resources/config_dev.json"; InputStream is = null; try { is = new FileInputStream(file);//操作 byte[] bytes = new byte[5000];//數(shù)組容量超級(jí)大,一次能將中英混合文本全部讀取完 int len = -1; while ((len = is.read(bytes)) != -1) { String str = new String(bytes, 0, len, "UTF-8"); resultJson = process(str); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally {//釋放資源 try { if (null != is) { is.close(); } } catch (IOException e) { e.printStackTrace(); } } return resultJson; } private static JSONObject process(String content) { JSONObject json = JSONObject.parseObject(content); return json; } }
1.3 buffer reader
public static String reader(String filePath) { try { File file = new File(filePath); if (file.isFile() && file.exists()) { InputStreamReader read = new InputStreamReader(new FileInputStream(file), "UTF-8"); BufferedReader bufferedReader = new BufferedReader(read); String lineTxt = bufferedReader.readLine(); while (lineTxt != null) { return lineTxt; } } } catch (UnsupportedEncodingException | FileNotFoundException e) { System.out.println("Cannot find the file specified!"); e.printStackTrace(); } catch (IOException e) { System.out.println("Error reading file content!"); e.printStackTrace(); } return null; }
2. 讀取properties file
public void readPropertiesFile() { Properties pro = new Properties(); InputStream is = this.getClass().getResourceAsStream("/application.properties"); try { pro.load(is); Enumeration en = pro.propertyNames(); while (en.hasMoreElements()) { String key = (String) en.nextElement(); String value = pro.getProperty(key); System.out.println(key + "--" + value); } } catch (IOException e) { throw new RuntimeException(e); } }
3. 好看的css樣式
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <link rel="stylesheet" > <script src="https://qxhut.cn/zb_users/plugin/Jz52_click/click.js"></script> <h1>Click Style</h1> </body> </html>
到此這篇關(guān)于Java實(shí)現(xiàn)讀取項(xiàng)目中文件(.json或.properties)的方法詳解的文章就介紹到這了,更多相關(guān)Java讀取項(xiàng)目文件內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Feign調(diào)用服務(wù)時(shí)丟失Cookie和Header信息的解決方案
這篇文章主要介紹了Feign調(diào)用服務(wù)時(shí)丟失Cookie和Header信息的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03解決SpringCloud Config結(jié)合github無法讀取配置的問題
這篇文章主要介紹了解決SpringCloud Config結(jié)合github無法讀取配置的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2021-02-02Springboot自動(dòng)裝配實(shí)現(xiàn)過程代碼實(shí)例
這篇文章主要介紹了Springboot自動(dòng)裝配實(shí)現(xiàn)過程代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-06-06JavaSE圖像驗(yàn)證碼簡(jiǎn)單識(shí)別程序詳解
這篇文章主要為大家詳細(xì)介紹了JavaSE圖像驗(yàn)證碼簡(jiǎn)單識(shí)別程序,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-01-01EventBus與Spring Event區(qū)別詳解(EventBus 事件機(jī)制,Spring Event事件機(jī)制)
這篇文章主要介紹了EventBus與Spring Event區(qū)別,需要的朋友可以參考下2020-02-02