亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Java實現(xiàn)讀取項目中文件(.json或.properties)的方法詳解

 更新時間:2023年04月07日 15:35:01   作者:百世經(jīng)綸『一頁書』  
這篇文章主要為大家詳細介紹了Java實現(xiàn)讀取項目中文件的方法,例如.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é)流

重點部分

  • . /代表同級目錄
  • …/ 代表上級目錄(兩個點)
  • / 代表根目錄
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ù)組容量超級大,一次能將中英混合文本全部讀取完
            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實現(xiàn)讀取項目中文件(.json或.properties)的方法詳解的文章就介紹到這了,更多相關(guān)Java讀取項目文件內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Feign調(diào)用服務(wù)時丟失Cookie和Header信息的解決方案

    Feign調(diào)用服務(wù)時丟失Cookie和Header信息的解決方案

    這篇文章主要介紹了Feign調(diào)用服務(wù)時丟失Cookie和Header信息的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-03-03
  • 多線程如何解決for循環(huán)效率的問題

    多線程如何解決for循環(huán)效率的問題

    這篇文章主要介紹了多線程如何解決for循環(huán)效率的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-06-06
  • 解決SpringCloud Config結(jié)合github無法讀取配置的問題

    解決SpringCloud Config結(jié)合github無法讀取配置的問題

    這篇文章主要介紹了解決SpringCloud Config結(jié)合github無法讀取配置的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-02-02
  • Springboot自動裝配實現(xiàn)過程代碼實例

    Springboot自動裝配實現(xiàn)過程代碼實例

    這篇文章主要介紹了Springboot自動裝配實現(xiàn)過程代碼實例,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-06-06
  • java排序算法之選擇排序詳解

    java排序算法之選擇排序詳解

    這篇文章主要為大家詳細介紹了java排序算法之選擇排序,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-05-05
  • JavaSE圖像驗證碼簡單識別程序詳解

    JavaSE圖像驗證碼簡單識別程序詳解

    這篇文章主要為大家詳細介紹了JavaSE圖像驗證碼簡單識別程序,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-01-01
  • 使用Java注解模擬spring ioc容器過程解析

    使用Java注解模擬spring ioc容器過程解析

    這篇文章主要介紹了使用Java注解模擬spring ioc容器過程解析,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-07-07
  • EventBus與Spring Event區(qū)別詳解(EventBus 事件機制,Spring Event事件機制)

    EventBus與Spring Event區(qū)別詳解(EventBus 事件機制,Spring Event事件機制)

    這篇文章主要介紹了EventBus與Spring Event區(qū)別,需要的朋友可以參考下
    2020-02-02
  • java遞歸算法的實例詳解

    java遞歸算法的實例詳解

    在本篇文章里小編給大家整理了關(guān)于java遞歸算法的實例內(nèi)容,以及相關(guān)知識點總結(jié),需要的朋友們可以學(xué)習(xí)下。
    2020-02-02
  • Spring整合Junit詳解

    Spring整合Junit詳解

    Spring 是目前主流的 Java Web 開發(fā)框架,是 Java 世界最為成功的框架。該框架是一個輕量級的開源框架,具有很高的凝聚力和吸引力,本篇文章帶你了解如何配置數(shù)據(jù)源、注解開發(fā)以及整合Junit
    2022-07-07

最新評論