java笔记_3_读取文件内容

    public JSONObject toGetFileData(Integer model){
        //用于存储文件内容
        String jsonString = "";
        InputStream is = null;
        try {
            is = new FileInputStream("D:\\data\\xxxxx.txt");
            //用来保存每行读取的内容
            String line = null;
            BufferedReader reader = new BufferedReader(new InputStreamReader(is));
            //读取第一行
            line = reader.readLine();
            //如果line为空说明读完了
            while (line != null) {
                //将读到的内容添加到 jsonString 中
                jsonString += line;
                //添加换行符
                jsonString += "\n";
                //读取下一行
                line = reader.readLine();
            }
            reader.close();
            is.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        JSONObject jsonObject = JSONObject.parseObject(jsonString);
        return jsonObject;
    }

 

posted @ 2022-08-03 15:20  LuLuYaa  阅读(13)  评论(0编辑  收藏  举报