تحميل ملف خصائص من حزمة جافا
-
22-07-2019 - |
سؤال
أحتاج إلى قراءة ملفات الخصائص المدفونة في بنية الحزمة الخاصة بي com.al.common.email.templates
.
لقد حاولت كل شيء ولا أستطيع معرفة ذلك.
في النهاية، سيتم تشغيل الكود الخاص بي في حاوية servlet، لكنني لا أريد الاعتماد على الحاوية في أي شيء.أكتب حالات اختبار JUnit ويجب أن تعمل في كليهما.
المحلول
عند تحميل خصائص من فئة في حزمة com.al.common.email.templates
يمكنك استخدام
Properties prop = new Properties();
InputStream in = getClass().getResourceAsStream("foo.properties");
prop.load(in);
in.close();
و(إضافة كافة معالجة الاستثناء ضروري).
إذا صفك ليست في هذه الحزمة، تحتاج إلى aquire وInputStream بشكل مختلف قليلا:
InputStream in =
getClass().getResourceAsStream("/com/al/common/email/templates/foo.properties");
ومسارات النسبية (من دون الرائدة '/') في getResource()
/ getResourceAsStream()
يعني أن الموارد سيتم البحث النسبي إلى الدليل الذي يمثل حزمة الطبقة هي في.
وعن طريق java.lang.String.class.getResource("foo.txt")
أن البحث ل(غير موجودة) /java/lang/String/foo.txt
الملف على CLASSPATH.
نصائح أخرى
لإضافة إلى الإجابة يواكيم سوير، وإذا كنت في حاجة من أي وقت مضى للقيام بذلك في سياق ثابت، يمكنك أن تفعل شيئا مثل ما يلي:
static {
Properties prop = new Properties();
InputStream in = CurrentClassName.class.getResourceAsStream("foo.properties");
prop.load(in);
in.close()
}
و(معالجة استثناء elided، كما كان من قبل.)
تتعلق الحالتين التاليتين بتحميل ملف خصائص من فئة مثال مسماة TestLoadProperties
.
حالة 1:تحميل ملف الخصائص باستخدام ClassLoader
InputStream inputStream = TestLoadProperties.class.getClassLoader()
.getResourceAsStream("A.config");
properties.load(inputStream);
في هذه الحالة، يجب أن يكون ملف الخصائص في ملف root/src
الدليل للتحميل الناجح.
الحالة 2:تحميل ملف الخصائص دون استخدام ClassLoader
InputStream inputStream = getClass().getResourceAsStream("A.config");
properties.load(inputStream);
في هذه الحالة، يجب أن يكون ملف الخصائص في نفس الدليل مثل ملف TestLoadProperties.class
للتحميل الناجح.
ملحوظة: TestLoadProperties.java
و TestLoadProperties.class
هما ملفان مختلفان.السابق، .java
الملف، عادة ما يوجد في ملف المشروع src/
الدليل، في حين أن الأخير، .class
الملف، عادة ما يتم العثور عليه في ملف bin/
الدليل.
public class Test{
static {
loadProperties();
}
static Properties prop;
private static void loadProperties() {
prop = new Properties();
InputStream in = Test.class
.getResourceAsStream("test.properties");
try {
prop.load(in);
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public class ReadPropertyDemo {
public static void main(String[] args) {
Properties properties = new Properties();
try {
properties.load(new FileInputStream(
"com/technicalkeeda/demo/application.properties"));
System.out.println("Domain :- " + properties.getProperty("domain"));
System.out.println("Website Age :- "
+ properties.getProperty("website_age"));
System.out.println("Founder :- " + properties.getProperty("founder"));
// Display all the values in the form of key value
for (String key : properties.stringPropertyNames()) {
String value = properties.getProperty(key);
System.out.println("Key:- " + key + "Value:- " + value);
}
} catch (IOException e) {
System.out.println("Exception Occurred" + e.getMessage());
}
}
}
وإذا افترضنا باستخدام خصائص و> الصف، عبر فيها <لأ href = "http://java.sun.com/j2se/1.5.0/docs/api/java/util/Properties.html#load(java.io.InputStream)" يختلط = "نوفولو noreferrer"> تحميل الطريقة، واعتقد انك تستخدم ClassLoader <لأ href = "http://java.sun.com/j2se/1.5.0/docs/api/java/lang/ClassLoader هتمل # getResourceAsStream (java.lang.String) "يختلط =" نوفولو noreferrer "> getResourceAsStream للحصول على تيار الإدخال.
وكيف حالك يمر في الاسم، يبدو أنه ينبغي أن يكون في هذا النموذج: /com/al/common/email/templates/foo.properties
وتمكنت من حل هذه المسألة مع هذه الدعوة
Properties props = PropertiesUtil.loadProperties("whatever.properties");
وخارج، لديك لوضع ملف whatever.properties بك في / SRC / الرئيسية / الموارد
لا أحد يذكر حل مماثل ولكن حتى أبسط من فوق دون الحاجة للتعامل مع مجموعة من الطبقة. على افتراض myfile.properties هو في classpath.
Properties properties = new Properties();
InputStream in = ClassLoader.getSystemResourceAsStream("myfile.properties");
properties.load(in);
in.close();
واستمتع
واستخدام رمز أدناه يرجى:
Properties p = new Properties();
StringBuffer path = new StringBuffer("com/al/common/email/templates/");
path.append("foo.properties");
InputStream fs = getClass().getClassLoader()
.getResourceAsStream(path.toString());
if(fs == null){
System.err.println("Unable to load the properties file");
}
else{
try{
p.load(fs);
}
catch (IOException e) {
e.printStackTrace();
}
}