zoukankan      html  css  js  c++  java
  • 读取Jar中的json文件

    现在操作json的jar 都是用的fastjson,

    如果需要读取的json文件不在jar包里面,则可以这样获取到:

    String path = this.getClass().getClassLoader().getResource("json/abc.json").getPath();//该目录是以resources目录为根目录
    //文件内容直接转为String类型 String content = FileUtils.readFileToString(new File(path), "UTF-8");
    JSONObject obj = JSONObject.parseObject(content)
    
    

    否则就需要下面这样获取:

    String path = "json/abc.json";//该目录是以resources目录为根目录
    //文件内容直接转为String类型
    InputStream is = this.getClass().getClassLoader().getResourceAsStream(path);
    BufferedReader in = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));
    StringBuffer buffer = new StringBuffer();
    String line = "";
    while ((line = in.readLine()) != null){
    buffer.append(line);
    }
    String input = buf
    JSONObject obj = JSONObject.parseObject(buf.toString())
  • 相关阅读:
    join
    PS1-4
    tftp + bras
    awk调用shell
    curl
    ssh
    查看cp进度,使用watch
    tftp
    scp 链接文件的问题 + tar
    mysql必知必会(三、使用mysql)
  • 原文地址:https://www.cnblogs.com/dwb91/p/9670882.html
Copyright © 2011-2022 走看看