Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to use relative path in files?

 
narendra cheeterala
Greenhorn
Posts: 4
Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i am designing a web project in MVC pattern, so i created a properties file & used that file to load properties for database.
my code is:

public static boolean loadDBProperties(){
try{
p = new Properties();

fis = new FileInputStream("C:/Program Files/Apache Software Foundation/Tomcat 6.0/webapps/LoginApplicationMVC1/WEB-INF/properties/pro.properties");

p.load(fis);

return true;

}
catch(Exception e){

return false;
}

}

//creating and returning connection object
public static Connection getConnection()
{

try{
if(loadDBProperties()){

Class.forName(p.getProperty("db.driver"));

con = DriverManager.getConnection(p.getProperty("db.url"),p.getProperty("db.userid"),p.getProperty("db.password"));

return con;
}

return null;

}
catch(Exception e){

return null;
}
}


In that code how to use relative path to load properties file........
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65111
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Place the file under the WEB-INF folder and use the ServletContext.getResourceAsStream() method to open a stream to the file.
 
Pablo Abbate
Ranch Hand
Posts: 30
Java Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you using spring framework, if not please use Resource Bundle and put the properties file into web-inf classes folder.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic