/*************************************** * ** Method 1 ********************************* ******************************/
 
Public class demo {
// The resource file can be loaded to the memory by means of a Class Loader. The advantage of this method is that the program does not need to specify the specific directory of the configuration file. The program can automatically
// Search for the file in the src directory and load the file
// Another drawback of using the following method is that the configuration file loaded by the class loader cannot be too large.
// Also, the Class Loader loads the class only once, so when the class is loaded into the memory, if someone modifies the configuration information in the configuration file, then access
// When the configuration information is not changed, it is still the original one. If we want to get the updated configuration information, we cannot use this method to load the configuration information.
Private Static Properties prop = new properties ();
Static {
Inputstream in = demo. Class. getclassloader (). getresourceasstream ("DB. properties ");
Try {
Prop. Load (in );
} Catch (ioexception e ){
// Like the database configuration file, the entire program will not run if it is not loaded to the configuration file, so the exception here will be converted to an initialization error exception in actual development.
Throw new exceptionininitializererror (E );
}
}
}
 
/*************************************** * *** Second method ********************************* ******************************/
 
Class demo2 {
// The advantage of this method is that when someone changes the configuration file information, I access it again and get the changed information.
Private Static Properties prop = new properties ();
Static {
Try {
// Obtain the resource first
URL url = demo2.class. getclassloader (). getresource ("DB. properties ");
// Obtain the resource path
String Path = URL. getpath ();
// Obtain the stream related to the Resource
Fileinputstream in = new fileinputstream (PATH );
Prop. Load (in );
} Catch (ioexception e ){
// Like the database configuration file, the entire program will not run if it is not loaded to the configuration file, so the exception here will be converted to an initialization error exception in actual development.
Throw new exceptionininitializererror (E );
}
}
}