Two ways to read json files in java

1 Background introduction

During the research and development process, it often involves repeated steps such as reading configuration files. It can also be a .conf file, or a .json file, but in any case they need to enter the inputStream of jave. Let's take reading the .json file as an example

2 FileInputStream read

One parameter is required:
fileName: The file name, usually an absolute path, otherwise it may not be found. Or in the same path as the java file

static String readWithFileInputStream(){
    
    
        String jsonString;
                //System.getProperty("user.dir")为获取根目录
                //File.separator为不同操作系统的分隔符,linux和win是不一样的
                //tempFilePath该字符串里面为我们配置文件的路径
               String fileName = "xx_config.json";
//                String tempFilePath = System.getProperty("user.dir") + File.separator + "resource" + File.separator + fileName;
//                System.out.print(tempFilePath);
        StringBuilder sb = new StringBuilder();
                try{
    
    
                    InputStream input = new FileInputStream(fileName);

                    byte[] buffer = new byte[1024];
                    int length = 0;
                    length = input.read(buffer);

                    while(length != -1){
    
    
                        sb.append(new String(buffer, 0 , length));
                        length = input.read(buffer);
                    }


                }catch (Exception e){
    
    
                    e.printStackTrace();
                }
        return    jsonString = sb.toString();
            }
      

Finally returns a String. Then you can convert it to the model you want to read through the JSON tool.

    TargetConfig config = (TargetConfig) JSON.parseObject(jsonString, TargetConfig.class);
            

However, this method is not flexible, and the path needs to be hardcoded or written as an absolute path.

3 ClassLoader read

Two parameters are required:
fileName: file name
ClassLoader: class loader, generally the current class

    static String readWithClassLoader() throws IOException {
    
    
        String fileName = "xx_config.json";

       ClassLoader  classLoader =  TargetConfig.class.getClassLoader();

        BufferedReader reader = null;

            InputStream inputStream = classLoader.getResourceAsStream(fileName);

            reader = new BufferedReader(new InputStreamReader(inputStream));

            StringBuilder content = new StringBuilder();
            String line = reader.readLine();
            while (!StringUtil.isEmpty(line)) {
    
    
                content.append(line);
                line = reader.readLine();
            }

            return content.toString();
    }
      和之前一样,最终返回一个String。然后通过JSON工具就可以转为自己想读取到模型啦。
    TargetConfig config = (TargetConfig) JSON.parseObject(jsonString, TargetConfig.class);
            

But using class loading to read, you don't need to hardcode the path. Much more flexible than the first one.
insert image description here

Guess you like

Origin blog.csdn.net/qq_39463175/article/details/130456989