错误再现
C:\Users\ZDY\.jdks\corretto-1.8.0_412\bin\java.exe -ea -Didea.test.cyclic.buffer.size=1048576 "-javaagent:E:\IntelliJ IDEA 2023.1.2\lib\idea_rt.jar=63470:E:\IntelliJ IDEA 2023.1.2\bin" -Dfile.encoding=UTF-8 -classpath "E:\IntelliJ IDEA 2023.1.2\lib\idea_rt.jar;E:\IntelliJ IDEA 2023.1.2\plugins\junit\lib\junit5-rt.jar;E:\IntelliJ IDEA 2023.1.2\plugins\junit\lib\junit-rt.jar;C:\Users\ZDY\.jdks\corretto-1.8.0_412\jre\lib\charsets.jar;C:\Users\ZDY\.jdks\corretto-1.8.0_412\jre\lib\ext\access-bridge-64.jar;C:\Users\ZDY\.jdks\corretto-1.8.0_412\jre\lib\ext\cldrdata.jar;C:\Users\ZDY\.jdks\corretto-1.8.0_412\jre\lib\ext\dnsns.jar;C:\Users\ZDY\.jdks\corretto-1.8.0_412\jre\lib\ext\jaccess.jar;C:\Users\ZDY\.jdks\corretto-1.8.0_412\jre\lib\ext\jfxrt.jar;C:\Users\ZDY\.jdks\corretto-1.8.0_412\jre\lib\ext\localedata.jar;C:\Users\ZDY\.jdks\corretto-1.8.0_412\jre\lib\ext\nashorn.jar;C:\Users\ZDY\.jdks\corretto-1.8.0_412\jre\lib\ext\sunec.jar;C:\Users\ZDY\.jdks\corretto-1.8.0_412\jre\lib\ext\sunjce_provider.jar;C:\Users\ZDY\.jdks\corretto-1.8.0_412\jre\lib\ext\sunmscapi.jar;C:\Users\ZDY\.jdks\corretto-1.8.0_412\jre\lib\ext\sunpkcs11.jar;C:\Users\ZDY\.jdks\corretto-1.8.0_412\jre\lib\ext\zipfs.jar;C:\Users\ZDY\.jdks\corretto-1.8.0_412\jre\lib\jce.jar;C:\Users\ZDY\.jdks\corretto-1.8.0_412\jre\lib\jfr.jar;C:\Users\ZDY\.jdks\corretto-1.8.0_412\jre\lib\jfxswt.jar;C:\Users\ZDY\.jdks\corretto-1.8.0_412\jre\lib\jsse.jar;C:\Users\ZDY\.jdks\corretto-1.8.0_412\jre\lib\management-agent.jar;C:\Users\ZDY\.jdks\corretto-1.8.0_412\jre\lib\resources.jar;C:\Users\ZDY\.jdks\corretto-1.8.0_412\jre\lib\rt.jar;C:\Users\ZDY\Desktop\JDBC\out\production\jdbc;C:\Users\ZDY\Desktop\JDBC\jdbc\lib\mysql-connector-java-8.0.15.jar;C:\Users\ZDY\.m2\repository\junit\junit\4.13.1\junit-4.13.1.jar;C:\Users\ZDY\.m2\repository\org\hamcrest\hamcrest-core\1.3\hamcrest-core-1.3.jar;C:\Users\ZDY\Desktop\JDBC\jdbc\lib\druid-1.1.12.jar" com.intellij.rt.junit.JUnitStarter -ideVersion5 -junit4 example.BrandTest,testSelectAll C:\Users\ZDY\Desktop\JDBC\jdbc java.io.FileNotFoundException: src\drui.properties (系统找不到指定的文件。) at java.io.FileInputStream.open0(Native Method) at java.io.FileInputStream.open(FileInputStream.java:195) at java.io.FileInputStream.<init>(FileInputStream.java:138) at java.io.FileInputStream.<init>(FileInputStream.java:93) at example.BrandTest.testSelectAll(BrandTest.java:27) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:59) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:56) at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306) at org.junit.runners.BlockJUnit4ClassRunner$1.evaluate(BlockJUnit4ClassRunner.java:100) at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:366) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:103) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:63) at org.junit.runners.ParentRunner$4.run(ParentRunner.java:331) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:79) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:329) at org.junit.runners.ParentRunner.access$100(ParentRunner.java:66) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:293) at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306) at org.junit.runners.ParentRunner.run(ParentRunner.java:413) at org.junit.runner.JUnitCore.run(JUnitCore.java:137) at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:69) at com.intellij.rt.junit.IdeaTestRunner$Repeater$1.execute(IdeaTestRunner.java:38) at com.intellij.rt.execution.junit.TestsRepeater.repeat(TestsRepeater.java:11) at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:35) at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:232) at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:55)
分析
对于这种写死文件路径的方式比较繁琐
不便于操作
因为我们没有输入提示
类似于手搓源码
这边给大家看看我的项目结构
我们要在BrandTest里用字节输入流引入druid.properties文件
我们可以有两种方式
绝对路径
复制绝对路径填入
//获取连接 Properties prop=new Properties(); prop.load(new FileInputStream("C:\\Users\\ZDY\\Desktop\\JDBC\\jdbc\\src\\druid.properties"));
测试通过
能成功链接
相对路径(推荐)
我们观察不难发现
配置文件在src文件夹下
我们直接找我们需要引入文件的上级文件夹即可
这样也能成功对应上文件
//获取连接 Properties prop=new Properties(); prop.load(new FileInputStream("src\\druid.properties"));