Home > Back-end >  How to read the properties file maven project
How to read the properties file maven project

Time:01-31

Younger brother has a try maven project, file structure as shown in figure, DBPoolConnection need to read the properties, the path always write wrong,
 
The static {
The Properties Properties=loadPropertiesFile (path);
Try {
DruidDataSource=(druidDataSource) DruidDataSourceFactory. CreateDataSource (properties);//DruidDataSrouce factory pattern
} the catch (Exception e) {
The log. The error (" failed to get the configuration ");
}
}

Private static Properties loadPropertiesFile (String fullFile) {
String webRootPath=null;
If (null==fullFile | | fullFile. Equals (" ")) {
Throw new IllegalArgumentException (" the Properties file path can not be null "+ fullFile);
}
WebRootPath=DBPoolConnection. Class. GetClassLoader (). The getResource (" "). GetPath ();
WebRootPath=new File (webRootPath). The getParent ();
InputStream InputStream=null;
The Properties of p=null;
Try {
InputStream=new FileInputStream (new File (webRootPath + File. The separator + fullFile));
P=new Properties ();
P.l oad (inputStream);
} the catch (Exception e) {
e.printStackTrace();
} the finally {
Try {
if (null ! InputStream)={
InputStream. Close ();
}
} the catch (Exception e) {
e.printStackTrace();
}
}
Return the p;
}

CodePudding user response:

DBPoolConnection. Class. GetClassLoader (). GetResource ("/" + fullFile)

CodePudding user response:

The second lines of the path that I should how to write?

CodePudding user response:

I want to change to relative path reads the properties, and so into the jar after operation is convenient
  • Related