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)文章
淺談spring使用策略模式實(shí)現(xiàn)多種場(chǎng)景登錄方式
本文主要介紹了spring使用策略模式實(shí)現(xiàn)多種場(chǎng)景登錄方式,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-12-12
Mybatis-plus對(duì)單表操作的封裝實(shí)現(xiàn)
本文詳細(xì)介紹了MyBatis-Plus單表操作,包括自定義SQL、邏輯刪除、樂(lè)觀鎖、全局?jǐn)r截器和代碼生成器等,具有一定的參考價(jià)值,感興趣的可以了解一下2024-12-12
詳解Springboot 優(yōu)雅停止服務(wù)的幾種方法
這篇文章主要介紹了詳解Springboot 優(yōu)雅停止服務(wù)的幾種方法 ,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08
啟動(dòng)SpringBoot報(bào)錯(cuò)Input length = 1問(wèn)題及解決
這篇文章主要介紹了啟動(dòng)SpringBoot報(bào)錯(cuò)Input length = 1問(wèn)題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-05-05
Java多線程編程中易混淆的3個(gè)關(guān)鍵字總結(jié)
這篇文章主要介紹了Java多線程編程中易混淆的3個(gè)關(guān)鍵字總結(jié),本文總結(jié)了、volatile、ThreadLocal、synchronized等3個(gè)關(guān)鍵字,對(duì)這幾個(gè)容易混淆概念的關(guān)鍵字分別做了講解,需要的朋友可以參考下2015-03-03
簡(jiǎn)單易懂的java8新特性之lambda表達(dá)式知識(shí)總結(jié)
一直想針對(duì)lambda表達(dá)式作一個(gè)總結(jié),借助于這次公司安排的考試作一個(gè)入門(mén)式的總結(jié),對(duì)正在學(xué)習(xí)java的小伙伴們非常有幫助,需要的朋友可以參考下2021-05-05
maven一行命令引入第三方包打包的實(shí)現(xiàn)
在項(xiàng)目開(kāi)發(fā)過(guò)程中,難免會(huì)用到第三方j(luò)ar的時(shí)候,本文主要介紹了maven一行命令引入第三方包打包的實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的可以了解一下2024-01-01

