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 URISyn…
要取编译后的路径,而不是你看到的src/main/resources的路径.如下: URL url = MyTest.class.getClassLoader().getResource("userFile.properties"); File file = new File(url.getFile()); 或者 InputStream is = MyTest.class.getClassLoader().getResourceAsStream("userFile.prope…
//生成一个文件对象: File file = new File(getClass().getClassLoader().getResource("test.xml").getPath()); //直接得到一个输入流: InputStream in = getClass().getClassLoader().getResourceAsStream("test.xml"); //在当前线程获取--这种方法不大稳定 //String path = Thread.curr…
String conf = System.getProperty("user.home") + File.separator + "a.properties"; FileInputStream fis = new FileInputStream(conf);prop.load(fis);fis.close();…
Java开发中,经常需要在maven工程中读取src/main/resources下的配置文件: 思路如下: Class.getClassLoader() 返回类加载器ClassLoader,进而可以获取到classpath路径下的资源 ClassLoader.getSystemResourceAsStream() 返回读取指定资源的输入流InputStream Properties.load(InputStream inStream) 从输入流InputStream中读取属性列表(键和元素对)…
今天在写分布式项目的时候,一直无法编译 resource 下的配置文件:(在target文件夹下的 classes文件查看是否编译) 最后只能通过在POM文件中配置resources配置 得以解决: <resources> <resource> <directory>src/main/resource</directory> <includes> <include>**/*.properties</include> <…
我们在用Mybatis去操作底层数据库的时候,需要用到xml配置文件,一般我们是把配置文件和dao放置在同一层目录.但是在用idea操作maven项目的时候,我们可能会遇到无法读取到dao对应的mapper.xml文件. 简单的解决方法如下: 第一种: 将xml文件移至src/main/resource下面,这样做最省事,但是面临一个问题,就是看起来项目整体结构不清晰,不统一,层次感不好,那么我们看下第二种. 第二种: 我们需要在pom.xml文件里面去配置一下,让项目启动的时候能够去读取到sr…
maven工程读取resource下配置文件 在maven工程中,我们会将配置文件放到,src/main/resources   下面,例如 我们需要确认resource 下的文件 编译之后存放的位置 它编译的路径直接位于classes下面,这个路径其实就是classPath的路径,所以,在resources 根目录下的配置文件其实就是 classPath的路径 public static void main(String[] args) throws ParserConfigurationEx…
java读取resource目录下的配置文件 1:配置resource目录 下的文件 host: 127.0.0.1 port: 9300 2:读取    / 代表resource目录 InputStream in = this.getClass().getResourceAsStream("/config.properties"); Properties properties = new Properties(); try { properties.load(in); } catch…
在Maven项目的开发中,当需要读取src/下的配置文件时,该怎么做? 我们假设Resources下有一个文件名为kafka.properties的配置文件(为什么用kafka.properties,因为这是在做kafka项目的时候碰到的问题,在网上查到了不少信息,索性当个搬运工,再根据自己的理解整理一下) 1.在java类中读取若配置文件不在src/main/resources目录下,可以直接使用 Properties prop = new properties(); prop.load(ne…