iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > Python >Java实现读取项目中文件(.json或.properties)的方法详解
  • 403
分享到

Java实现读取项目中文件(.json或.properties)的方法详解

Python 官方文档:入门教程 => 点击学习

摘要

目录1. 读取JSON file1.1 json dependency1.2 字节流1.3 buffer reader2. 读取properties file3. 好看的CSS样式1

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>

到此这篇关于Java实现读取项目中文件(.json或.properties)的方法详解的文章就介绍到这了,更多相关Java读取项目文件内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

--结束END--

本文标题: Java实现读取项目中文件(.json或.properties)的方法详解

本文链接: https://www.lsjlt.com/news/204762.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作