×

Loading...
Ad by
  • 最优利率和cashback可以申请特批,好信用好收入offer更好。请点链接扫码加微信咨询,Scotiabank -- Nick Zhang 6478812600。
Ad by
  • 最优利率和cashback可以申请特批,好信用好收入offer更好。请点链接扫码加微信咨询,Scotiabank -- Nick Zhang 6478812600。

If a class needs a .properties file or .xml file, I usually use Java resource mechanism.....

本文发表在 rolia.net 枫下论坛Let me say we have a com.abccompany.infrastructure.DBConnection class. Right beside this class, we can create dbconfiguartion.properties file. Then in the constructor, we may read properties file as follows

Class cls = this.getClass();
ClassLoader loader = cls.getClassLoader();
InputStream input = loader.getResourseAsInputStream("com/abccompany/infrastructure/dbconfiguration.properties");
Properties p = new Properties();
p.load(input);
.................................
The above code may contain mistakes but the main idea is correct.
Configuartion properties file is part of the class file, so it is always shipped with the class file. Never separate it from the class file.

If you are talking about server configuartion, please follow server documentation.

Each server has its different "." direcetory. You can create a small Java servlet programm to determine it:
.....................
File file =new File(".");
System.out.println(file.getAbstractPath());
........

The above code should be in a doGet, doPost or service method.

Good luck更多精彩文章及讨论,请光临枫下论坛 rolia.net
Report

Replies, comments and Discussions:

  • 工作学习 / IT技术讨论 / Jabber: When you are writing a java program, you surely will take care of the configuration file , right ? what method do u use to read and write the configuration file ??!! Thank you in advance !
    • I use struts. And use XML file as the configuration file .
      • Is it part the Jakarta project ?? Could you send me a sample code for that ?! Thank you !
        • http://www.linuxforum.net/doc/strutsuserguide.html It's a guid, please read it first. And it's not easy to use, you must study it hard...
      • If yes , do you find the Struts Framework powerful , yet easy to use ?!
    • If a class needs a .properties file or .xml file, I usually use Java resource mechanism.....
      本文发表在 rolia.net 枫下论坛Let me say we have a com.abccompany.infrastructure.DBConnection class. Right beside this class, we can create dbconfiguartion.properties file. Then in the constructor, we may read properties file as follows

      Class cls = this.getClass();
      ClassLoader loader = cls.getClassLoader();
      InputStream input = loader.getResourseAsInputStream("com/abccompany/infrastructure/dbconfiguration.properties");
      Properties p = new Properties();
      p.load(input);
      .................................
      The above code may contain mistakes but the main idea is correct.
      Configuartion properties file is part of the class file, so it is always shipped with the class file. Never separate it from the class file.

      If you are talking about server configuartion, please follow server documentation.

      Each server has its different "." direcetory. You can create a small Java servlet programm to determine it:
      .....................
      File file =new File(".");
      System.out.println(file.getAbstractPath());
      ........

      The above code should be in a doGet, doPost or service method.

      Good luck更多精彩文章及讨论,请光临枫下论坛 rolia.net