IDEA中向properties配置文件中写入静态路径,并JAVA代码实现文件的读取和运行

功能:实现json文件的自动化读取

要求:写一个json文件,并对文件进行解析,文件的路径必须写在配置文件中

  •  在C:\Users\qx\Documents路径下,创建一个t2.json文件
{"state":"success"}
  • 在IDEA中创建一个maven项目,在resources文件夹下创建py.properties文件如下:
  • 因为需要解析json文件,因此在pom.xml中添加依赖如下
    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
    
        <groupId>Python_Airflow</groupId>
        <artifactId>Python_Airflow</artifactId>
        <version>1.0-SNAPSHOT</version>
    
        <dependencies>
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>fastjson</artifactId>
                <version>1.2.4</version>
            </dependency>
        </dependencies>
    
        <properties>
            <maven.compiler.source>1.8</maven.compiler.source>
            <maven.compiler.target>1.8</maven.compiler.target>
        </properties>
    
    </project>

    依赖根据自己需要进行添加

  • 在java目录下创建Python_Airflow类
    import com.alibaba.fastjson.JSON;
    import com.alibaba.fastjson.JSONArray;
    import com.alibaba.fastjson.JSONObject;
    
    import java.io.*;
    import java.io.InputStream;
    import java.util.Iterator;
    import java.util.Properties;
    import java.util.ArrayList;
    import java.util.List;
    
    
    public class Python_Airflow {
        public static String readJsonFile(String fileName) {
            String jsonStr = "";
            try {
                File jsonFile = new File(fileName);
                FileReader fileReader = new FileReader(jsonFile);
                Reader reader = new InputStreamReader(new FileInputStream(jsonFile), "utf-8");
                int ch = 0;
                StringBuffer sb = new StringBuffer();
                while ((ch = reader.read()) != -1) {
                    sb.append((char) ch);
                }
                fileReader.close();
                reader.close();
                jsonStr = sb.toString();
                return jsonStr;
            } catch (IOException e) {
                e.printStackTrace();
                return null;
            }
        }
    
        public static class ReadProperties {
    
            public String getPyValue(String urlName) {
                String p = null;
                Properties prop = new Properties();
                try {
                    ClassLoader classLoader = ReadProperties.class.getClassLoader();// 读取属性文件xxxxx.properties
                    InputStream in = classLoader.getResourceAsStream("py.properties");
                    prop.load(in); /// 加载属性列表
                    Iterator<String> it = prop.stringPropertyNames().iterator();
                    while (it.hasNext()) {
                        if (it.next().equals(urlName)) {
                            p = prop.getProperty(urlName);
                        }
                    }
                    in.close();
                } catch (Exception e) {
                }
                System.out.println(p);
                return p;
            }
        }
    
        public static void main(String[] args)
        {
            String path = new ReadProperties().getPyValue("path");
    
            String s = readJsonFile(path);
            JSONObject jobp = JSONObject.parseObject(s);
            String state = (String) jobp.get("state");
            System.out.println(state);
        }
    }

    运行成功:

发布了111 篇原创文章 · 获赞 57 · 访问量 6万+

猜你喜欢

转载自blog.csdn.net/qq_38358499/article/details/100709844