java在gradle工程访问src/test/resources目录下的资源配置文件

package com.jiepu;
import java.io.File;
import java.net.URISyntaxException;
import java.util.Map;
import java.util.Properties;

//java在gradle工程访问src/test/resources或者src/main/resources目录下的资源配置文件
public class  TestMain
{
    public static  void main(String args[]) throws URISyntaxException {
        System.out.println(new File(".").getAbsolutePath());
        Properties properties=new Properties();
        try {
            // properties.load(new FileInputStream("config.properties"));
            System.out.println(TestMain.class.getResource("/config.properties").toExternalForm());
            System.out.println(Thread.currentThread().getContextClassLoader().getResource("config.properties"));
            properties.load(TestMain.class.getResource("/config.properties").openStream());

        } catch (Exception e) {
            e.printStackTrace();
        }
        String version=properties.getProperty("version");
        System.out.println(version);
        for(Map.Entry<Object,Object>  entry:properties.entrySet())
        {
            Object key=entry.getKey();
            Object value=entry.getValue();
            System.out.println(key+"="+value);
        }
    }

}

import java.io.File;
import java.io.IOException;
import java.net.URL;

public class MyUrlDemo {


    public static void main(String[] args) {
        MyUrlDemo muDemo = new MyUrlDemo();
        try {
            muDemo.showURL();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public void showURL() throws IOException {

        // 第一种:获取类加载的根路径   D:\git\daotie\daotie\target\classes
        File f = new File(this.getClass().getResource("/").getPath());
        System.out.println(f);

        // 获取当前类的所在工程路径; 如果不加“/”  获取当前类的加载目录  D:\git\daotie\daotie\target\classes\my
        File f2 = new File(this.getClass().getResource("").getPath());
        System.out.println(f2);

        // 第二种:获取项目路径    D:\git\daotie\daotie
        File directory = new File("");// 参数为空
        String courseFile = directory.getCanonicalPath();
        System.out.println(courseFile);


        // 第三种:  file:/D:/git/daotie/daotie/target/classes/
        URL xmlpath = this.getClass().getClassLoader().getResource("");
        System.out.println(xmlpath);


        // 第四种: D:\git\daotie\daotie
        System.out.println(System.getProperty("user.dir"));
         /*
          * 结果: C:\Documents and Settings\Administrator\workspace\projectName
          * 获取当前工程路径
          */

        // 第五种:  获取所有的类路径 包括jar包的路径
        System.out.println(System.getProperty("java.class.path"));

    }
}

<think>嗯,用户的问题是关于在Spring Boot项目的特定目录下创建配置文件,并向其中添加内容。让我仔细看看他们的需求。 首先,用户提到的是项目ch4d的src/main/resources目录下创建ok.properties和test.properties文件。他们需要向ok.properties中添加指定内容,可能类似之前提到的application.properties配置。根据引用[1],Spring Boot默认会在src/main/resources下寻找配置文件,所以创建在这里是合理的。 接下来,我需要确认如何正确创建这些文件。在Maven或Gradle项目中,resources目录用于存放配置文件和其他资源文件,编译时会自动包含到classpath中。用户可能使用IDE如IntelliJ或Eclipse,或者手动创建文件。步骤应该是导航到项目的src/main/resources目录,然后新建两个文件ok.properties和test.properties。对于ok.properties,用户需要添加特定的配置项,比如示例中的logging.file=my.log,或者类似的内容。需要确保语法正确,每个配置项占一行,格式为key=value。 另外,用户可能想知道是否需要其他配置让Spring Boot识别这些文件。根据引用[2],Spring Boot会自动加载application.properties,但其他自定义的properties文件可能需要通过@PropertySource注解来引入。例如,在Java配置类中添加@PropertySource("classpath:ok.properties"),这样Spring才会读取其中的配置。不过用户的问题可能仅涉及创建文件,所以是否需要额外配置取决于他们的使用场景。 还需要注意文件编码和格式,确保没有特殊字符或格式错误。例如,引用[4]中提到修改linkis-ps-publicservice.properties的问题,可能是由于配置错误导致的问题,所以正确书写内容很重要。 总结步骤:1. 定位到正确目录;2. 创建文件;3. 添加内容。可能需要指导用户如何验证配置是否生效,比如在代码中注入属性值进行测试。此外,是否需要处理多环境配置,比如使用profile-specific文件,但用户的问题中没有提到,所以可能不需要涉及。 最后,用户可能还会关心如何确保这些配置文件被打包到最终的jar中,但Maven的标准结构会自动处理。所以只需确认文件位于正确的resources目录下即可。</think>### 操作步骤 1. **定位项目目录** 打开项目`ch4d`,找到`src/main/resources`目录。这是Spring Boot默认加载配置文件的路径[^2]。 2. **创建配置文件** 在`src/main/resources`目录下新建两个文件: - `ok.properties` - `test.properties` 3. **编辑ok.properties** 向`ok.properties`中添加指定内容(以下为示例配置,可根据需求调整): ```properties # 示例配置 app.name=MyApplication app.version=1.0.0 logging.level.root=INFO ``` 4. **验证配置文件加载** Spring Boot会自动加载`resources`目录下的配置文件。若需在代码中引用配置,可使用`@Value`注解: ```java @Value("${app.name}") private String appName; ``` ### 注意事项 - 若需覆盖默认配置加载路径,可在`application.properties`中通过`spring.config.location`指定[^2]。 - 多环境配置建议使用`application-{profile}.properties`格式(如`application-dev.properties`)。
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值