java实现从json文件中读取数据

2023-02-18 00:00:00 数据 文件 读取

java实现从json文件中读取数据

import java.io.*;
import java.nio.charset.StandardCharsets;

/** * @Description 文件工具类 * @ClassName FileUtils * @Author yuhuofei * @Date 2022/3/18 0:00 * @Version 1.0 */
public class FileUtils { 
    /** * 从json文件中读取数据 * * @param jsonFilePath * @return */
    public static String getDataFromJsonFile(String jsonFilePath) { 
        StringBuilder jsonString = new StringBuilder();
        File file = new File(jsonFilePath);
        if (file.exists()) { 
            BufferedReader bufferedReader = null;
            try { 
                bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(file), StandardCharsets.UTF_8));
                char[] chars = new char[8192];
                int length;
                while ((length = bufferedReader.read(chars)) != -1) { 
                    String temp = new String(chars, 0, length);
                    jsonString.append(temp);
                }
            } catch (IOException e) { 
                System.out.println("=====获取数据异常=====" + e);
            } finally { 
                if (null != bufferedReader) { 
                    try { 
                        bufferedReader.close();
                    } catch (IOException ex) { 
                        System.out.println("=======获取数据时,关闭流异常=======" + ex);
                    }
                }
            }
        }
        return jsonString.toString();
    }
}

    原文作者:yuhuofei2021
    原文地址: https://blog.csdn.net/Crezfikbd/article/details/123563992
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。

相关文章