मैं Kotlin के लिए एक परियोजना की ओर पलायन कर रहा हूँ, और यह:Kotlin: MyClass :: class.java बनाम this.javaClass
public static Properties provideProperties(String propertiesFileName) {
Properties properties = new Properties();
InputStream inputStream = null;
try {
inputStream = ObjectFactory.class.getClassLoader().getResourceAsStream(propertiesFileName);
properties.load(inputStream);
return properties;
} catch (IOException e) {
e.printStackTrace();
} finally {
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return null;
}
अब है:
fun provideProperties(propertiesFileName: String): Properties? {
return Properties().apply {
ObjectFactory::class.java.classLoader.getResourceAsStream(propertiesFileName).use { stream ->
load(stream)
}
}
}
बहुत अच्छा, Kotlin! : पी
सवाल यह है: यह विधि के अंदर .properties
फ़ाइल की तलाश करती है। का उपयोग करना:
ObjectFactory::class.java.classLoader...
यह काम करता है, लेकिन का उपयोग कर:
this.javaClass.classLoader...
classLoader
null
है ...
(ध्यान दें कि मेमोरी पता भी अलग है)
क्यों?
धन्यवाद
'this.javaClass' का मूल्य क्या है? – chrylis
मैंने अपना प्रश्न संपादित कर लिया है। –
क्या आपको रनटाइम या संकलन-समय पर त्रुटि मिल रही है? रनटाइम पर – chrylis