I want to read a file from a java web application. I don't want to give the absolute path of the file. I just want to put the file in some directory of my web application.
Or
It can be placed along with .war file (packaged web application).
What relative path to give for the file. I tried ./filename.csv
but it didn't work.
========
I will deliver a WAR
file (packaged web application) to my client. This web application will read a file (lets say SuppliedFile.csv
) which will be copied to the server by the client. So I need a mechanism (that will work irrespective of whether the application server will unpak the WAR
or not) so that web application can read that file.
Note: I am not using the SuppliedFile.csv
in a servlet... I am using it in a plain Java class...
In Java, for NIO Path, we can use path. toAbsolutePath() to get the file path; For legacy IO File, we can use file. getAbsolutePath() to get the file path.
Define a Relative Path to Locate File in JavaWe can use the relative path to locate a file resource in the current working directory.
Do you really need to load it from a file? If you place it along your classes (in WEB-INF/classes) you can get an InputStream to it using the class loader:
InputStream csv = SomeClassInTheSamePackage.class.getResourceAsStream("filename.csv");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With