Sometimes static calling SomeClass.class.getClassLoader().getResourceAsStream() and SomeClass.class.getClass().getResourceAsStream() to load a resource...
Use the getResourceAsStream() method to get the InputStream when reading a file from inside a jar file. ... .getClassLoader(). .getResourceAsStream(fileName)
searchs you classpath for the given file/resource and it can also provide
InputStreams
of resources from inside a JAR.So, if your properties exist in some folder in the physical filesystem (e.g. user folder, ...) use
FileInputStream
and if the file is embedded in your classpath (e.g. as a resource inside the JAR) use
getResourceAsStream
.
20 Nisan 2012
11 kişi değerlendirdi
0 yorum
{"2_i40y":{"state":{"logo":{"href":"//yandex.com.tr","isForeign":true},"form":{"action":"/search","searchLabel":"Bul","hiddenInputs":[{"name":"lr","value":20815}]},"actions":{"advancedSearch":{"checked":false,"ariaLabel":"Gelişmiş arama"},"moreButtonProps":{"ariaLabel":"Daha fazla"},"cbir":{"name":"cbir","label":"Поиск по картинке","infoUrl":"//yandex.ru/promo/photosearch/index?source=landingdesk","isMacOS":false}},"userSection":{"loggedIn":false,"loginProps":{"loginUrl":"https://passport.yandex.com.tr/auth?retpath=%2F&origin=serp_desktop_auth_new","loginText":"Giriş yap","burgerAriaLabel":"Menü"}},"navigation":{"tabs":[{"id":"www","label":"Arama","url":"//yandex.com.tr","params":{"source":"tabbar"},"target":""},{"id":"images","label":"Görsel","url":"//yandex.com.tr/gorsel/search","params":{"text":"[$query]","noreask":"[$noreask]","nomisspell":"[$nomisspell]","parent-reqid":"[$reqid]","from":"tabbar"}},{"id":"video","label":"Video","url":"//yandex.com.tr/video/search","params":{"text":"[$query]","noreask":"[$noreask]","parent-reqid":"[$reqid]","from":"tabbar"}},{"id":"maps","label":"Haritalar","url":"//yandex.com.tr/harita","params":{"text":"[$query]","source":"serp_navig"}},{"id":"translate","label":"Çeviri","url":"//ceviri.yandex.com.tr","params":{"text":"[$query]","from":"tabbar"}}],"selectedTabId":"www"},"context":{"query":"getresourceasstream","reqid":"1720995922161982-13043395969109055731-balancer-l7leveler-kubr-yp-vla-81-BAL","noreask":"1","lr":"20815","counters":{"type":"b","data":{"$header":"0_dymmz000","logo":"0_dymmz001","login":"0_dymmz003","usermenu":"0_dymmz004","market-cart":"0_dymmz006","more-button":"0_dymmz007","cbir":"0_dymmz008","$navigation":"0_dymmz009","item":"0_dymmz00i","tab-www":"0_dymmz00b","tab-images":"0_dymmz00d","tab-video":"0_dymmz00f","tab-maps":"0_dymmz00h","tab-translate":"0_dymmz00j"}}},"loggedIn":false,"baobab":{"parentNode":{"context":{"genInfo":{"prefix":"0_dymmw01-0-1"},"ui":"desktop","service":"web","reqid":"1720995922161982-13043395969109055731-balancer-l7leveler-kubr-yp-vla-81-BAL","fast":{"name":"header-desktop"}}}}},"type":"header-desktop"},"2_dymm0":{"state":{"answersUrl":"https://stackoverflow.com/questions/10247161/when-to-use-getresourceasstream-method#answers","type":"fact","tabsOptions":[{"id":"question","text":"Soru"},{"id":"bestAnswer","text":"En iyi yanıt"},{"id":"moreAnswers","text":"3 yanıt daha","shortText":"Daha fazla"}],"tabContents":{"question":{"construct":[{"type":"paragraph","content":[{"type":"text","content":"I was confused using the said method because while loading some properties file people are following a different approaches..."}]},{"type":"code","content":[{"type":"text","content":[{"type":"text","content":"Properties prop"}]},{"type":"text","content":[{"type":"text","content":" = "},{"type":"text","color":"keyword","content":"new"},{"type":"text","content":" Properties("},{"type":"text","color":"keyword","content":"new"},{"type":"text","content":" FileInputStream("},{"type":"text","color":"keyword","content":"new"},{"type":"text","content":" File("},{"type":"text","color":"string","content":"\"\u003cpath of the properties file>\""},{"type":"text","content":"));"}]}]},{"type":"paragraph","content":[{"type":"text","content":"and few are using.."}]},{"type":"code","content":[{"type":"text","content":[{"type":"text","content":"Properties prop"}]},{"type":"text","content":[{"type":"text","content":" = "},{"type":"text","color":"keyword","content":"new"},{"type":"text","content":" Properties(getClass().getResourceAsStream("},{"type":"text","color":"string","content":"\"\u003cpath of the properties file>\""},{"type":"text","content":"));"}]}]},{"type":"paragraph","content":[{"type":"text","content":"Which one to use when?"}]}],"footerProps":{"date":1334913930,"numLiked":5,"numCommented":2}},"bestAnswer":{"construct":[{"type":"paragraph","content":[{"type":"code","content":[{"type":"text","content":"getResourceAsStream"}],"isInline":true},{"type":"text","content":" searchs you classpath for the given file/resource and it can also provide "},{"type":"code","content":[{"type":"text","content":"InputStreams"}],"isInline":true},{"type":"text","content":" of resources from inside a JAR."}]},{"type":"paragraph","content":[{"type":"text","content":"So, if your properties exist in some folder in the physical filesystem (e.g. user folder, ...) use "},{"type":"code","content":[{"type":"text","content":"FileInputStream"}],"isInline":true},{"type":"text","content":" and if the file is embedded in your classpath (e.g. as a resource inside the JAR) use "},{"type":"code","content":[{"type":"text","content":"getResourceAsStream"}],"isInline":true},{"type":"text","content":"."}]}],"footerProps":{"date":1334914637,"numLiked":11,"numCommented":0}},"moreAnswers":[{"construct":[{"type":"paragraph","content":[{"type":"text","content":"When reading a file from the "},{"type":"text","content":"filesystem"},{"type":"text","content":" use a FileInputStream(File()) using relative or absolute paths."}]},{"type":"paragraph","content":[{"type":"text","content":"when your program is distributed as a jar and you need to load a file that is inside that jar, you need to use getResourceAsStream(), it will search the "},{"type":"text","content":"classpath"},{"type":"text","content":" for the file, and the path is relative to the classpath."}]}],"footerProps":{"date":1334914534,"numLiked":2,"numCommented":0}},{"construct":[{"type":"paragraph","content":[{"type":"text","content":"You can use the first approach if you are 100 % sure that the file location doesn't change across environments. This means there is a operations over head to make sure those directory paths are created across all environments. On the flip side, you have the flexibility of updating the properties file without opening the jar."}]},{"type":"paragraph","content":[{"type":"text","content":"The second approach is very portable as you are reading from the classpath. But it has the disadvantage of re-bundling the jar file for every property update."}]},{"type":"paragraph","content":[{"type":"text","content":"So, it basically depends upon your use-case."}]}],"footerProps":{"date":1334914272,"numLiked":0,"numCommented":0}}]},"organic":{"faviconProps":{"src":"https://favicon.yandex.net/favicon/v2/https://stackoverflow.com","size":"m","pageNumber":0,"index":0},"title":"When to use \"\u0007[getResourceAsStream\u0007]\" method?","pathProps":{"items":[{"url":"https://stackoverflow.com/questions/10247161/when-to-use-getresourceasstream-method","text":"stackoverflow.com"},{"url":"https://stackoverflow.com/questions/10247161/when-to-use-getresourceasstream-method","text":"when-to-use-getresourceasstream-method"}]},"faviconPosition":0,"subtitlePosition":1,"linkProps":{"url":"https://stackoverflow.com/questions/10247161/when-to-use-getresourceasstream-method"},"hostTitle":"stackoverflow.com","description":""},"extralinksProps":{"hidden":true,"directions":["bottom-end"],"items":[{"variant":"reportFeedback","reportFeedback":{"feature":"Блок ответов для разработчиков","title":"Bu yanıtta yanlış olan nedir?","checkBoxLabels":[{"value":"Yanıt sorguya uygun değil"},{"value":"Yanıt hatalı"},{"value":"Yanıttaki bilgiler eksik"},{"value":"Diğer"}]}}]},"baobab":{"parentNode":{"context":{"genInfo":{"prefix":"2_dymmw0c-0-1"},"ui":"desktop","service":"web","reqid":"1720995922161982-13043395969109055731-balancer-l7leveler-kubr-yp-vla-81-BAL","fast":{"name":"article"}}}}},"type":"article"},"2_dymm1":{"state":{"linksGroups":[{"type":"geo","links":[{"label":"Columbus","title":"Columbus","url":"//yandex.com.tr/tune/geo","logNode":{"name":"region"},"target":"_self","a11yLabel":"Bölgeniz Columbus","needRetpath":true}]},{"type":"feedback","links":[{"label":"Hata bildir","logNode":{"name":"feedback","attrs":{"behaviour":{"type":"dynamic"}}}}]},{"type":"settings","links":[{"label":"Ayarlar","url":"//yandex.com.tr/tune/search","logNode":{"name":"settings"},"target":"_self","needRetpath":true}]},{"type":"company","links":[{"label":"Hakkımızda","url":"https://company.yandex.com.tr","logNode":{"name":"about"},"target":"_blank"},{"label":"Ticari teklifler","url":"/search/direct?text=getresourceasstream&filters_docs=direct_cm","logNode":{"name":"commercial"},"target":"_blank"},{"label":"Kariyer","url":"//yandex.com.tr/jobs/?utm_source=serp_turkie&utm_medium=footerlink&utm_content=career","logNode":{"name":"vacancy"},"target":"_blank"},{"label":"Kullanım lisansı","url":"//yandex.com.tr/legal/termsofuse/","logNode":{"name":"license"},"target":"_blank"},{"label":"Gizlilik Politikası","url":"//yandex.com.tr/legal/confidential/","logNode":{"name":"confidential"},"target":"_blank"}],"a11yHidden":true}],"reportFeedbackProps":{"feature":"Подвал","title":"Hata bildir","checkBoxLabels":[]},"isSearchApp":false,"hasExtralinks":true,"limitingSelector":".AliceFabButton","baobab":{"parentNode":{"context":{"genInfo":{"prefix":"2_dymmw0d-0-1"},"ui":"desktop","service":"web","reqid":"1720995922161982-13043395969109055731-balancer-l7leveler-kubr-yp-vla-81-BAL"}}}},"type":"serp-footer"},"2_dymm2":{"state":{"conditionLoadList":{"marketCart":false,"highchartsHighstock":false,"advProductGallery":false,"musicPlayer":false,"ecomRates":false,"photoViewer":false,"darkmodeChangeDrawer":true},"reportFeedbackBaseProps":{"initEmail":"","metaFields":{"userAgent":"Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; ClaudeBot/1.0; +claudebot@anthropic.com)","userTestids":"186144,287509,142430,338390,785125,1053283,1060748,1062411,1054351,1053436,1047584,1052893,967970","yandexuid":"9090557311720995922","uid":"0","queryText":"getresourceasstream","reqid":"1720995922161982-13043395969109055731-balancer-l7leveler-kubr-yp-vla-81-BAL","searchSlices":["drawer_search_slice_english","drawer_search_slice_cs"],"userRegionName":"Columbus","userRegionId":"20815"}},"extralinksPopupProps":{"abuseLink":"https://twitter.yandex.com.tr/search/abuse?sign=205c1aa4004bbd76643da195b0c67e3e&keyno=0"},"isLoggedIn":false,"userMenuDialogProps":{"flag":"skin,feedback","host":"yandex.com.tr","lang":"tr","project":"serp","queryParams":{"utm_source":"search"},"retpath":"https%3A%2F%2Ftwitter.yandex.com.tr%2Fsearch%3Fes_context%3D0oYmAQ____________ARpMMTcyMDEwODU2Mjc2NzMxNi0xMTk1NTEzNzY3MjIwMjI1MzM0Mi1iYWxhbmNlci1sN2xldmVsZXIta3Vici15cC1zYXMtMTg2LUJBTDIFYXNzb2MbDusp%26lr%3D20815%26noreask%3D1%26rq%3D1%26serp-reload-from%3Drec_bottom%26source%3Dtabbar%26text%3Dgetresourceasstream%26ncrnd%3D49851","tld":"com.tr"},"baobab":{"parentNode":{"context":{"genInfo":{"prefix":"2_dymmw0e-0-1"},"ui":"desktop","service":"web","reqid":"1720995922161982-13043395969109055731-balancer-l7leveler-kubr-yp-vla-81-BAL","fast":{"name":"lazy-load"}}}}},"type":"lazy-load"}}