The following contents 1-4 are related knowledge collected online. To sum up, they are the following knowledge points:
1. The most commonly used method to read properties files InputStream in = getClass().getResourceAsStream("ResourceName"); This method requires that the properties file and the current class be in the same folder. If in a different package, you must use:
InputStream ins = this.getClass().getResourceAsStream("/cn/zhao/properties/testPropertiesPath2.properties");2. Get path method in Java
3. A simple implementation of obtaining paths
4. Three ways to obtain properties files in reflection
1 The most common methods and thoughts about obtaining properties files in reflection:
There are many ways to read properties files in Java. The most common articles on the Internet are "Six ways to read properties files in Java". However, in Java applications, the most commonly used method is to implement it through the getResourceAsStream(String name) method of the java.lang.Class class. However, I have seen that many codes that read properties files do this:
InputStream in = getClass().getResourceAsStream("ResourceName"); There is a problem here, that is, this is omitted by default when calling getClass()! We all know that this cannot be used in static (static) methods or static blocks. The reason is that static-type methods or code blocks belong to the class itself and do not belong to a certain object. This itself represents the current object, and the object is not initialized when static methods or blocks are called.
The problem is: if I don't want a certain class to have an object, then I will set the default constructor of this class to private, and of course I will not write other common constructors. And my class is a tool class, all of which are static methods and variables. I want to get properties files in static blocks or static methods, and this method won't work.
So what should I do? In fact, this class is not used like this. It just needs to get a Class object, which is not easy -
Taking the parent class Object of all classes, isn't it convenient and safer to use Object.class than using yours to write the class itself? Haha, here is an example to facilitate communication.
import java.util.Properties; import java.io.InputStream; import java.io.IOException; /** * Example of reading Properties files* File: TestProperties.java * User: leizhimin * Date: 2008-2-15 18:38:40 */ public final class TestProperties { private static String param1; private static String param2; static { Properties prop = new Properties(); InputStream in = Object. class .getResourceAsStream( "/test.properties" ); try { prop.load(in); param1 = prop.getProperty( "initYears1" ).trim(); param2 = prop.getProperty( "initYears2" ).trim(); } catch (IOException e) { e.printStackTrace(); } } /** * Private constructor, no object is needed*/ private TestProperties() { } public static String getParam1() { return param1; } public static String getParam2() { return param2; } public static void main(String args[]){ System.out.println(getParam1()); System.out.println(getParam2()); } }Running results:
151
152
Of course, it is still OK to replace Object.class with int.class. Haha, you can try it.
In addition, if you read the Properties file in a static method or block, there is another safest method, which is to directly obtain the Class object by the name of the class. For example, in this example, it can be written as TestProperties.class. This is the safest method.
2 How to get the path:
File fileB = new File( this .getClass().getResource( "" ).getPath()); System. out .println( "fileB path: " + fileB);
2.2 Get the project name of the current class:
System. out .println("user.dir path: " + System. getProperty ("user.dir"))3. A simple Java implementation to obtain the path
/** *Get the absolute path of the file under the relative path of the project* * @param parentDir *The parent directory of the target file, for example, in the project directory, there are lib, bin and conf directories, so the program runs in lib or * bin, and the required configuration file is in conf, you need to find the absolute path of the configuration file* @param fileName *File name* @return an absolute path*/ public static String getPath(String parentDir, String fileName) { String path = null; String userdir = System.getProperty("user.dir"); String userdirName = new File(userdir).getName(); if (userdirName.equalsIgnoreCase("lib") || userdirName.equalsIgnoreCase("bin")) { File newf = new File(userdir); File newp = new File(newf.getParent()); if (fileName.trim().equals("")) { path = newp.getPath() + File.separator + parentDir; } else { path = newp.getPath() + File.separator + parentDir + File.separator + fileName; } } else { if (fileName.trim().equals("")) { path = userdir + File.separator + parentDir; } else { path = userdir + File.separator + parentDir + File.separator + fileName; } } return path; } 4 Use reflection to obtain the path:
InputStream ips1 = Enumeration . class .getClassLoader() .getResourceAsStream( "cn/zhao/enumStudy/testPropertiesPath1.properties" ); InputStream ips2 = Enumeration . class .getResourceAsStream( "testPropertiesPath1.properties" ); InputStream ips3 = Enumeration . class .getResourceAsStream( "properties/testPropertiesPath2.properties" );
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.