Java基础 -- 读取项目中的文件(.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 字节流

  • 重点部分
    . /代表同级目录
    …/ 代表上级目录(两个点)
    / 代表根目录
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];//数组容量超级大,一次能将中英混合文本全部读取完
            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" href="https://qxhut.cn/zb_users/plugin/Jz52_click/click.css">
<script src="https://qxhut.cn/zb_users/plugin/Jz52_click/click.js"></script>
<h1>Click Style</h1>
</body>
</html>

4. Awakening

         在一秒钟内看到本质的人和花半辈子也看不清一件事本质的人,自然是不一样的命运。

在这里插入图片描述

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

百世经纶『一页書』

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值