FileInputStream (Absolute path)
To load a resource file such as x.properties for program use, first thing that we would consider will be specifying the absolute file path as given below:
InputStream input = new FileInputStream("/Users/jwithanawasam/some_dir/src/main/resources/
config.properties”);
However, when ever we moved the project to another location, this path has to be changed, which is not acceptable.
FileInputStream (Relative path)
So, the next option would be to use the relative file path as given below, instead of giving absolute file path:
InputStream input = new FileInputStream("src/main/resources/config.properties”);
This approach seems to solve the above mentioned concern.
However, problem with this is the relative path is depending on the current working directory, which JVM is started. In this scenario, it is "/Users/jwithanawasam/some_dir". But, in a different deployment setting this may change, which leads to change the given relative path accordingly. Moreover, we, as developers do not have much control over JVMs current working directory.
In any of the above cases, we will get java.io.FileNotFoundException error, which is a familiar exception for most java developers.
class.getResourceAsStream
At runtime, JVM checks the class path to locate any user defined classes and packages. (In Maven, build artifacts and dependancies are stored under path given for M2_REPO class path variable. E.g., /Users/jwithanawasam/.m2/repository) The .jar file which is the deployable unit of the project will be located here.
JVM uses class loader to load java libraries specified in class path.
So, best thing we can do is load the resource specifying a path relative to its class path using class loader. Here, specified relative path will work irrespective of the actual disk location the package is deployed.
Following methods reads the file using class loader.
InputStream input = Test.class.getResourceAsStream("/config.properties");
Usually, in Java projects resources such as configuration files, images etc. are located in src/main/resources/ path. So, if we add a resource immediately inside this folder, during packaging, the file will be located in the immediate folder in .jar file.
We can verify this using the following command to extract content of jar file:
jar xf someproject.jar
If you place the resources in another sub folder, then you have to specify the path relative to src/main/resources/ path.
So, using this approach we can load resources using relative paths in a hard disk location independent manner. Once we package the application, it is ready to be deployed anywhere, as it it is, without the overhead of having to validate resource file paths, thus improving the portability of the application.
ServletContext.getResourceAsStream for web applications
For web applications, use the following method:
ServletContext context = getServletContext();
InputStream is = context.getResourceAsStream("/filename.txt");
Here, file path is taken relative to your web application folder. (The unzipped version of the .war file)
E.g., mywebapplication.war (unzipped) will have a hierarchy similar to the following.
mywebapplication
META-INF
WEB-INF
classes
filename.txt
So, "/" means the root of this web application folder.
This method allows servlet containers to make a resource available to a servlet from any location, without using a class loader.
https://bayanlarsitesi.com/
ReplyDeleteTokat
Kastamonu
Tekirdağ
Gümüşhane
WA85P
whatsapp görüntülü show
ReplyDeleteücretli.show
8J41S4
adıyaman evden eve nakliyat
ReplyDeletekastamonu evden eve nakliyat
kırklareli evden eve nakliyat
diyarbakır evden eve nakliyat
ankara evden eve nakliyat
4EUM
düzce evden eve nakliyat
ReplyDeletedenizli evden eve nakliyat
kırşehir evden eve nakliyat
çorum evden eve nakliyat
afyon evden eve nakliyat
RMTQU
tekirdağ evden eve nakliyat
ReplyDeletekocaeli evden eve nakliyat
yozgat evden eve nakliyat
osmaniye evden eve nakliyat
amasya evden eve nakliyat
AREN
yalova evden eve nakliyat
ReplyDeletetunceli evden eve nakliyat
giresun evden eve nakliyat
ağrı evden eve nakliyat
van evden eve nakliyat
XARUR
6019B
ReplyDeleteAfyon Şehirler Arası Nakliyat
Ankara Asansör Tamiri
Yalova Parça Eşya Taşıma
Çerkezköy Oto Boya
AAX Güvenilir mi
Bingöl Lojistik
Çerkezköy Parke Ustası
Şırnak Parça Eşya Taşıma
Sivas Şehirler Arası Nakliyat
F5336
ReplyDeleteBitlis Şehir İçi Nakliyat
Ağrı Şehirler Arası Nakliyat
Tunceli Parça Eşya Taşıma
Edirne Evden Eve Nakliyat
İzmir Şehirler Arası Nakliyat
Mersin Şehirler Arası Nakliyat
Bartın Lojistik
Bibox Güvenilir mi
Gölbaşı Boya Ustası
9C4C7
ReplyDeleteMuş Şehir İçi Nakliyat
Jns Coin Hangi Borsada
Ordu Evden Eve Nakliyat
Mersin Evden Eve Nakliyat
Malatya Şehirler Arası Nakliyat
Çerkezköy Yol Yardım
Pepecoin Coin Hangi Borsada
Bitlis Şehir İçi Nakliyat
Balıkesir Lojistik
B5305
ReplyDeleteBitcoin Madenciliği Siteleri
Kripto Para Madenciliği Nedir
Coin Kazanma
Coin Madenciliği Siteleri
resimli
Madencilik Nedir
Bitcoin Kazanma Siteleri
Yeni Çıkan Coin Nasıl Alınır
Coin Nasıl Çıkarılır
4825C
ReplyDeletebinance referans kodu
resimli magnet
resimli magnet
binance referans kodu
binance referans kodu
resimli magnet
referans kimliği nedir
referans kimliği nedir
binance referans kodu
81E18
ReplyDeleteamiclear
FFC51
ReplyDeletehatay en iyi ücretsiz sohbet uygulamaları
elazığ ücretsiz sohbet
denizli parasız sohbet siteleri
edirne rastgele sohbet siteleri
tokat sesli sohbet uygulamaları
kocaeli bedava sohbet odaları
karabük canlı görüntülü sohbet siteleri
canlı sohbet odası
canli sohbet
25CD9
ReplyDeletebitlis bedava görüntülü sohbet
malatya random görüntülü sohbet
karaman canlı sohbet bedava
parasız sohbet siteleri
ısparta Sesli Sohbet Sitesi
kırşehir muhabbet sohbet
bingöl sesli sohbet sitesi
nevşehir canlı sohbet et
hatay telefonda görüntülü sohbet
0D998
ReplyDeleteYoutube Beğeni Hilesi
Referans Kimliği Nedir
Onlyfans Takipçi Hilesi
Onlyfans Beğeni Hilesi
Bitcoin Oynama
Gate io Borsası Güvenilir mi
Threads Yeniden Paylaş Satın Al
Threads Takipçi Satın Al
Bitcoin Nasıl Alınır
0C66D
ReplyDeletekripto ne demek
bitexen
bitget
canli sohbet
telegram türk kripto kanalları
kripto para nasıl alınır
binance 100 dolar
bitexen
bitget
CC3DC
ReplyDeletekizlarla canli sohbet
kripto para telegram
telegram kripto
gate io
binance referans kimliği
papaya meyvesi
bitcoin seans saatleri
copy trade nedir
kucoin
E4EC4
ReplyDelete----
----
----
----
----
----
matadorbet
----
----
افران الغاز Fpf2zkA74m
ReplyDeleteشركة مكافحة حشرات بخميس مشيط 6vUKWArc7X
ReplyDeleteتنظيف مجالس n7eHUg2Fbe
ReplyDelete