आपको ClassLoader.getResources(name)
(या स्थिर संस्करण ClassLoader.getSystemResources(name)
) की आवश्यकता है।
लेकिन दुर्भाग्यवश ऐसे संसाधनों के साथ ज्ञात समस्या है जो "निर्देशिका" के अंदर नहीं हैं। जैसे foo/bar.txt
ठीक है, लेकिन bar.txt
एक समस्या हो सकती है। यह described well in the Spring Reference है, हालांकि यह किसी भी तरह से वसंत-विशिष्ट समस्या नहीं है।
अद्यतन:
यहाँ एक सहायक विधि है कि InputStreams की सूची लौटाता है:
public static List<InputStream> loadResources(
final String name, final ClassLoader classLoader) throws IOException {
final List<InputStream> list = new ArrayList<InputStream>();
final Enumeration<URL> systemResources =
(classLoader == null ? ClassLoader.getSystemClassLoader() : classLoader)
.getResources(name);
while (systemResources.hasMoreElements()) {
list.add(systemResources.nextElement().openStream());
}
return list;
}
उपयोग:
List<InputStream> resources = loadResources("config.properties", classLoader);
// or:
List<InputStream> resources = loadResources("config.properties", null);
स्रोत
2011-07-18 09:27:39
कूल। धन्यवाद! क्या आप एक उदाहरण जोड़ सकते हैं कि इनपुट यूआरटी प्राप्त करने के लिए उन यूआरएल के साथ कैसे आगे बढ़ना है? – Zeemee
@ मूलमोथ: 'यूआरएल' वर्ग में 'ओपनस्ट्रीम' विधि है जो उस यूआरएल के लिए इनपुटस्ट्रीम लौटाती है। आपको बस इतना ही चाहिए। –
@Mmmoth मेरा अपडेट देखें –