How Java reads a configuration file-notes
1??????? Take the configuration file under the current Startup folder
?
In general, when you start a Java program. There will be a configuration file under the folder that starts
Classloader.getresource (""). GetFile ()? The folder that will fetch the current startup project for Java. Then specify the appropriate profile path for example conf/conf.properties
?
To take the current Startup folder's configuration file
String FilePath =classloader.getresource (""). GetFile () + "conf/conf.properties";
2??????? Take the configuration file under Classpath
If you do not consider a configuration file of the same name with the same path in multiple jar packages, you can directly take the following
?
Classloader.getsystemresource ("conf/conf.properties")//static method or Classloader.getresource ("Conf/conf.properties") //
?
?
?
Small instances
/** * Take the profile of the current Startup folder, assuming it is not loaded under current classpath. * @param classLoader * @param confclassdirectory * @return * @throws filenotfoundexce Ption */public static InputStream Getinputstreambyconfpath (ClassLoader classloader,stringconfclassdirect Ory) throws FileNotFoundException {if (Confclassdirectory.startswith ("/")) { confclassdirectory= confclassdirectory.substring (1); }//Take the configuration file for the current Startup folder String FilePath = Classloader.getresource (""). GetFile () + CONFCLASSDI Rectory; InputStream In=null; File File = new file (FilePath); Assume that there is no classpath under the current startup (!file.exists ()) {in= classloader.getresourceasst Ream (confclassdirectory); if (null = = in) {In=classloader.getresourceasstream ("/" + confclassdirectory); } }else{in=newfileinputstream (file); } return in; }
?
?
?
?
3??????? The configuration file in the jar package that contains the specified class
Sometimes A.jar has a file and a file with the same name as the B.jar. Path (for example: Conf/abc.txt),
It is assumed that the configuration file that is conf/abc.txt by Classpath can only fetch the first Jar package loaded is A.jar, but the B.jar is not.
?
Suppose this time you want to take the configuration file in B.jar can find the location of the jar package and then find the corresponding configuration file path is the file.
Such functions can be implemented such as the following
?
/** * Find the appropriate jar according to class to take the specified configuration file * @param CLS * @param confclassdirectory * @return * @throws IOException */public static InputStream Getinputstreambyjarfileconfpath (CLASS<?> Cls,stringconfclassdirectory) throws IOException {String jarpath=cls.getprotectiondomain (). GetCode Source (). GetLocation (). GetFile (); if (Confclassdirectory.startswith ("/")) {confclassdirectory= confclassdirectory.substring (1); } if (jarpath==null) {returnnull; } InputStream In=null; The inference hypothesis is that when the jar ends, the IF (Jarpath.endswith (". Jar")) is used in the server {jarfile jarfile = new Jarfile (Jarpath); Jarentry entry =jarfile.getjarentry (confclassdirectory); in= Jarfile.getinputstream (entry); }else{//is likely to be locally directly dependent on the project using the file File=new file (jarpath+confclassdirecto RY); if (file.exists ()) {In=newfileinputstream (file); }} return in; }}
?
Of course it seems to pass
Enumeration<url> urlss=classloader.getsystemresources ("conf/conf.properties"); while (Urlss.hasmoreelements ()) { System.out.println (urlss.nextelement (). GetFile ()); }
Take all of the conf/conf.properties's configuration files.
can also be inferred by inferring the path.
4??????? Take the configuration file under Class
?
With Class.getResource no/(underline) is from the current package to start looking, generally not recommended. After all, the configuration file configuration is not easy to say and, Maven seems to be packaged by default in the configuration file under the Src/main/java is not called in.
?
?
?
Add/(underline) is from the classpath root path to find
?
?
?
How Java reads a configuration file