A successful execution case:
Package com.test.properties;
Import java.io.IOException;
Import Java.io.InputStream;
Import java.util.Properties;
public class Testproperties {public
static void Main (string[] args) {
Properties Configprops = new properties (); c6/>string configfile = "Com/test/properties/info.properties";
try {
//Config File must is in classpath
ClassLoader cl = Thread.CurrentThread (). Getcontextclassloader ();
InputStream in = Cl.getresourceasstream (configfile);
Configprops.load (in);
In.close ();
} catch (IOException e) {
e.printstacktrace ();
}
/**
* Gets the absolute URI path
* Thread.CurrentThread (). Getcontextclassloader (). GetResource (""
) * Result: file:/c:/ documents%20and%20settings/dashan.yin/workspace/createpdf/webroot/web-inf/classes/
*
System.out.println (Thread.CurrentThread (). Getcontextclassloader (). GetResource (""));
System.out.println (configprops.getproperty ("url"));
}
Info.properties file:
Url=http://localhost:8081/webmonitor
Read properties file read, path, space, Chinese problem read properties file and path problem
such as: Read Jdbc.properties file
Path
The class being read is located at: Package com.query.util;
GetClass (). getResourceAsStream (Jdbc.properties)
The path described by Jdbc.properties is relative to the root path of the package where the class is located,
That is, relative to the directory where the folder util is located,
GetClass (). getResourceAsStream (/jdbc.properties) to/start
The path described by/jdbc.properties is relative to the current folder of this category.
That is, relative to the directory where the folder COM resides,
Methods of Reading
1. Use the Load () method of the Java.util.Properties class
Example: InputStream in = Lnew bufferedinputstream (new FileInputStream (name));
Properties P = new properties ();
P.load (in);
2. Using the Getbundle () method of the Java.util.ResourceBundle class
Example: ResourceBundle RB = Resourcebundle.getbundle (name, Locale.getdefault ());
3. Constructors using the Java.util.PropertyResourceBundle class
Example: InputStream in = new Bufferedinputstream (new FileInputStream (name));
ResourceBundle RB = new propertyResourceBundle (in);
4. getResourceAsStream () method with class variable
Example: InputStream in = JProperties.class.getResourceAsStream (name);
Properties P = new properties ();
P.load (in);
5. The getResourceAsStream () method of the Java.lang.ClassLoader obtained by using Class.getclassloader ()
Example: InputStream in = JProperties.class.getClassLoader (). getResourceAsStream (name);
Properties P = new properties ();
P.load (in);
6. Getsystemresourceasstream () static method using the Java.lang.ClassLoader class
Example: InputStream in = Classloader.getsystemresourceasstream (name);
Properties P = new properties ();
P.load (in);
Add
Javax.servlet.ServletContext getResourceAsStream () method can be used in the servlet
Example: InputStream in = context.getresourceasstream (path);
Properties P = new properties ();
P.load (in);
The fifth type is commonly used
Space
Get the file path and process the path
private static String GetUrl ()
{
String Path = ConfigLoad.class.getResource ("Config.properties"). ToString ();
Path = Path.replace ("%20", ""); A half-corner space in quotation marks
Path = path.substring (6);
return path;
}
}
So this returns a value of the properties type, where you can use GetProperty () to get the value
such as: Properties Pro = Configload.getconfig ();
String http = pro.getproperty ("url"). toString ();
Another way to achieve this:
1. Location of resource files
Resource files hinder classpath under the class package of the project
2. There are 2 ways to get system resource files
A. Through InputStream InputStream = Classloader.getsystemresourceasstream ("info.properties");
B. Through InputStream InputStream = This.getclass (). getResourceAsStream ("/info.properties");
When you obtain a resource file in the first way, the file does not start with "/", and the file must start with Method B.
3. Extract information to load a resource file
Properties Properties = new properties ();
InputStream InputStream = Classloader.getsystemresourceasstream ("info.properties");
InputStream InputStream = This.getclass (). getResourceAsStream ("/info.properties");
Properties.load (InputStream);
4. Operation Resource File
A. Querying value values in resource files based on key values
1. GetProperty (String key) searches for properties in this property list with the specified key.
2. GetProperty (string key, String defaultvalue) searches for properties in the list of properties with the specified key.
B. Get information about all the key-value pairs
Enumeration<string> enumvalue = (enumeration<string>) properties.propertynames ();//Returns an enumeration of all the keys in the property list, If a key with the same name is not found in the main list of properties, it includes the different keys in the default property list while
(Enumvalue.hasmoreelements ())
{
String key = Enumvalue.nextelement ();
SYSTEM.OUT.PRINTLN (key + ":" + properties.getproperty (key));
}
C. Add key value information to the resource file, overwriting the original information if the key value is the same
URL url = classloader.getsystemresource ("info.properties");
File File = new file (Url.touri ());
InputStream is = new FileInputStream (file);
Properties.load (is);
Properties.setproperty ("Key", "value");
OutputStream fos = new FileOutputStream (file);
Properties.store (FOS, null);
Fos.flush ();
Is.close ();
D. Delete related key-value pairs
File File = new file (Classloader.getsystemresource ("Info.properties"). Touri ());
InputStream is = new FileInputStream (file);
Properties.load (is);
Properties.remove ("key");
OutputStream fos = new FileOutputStream (file);
Properties.store (FOS, null);
Is.close ();
Fos.flush ();
Fos.close ();