Reading and Writing a Properties File

// Read properties file.
Properties properties = new Properties();
try {
properties.load(new FileInputStream("filename.properties"));
} catch (IOException e) { }

// Write properties file.
try {
properties.store(new FileOutputStream("filename.properties"), null);
} catch (IOException e) { }


To read java properties files from the classpath:

The properties files can be stored in the classpath. This way they can be put inside jar files and it’s really useful for web applications when the absolute location of the properties files is not known. When I tested this in an web application it didn’t work:

Properties properties = new Properties() ;
URL url = ClassLoader.getSystemResource("test.properties");
properties.load(new FileInputStream(new File(url.getFile())));

To read java properties files from the classpath in a web application:

The following example works to load the properties files in a web application. This snippet was tested on Tomcat 5.5. The ‘/’ represents the root of the class path. Otherwise the properties file location is considered relatively to “this” class (or to MyClass for the second example):

Properties properties = new Properties() ;
properties.load(this.getClass().getResourceAsStream("/seoimproved.properties"));

Similar example to use in a static context:

Properties properties = new Properties() ;
properties.load(MyClass.class.getResourceAsStream("/seoimproved.properties"));

To read java properties files from a specific location:

The properties files can be loaded from any location.

Properties properties = new Properties() ;
properties.load(new FileInputStream("C:\\tmp\\test.properties"));
In the J2EE application you can trust the class loader to find the properties file under this directory. For this you should code your application as:

String propFile = "propConfig.properties";
URL url = Thread.currentThread().getContextClassLoader().getResource(propFile);
Properties prop = new Properties();
prop.load( url.openStream() );


or

Properties prop = new Properties();
try
{
prop.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("hello.properties"));
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
Q : พอสั่ง Run server แล้วดันกลายเป็นสั่งให้ Publish แล้วทำให้ Start Server ไม่ได้
A : ให้ทำการปิดเปิด Rad 6.0 ใหม่ ถ้ายังไม่หายอีกให้ Restart เครื่องซึ่งน่าจะหายหมด

Q : อยู่ดีๆ Config ของ Workspace หายไป เช่น Classpath ของ Ear เรา หรือว่าอยู่ดีๆ Application เราก็ Error ทั้งๆ ทีไม่ได้ทำอะไร
A : ให้ Config ใหม่ครับ ก็จะทำงานได้ หรือ Restart อีกครั้งนึงหลังจากปิดไปแล้ว

Q : ปัญหา Generate Webservice ไม่ได้
A : บางทีเราอาจจะ Generate บ่อยเลยไม่สามารถ Generate ได้ให้ไปสร้าง Project ใหม่พื่อ Generate จะสามารถ Generate ได้ครับ (ถ้าจะให้ดีสร้าง Workspace ใหม่เลย)

Q : ทำไมบางทีเรา Restart Server แล้วมันค้างที่ Starting ตลอดเลย
A : แก้โดยให้ Stop Server แล้วเอา Application ที่อยู่ใน Server ออกให้หมดแล้ว Start อีกทีก็น่าจะได้ แล้วส่ง Publish ก่อนแล้วค่อยเอา Application ไปลงที่ Server อีกทีก็จะใช้งานได้

Q : ทำไม Generate Web Service Client แล้วไปเรียกคนอื่นไม่ได้
A : Rad 6.0 มันใช้ Mechanism ในการไปเรียกเป็นของ IBM เองซึ่งมันเก่าอ่ะ ทำให้ไปเรียกไม่ได้ แล้วต้องเรียกผ่าน Server ด้วยเรียกผ่าน Application ไม่ได้ แล้วถ้าให้มันเป็น Axis มันก็เก่าไป เรียกคนอื่นไม่ได้ (Rad 7.0 ก็เป็นครับ) สามารถดูได้ที่ Entry นี้

Q : Config SSL แล้วไม่สามารถที่จะ Start Server ได้
A : ให้ Config เครื่องเป้นปี ค.ศ. ก็จะหายครับ (เกิดจาก Certification หมดอายุนั่นเอง มันมอง พ.ศ. เป็น ค.ศ.)

Q : ปัญหาไม่สามารถเปิด File JSP ได้
A : ปิด RAD 6.0 แล้วเปิดใหม่ก็จะหาย

Q : อยู่ดี ๆ RAD 6.0 ก็ปิดตัวเอง
A : แสดงว่า Ram ของเครื่องน้อยเกินไปให้เพิ่ม ปัญหานี้ก็จะหายครับ

Q : พอ Start Server แล้วมองไม่เห็น Class ใน Java Application
A : ให้ทำการ Set Java Dependencies ที่ Properties ของ Project นั้นๆ (จำเป็นจะต้อง Add ลงใน application.xml ใน Ear Project ก่อนด้วย)

Q : เวลาสร้าง Server ใหม่แล้วเรียก Datasource จะมองไม่เห็น Table
A : ปัญหานี้เกิดจากว่าเราสร้าง Server ใหม่แล้ว ตัวที่เรา Config (รวมทั้ง Datasource) ยังจำที่เรา Config ไว้ที่ Server เก่าอยู่ให้ลบทิ้งแล้ว Config ใหม่ก็จะหายครับ

Q : ทำไมเวลเรา เรียก Application แล้วมองไม่เห็น Class จากทางฝั่ง Web (เรียกจาก EJB)
A : ปัญหาเกิดจากเอาไม่ได้กำหนดที่ Deployment Description บน Ear Project ให้เป็น Application น่ะครับ โดยทำตามนี้
  1. ไปที่ Tab Deployment (application.xml ใน Ear Project)
  2. แล้วดูที่ Application (อยู่ล่างสุด) ให้ดูที่ WAR classloader policy ว่าเป็น APPLICATION หรือยัง ถ้ายังทำให้เป็นแล้ว Restart Application ใหม่ก็จะเห็นครับ
Q : ทำไมตอนแรกเรียก EJB ได้ แล้วพอแก้ไขอะไรใหม่ กลับเรียกไม่ได้
A : วิธีแก้มีดังนี้ครับ
  1. ดู Error Log ก่อนว่ามันขึ้นมาเป็น JNDI Name Not Found หรือเปล่า ถ้าใช่แก้โดยได้ตรวจสอบ Reference ใน Web.xml ว่าไปเรียก EJB ถูกอันหรือเปล่า (ตรวจสอบทั้ง Web.xml และ ejb-jar.xml ว่าไปถูกตัวกันจริงหรือเปล่า) อ่านเพิ่มเติม ลิงค์ นี้
  2. ถ้าไม่ได้ขึ้นแบบข้างบนให้แก้โดย Deploy EJB ใหม่โดยการคลิกขวาที่ EJB Project แล้วเลือก Deploy ครับ มันจะ Generate ตัว Stub ใหม่ แล้วจะใช้งานได้ กรณี่ที่ Deploy EJB ไม่ได้ให้ทำตาม ลิงค์ นี้ ครับ
  3. ถ้ายังไม่ได้อีกลอง Clean Project ทั้งหมดที่ใช้แล้ว Build ใหม่ดูน่าจะหายครับ
Q : File Config (.xml เช่น web.xml) พอแก้ไขแล้วทำไมยังเป็น Config แบบเดิมอยู่
A : พวก File เหล่านี้ ถ้าเราแก้ไขแล้ว เราจะต้องทำการ Restart Application เสมอ เพราะ Rad 6.0 จะไม่ Restart ให้ครับ ต้องมา Restart เอง (ในบางครั้ง อาจจะต้องสั่ง Publish Server ใหม่ด้วยถึงจะเป็น Config แบบใหม่)

reference : link1
#1

#2
top