美文网首页
后台读取本地文件(以本地mock数据为例)

后台读取本地文件(以本地mock数据为例)

作者: heler | 来源:发表于2018-05-15 14:55 被阅读0次
    File file = new File("url");
        if (file.isFile()) {
            // 以字节流方法读取文件
            FileInputStream fis = null;
            try {
                fis = new FileInputStream(file);
                // 设置一个,每次 装载信息的容器
                byte[] buf = new byte[1024];
                // 定义一个StringBuffer用来存放字符串
                StringBuffer sb = new StringBuffer();
                // 开始读取数据
                int len = 0;// 每次读取到的数据的长度
                while ((len = fis.read(buf)) != -1) {// len值为-1时,表示没有数据了
                    // append方法往sb对象里面添加数据
                    sb.append(new String(buf, 0, len, "utf-8"));
                }
                String sbjson=sb.toString();
                // 输出字符串
                try {
                    JSONObject jsonObject=new JSONObject(sbjson);
                    org.json.JSONArray obj1=jsonObject.getJSONArray("data");
                list=JSONObject.parseArray(String.valueOf(obj1),TreeDto.class);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                System.out.println(list.toString());
            } catch (IOException e) {
                e.printStackTrace();
            }
        } else {
            System.out.println("文件不存在!");
        }
    

    java后台读取本地JSON数据,简单粗暴粘贴复制直接ok。注意里面的变量名自己定义

    相关文章

      网友评论

          本文标题:后台读取本地文件(以本地mock数据为例)

          本文链接:https://www.haomeiwen.com/subject/xsaedftx.html