Suppose I have a Java class that needs to access a file with the absolute path /home/gem/projects/bar/resources/test.csv:
package com.example class Foo { String filePath = ?????
If the path to Foo.java is / home / gem / projects / bar / src / com / example.
Of course, I cannot specify the absolute path to the resource file. This is because the jar file will be distributed as a library for any clients that will be used in their own environments.
Assume that a resource file, such as test.csv, is always in the same path relative to the project root. When a jar containing Foo.class is created, this gang also contains test.csv in the same relative path (relative to the project root).
What is the way to specify a relative path that will work no matter where the project pane moves? Also how can I create a jar file (which can be anywhere), so the path to the test.csv resource file will still be correct.
To keep things simple, I used the wrong Java API (readAllLines (), which reads all lines and returns a line containing all the contents of the file. Also do not use try / catch).
Suppose a csv file can be read as well as written to.
Hopefully this will become clear now.
source share