Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java - Relative path of a file in a java web application

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.

========

Updated

========

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...

like image 280
Yatendra Avatar asked Mar 07 '10 09:03

Yatendra


People also ask

How do I find the path of a file in Java?

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.

Does Java file use relative 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.


1 Answers

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"); 
like image 171
Thilo Avatar answered Sep 20 '22 09:09

Thilo