In this tutorial, we’ll explore the differences between different methods of reading files in Java. We’ll compare the getResourceAsStream() method and the...
The parameter of the function getResourceAsStream(String path) is a path, but how should this path be filled in? This is the difficulty of using this function.
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
12 kişi değerlendirdi
0 yorum
{"2_mwe":{"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":"Görsele göre arama","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 java что это","reqid":"1723973951217994-3577752632855489975-balancer-l7leveler-kubr-yp-sas-143-BAL","noreask":"1","lr":"20815","counters":{"type":"b","data":{"$header":"0_9pnz000","logo":"0_9pnz001","login":"0_9pnz003","usermenu":"0_9pnz004","market-cart":"0_9pnz006","more-button":"0_9pnz007","cbir":"0_9pnz008","$navigation":"0_9pnz009","item":"0_9pnz00i","tab-www":"0_9pnz00b","tab-images":"0_9pnz00d","tab-video":"0_9pnz00f","tab-maps":"0_9pnz00h","tab-translate":"0_9pnz00j"}}},"loggedIn":false,"baobab":{"parentNode":{"context":{"genInfo":{"prefix":"0_9pnw01-0-1"},"ui":"desktop","service":"web","reqid":"1723973951217994-3577752632855489975-balancer-l7leveler-kubr-yp-sas-143-BAL","fast":{"name":"header-desktop"}}}}},"type":"header-desktop"},"2_9pn0":{"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":12,"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_9pnw0c-0-1"},"ui":"desktop","service":"web","reqid":"1723973951217994-3577752632855489975-balancer-l7leveler-kubr-yp-sas-143-BAL","fast":{"name":"article"}}}}},"type":"article"},"2_9pn1":{"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%20java%20%D1%87%D1%82%D0%BE%20%D1%8D%D1%82%D0%BE&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_9pnw0d-0-1"},"ui":"desktop","service":"web","reqid":"1723973951217994-3577752632855489975-balancer-l7leveler-kubr-yp-sas-143-BAL"}}}},"type":"serp-footer"},"2_9pn2":{"state":{"conditionLoadList":{"marketCart":false,"highchartsHighstock":false,"advProductGallery":false,"musicPlayer":false,"ecomRates":false,"photoViewer":false,"easterEggModal":false,"easterEggPlusGamesModal":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,1088045,1084834,1085212,1084257,1078245,1073688,1083708,1078940,1075725,1086874,1074523,1048634,1083275,1081436,614106,1083943,30276,1088314,936723,1079245,1076738,1090223,1086968,1084554,1085587,1088066,1083569,1070959,1060354,1079187,1085582,124065,1087988,1087903,1077693,1070803,801288,461652,60,1081011,912221,728509,1084364,1080095,89890,151171,126351,1079127,13962,287509,338390,777799,1081010,1084229,1079367,1088400,1083829,1087883,1051464,1081664,1087425,1083872,1088512","yandexuid":"2900956811723973947","uid":"0","queryText":"getresourceasstream java что это","reqid":"1723973951217994-3577752632855489975-balancer-l7leveler-kubr-yp-sas-143-BAL","searchSlices":["drawer_search_slice_cs"],"userRegionName":"Columbus","userRegionId":"20815","isChildAccount":false}},"extralinksPopupProps":{"abuseLink":"https://twitter.yandex.com.tr/search/abuse?sign=02893802e6cc8f116322397b4f7a3098&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%3D0oYl4Q____________ARpKMTcyMDEwODYyMTE4NzEzMi02MDA2Mjk1OTMwNTc5MDMzMjA0LWJhbGFuY2VyLWw3bGV2ZWxlci1rdWJyLXlwLWtsZy0yNS1CQUwyBWFzc29j7TzKBg%26lr%3D20815%26noreask%3D1%26rq%3D1%26serp-reload-from%3Drec_bottom%26source%3Dtabbar%26text%3Dgetresourceasstream%2Bjava%2B%25D1%2587%25D1%2582%25D0%25BE%2B%25D1%258D%25D1%2582%25D0%25BE%26ncrnd%3D11864","tld":"com.tr"},"baobab":{"parentNode":{"context":{"genInfo":{"prefix":"2_9pnw0e-0-1"},"ui":"desktop","service":"web","reqid":"1723973951217994-3577752632855489975-balancer-l7leveler-kubr-yp-sas-143-BAL","fast":{"name":"lazy-load"}}}}},"type":"lazy-load"}}