0

I have to read properties file "MyProperty.properties" from "ReadProp.java" class given my the following directory structure of my "war" file I am going to deploy.

MyApp.war
 | ----MyProps 
 |     |--MyProperty.properties
 |---WEB-INF |   
     |--classes
          |---ReadProp.java 

I am going to deploy this "war" file in "Sun portal server". But I should not change any of this directory structure because of the requirement specification.

I am reading this file in the following way

     String path = servletContext.getRealPath("/MyProps/MyProperty.properties");         System.out.println("path: " + path);  
            Properties prop = new Properties();         
    try {           
             prop.load(new FileInputStream(path));
);
         } catch (Exception e) { 

                            e.printStackTrace();      
       }      
       String name= prop.getProperty("name"); 

It is working fine. but the problem is if I change properties file after loading the application the changes are not reflecting.

I may change the properties file anytime how to do If I want that changes should be reflected . I mean the application should load the properties file everytime in the exexcutio

Raju Boddupalli
  • 1,789
  • 4
  • 21
  • 29

4 Answers4

0

You won't see changes unless you bounce the app server.

A better choice would be to put the .properties file in your /WEB-INF/classes folder and read it from the CLASSPATH using getResourceAsStream().

You don't say where you're reading the code. You might have to implement a Timer task to periodically wake up and reload the .properties file.

You might also try a WatchService if you're using JDK 7:

Auto-reload changed files in Java

Community
  • 1
  • 1
duffymo
  • 305,152
  • 44
  • 369
  • 561
0

You need to use java 7 WatchService for that Example Link

twid
  • 6,368
  • 4
  • 32
  • 50
0

I got the answer:

String path = servletContext.getRealPath("/MyProps/MyProperty.properties");       
System.out.println("path: " + path);      
Properties prop = new Properties();       
try {      
   File f = new File(path);      
   FileInputStream fis = new FileInputStream(f);     
   prop.load(fis);    
}
catch (Exception e) {   
   e.printStackTrace();   
}
Pang
  • 9,564
  • 146
  • 81
  • 122
Raju Boddupalli
  • 1,789
  • 4
  • 21
  • 29
0

The newer way to do this is:

    Path path;
    try {
        path = Paths.get("MyProperty.properties");
        if (Files.exists(path)) {
            props = new Properties();
            props.load(Files.newInputStream(path));
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
Paul Gregoire
  • 9,715
  • 11
  • 67
  • 131