File Path - Relative Path


File Path - Relative Path



I have a maven project and as part of my requirement , I need to read a file from within a Test class and thats in one of the directories inside the test folders.



My proj hierarchy is like this..


src
--test
---java
--org
--sample
--- MyTestClass.java
---jmeter
---load_data.csv



In MyTestClass.java I need to read the load_data.csv. My trials so far with File and Path have not yielded results



file.exists() is always giving me false...How can i find the file and read it



Regards





Try adding the following code to your MyTestClass so that you can see what the current working directory is when you run your test: System.out.println("working dir: "+Paths.get("").toAbsolutePath()); Then you should be able to see how to build your full path.
– D.B.
Jul 1 at 16:45


MyTestClass


System.out.println("working dir: "+Paths.get("").toAbsolutePath());





Add it to the classpath by specifying it as a resources directory in your build tool. Then just load it as you would any other classpath resource.
– Boris the Spider
Jul 1 at 17:00




3 Answers
3



Assuming the root of your project is the parent of src, then your path should be:


src


Paths.get("src", "test", "jmeter", "load_data.csv")





Why not pass multiple values to Paths.get()? E.g. Paths.get("src","test","jmeter","load_data.csv") or use one of the resolve methods? Using either of these ways you don't have to worry about the separator. Also you may want to include that you're assuming that the root of the project is the current working directory at runtime.
– D.B.
Jul 1 at 16:55



Paths.get()


Paths.get("src","test","jmeter","load_data.csv")





@D.B. Good point, I forgot that existed.
– Jacob G.
Jul 1 at 16:57



The question is 2 fold. How do you access a file from Maven, and second how do you access the file from your class. I am assuming your maven build is failing because it does not pass the test due to the missing file.



Quick and dirty ways:



Enter full path. Depending on OS navigate to the file in your terminal and type (OSX and nix): "pwd" and copy and paste the full path inside your new File()
Windows: echo %cd%
OR
How to construct a relative path in Java from two absolute paths (or URLs)?



Better ways:
You should probably add that file inside resources. By convention you want to place your resources inside that folder. It makes it easy to access inside your classes aka class.getResource("somefile.txt") no need to worry about relative path.
Also it gets added to your jar when you are distributing your program.
File Structure:
Convention src-> main -> resources -> load_data.csv



In your case src -> test -> resources -> load_data.csv



Now how to reference a resource from maven. Hard way add resources during build below is from Maven documentation https://maven.apache.org/plugins/maven-resources-plugin.


<executions>
<execution>
<id>copy-resources</id>
<!-- here the phase you need -->
<phase>validate</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${basedir}/target/extra-resources</outputDirectory>
<resources>
<resource>
<directory>src/non-packaged-resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</configuration>
</execution>
</executions>



If you don't want testResources to be part of the main distribution you need to add a test profile and exclude them from your main phase/build. Some more documentation https://maven.apache.org/plugins/maven-resources-plugin/testResources-mojo.html



Add your load_data.csv inside src/test/resources/ and query accordingly


load_data.csv


src/test/resources/






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

How to make file upload 'Required' in Contact Form 7?

Rothschild family

amazon EC2 - How to make wp-config.php to writable?