Note: logger and error message configurations are indispensable for a project. The common solutions are provided.
-Create a configutils class and its corresponding graphic. properties file and test class.
Configutis:
Package COM.. io. ioexception; import Java. io. inputstream; import Java. util. properties; public class configutils {Private Static final string properties_file = "com/rah. properties "; Private Static Properties prop = NULL; static {inputstream propstream = configutils. class. getclassloader (). getresourceasstream (properties_file); prop = new properties (); try {prop. load (propstream);} catch (ioexception e) {system. out. println ("failed to read file") ;}} public static string getproperty (string key) {return prop. getproperty (key );}}
*
photoDir=d:/temp/photovideoDir=d:/temp/video
Test
package com.rah;public class Test { public static void main(String[] args) { String photoDir = ConfigUtils.getProperty("photoDir"); String videoDir = ConfigUtils.getProperty("videoDir"); System.out.println("photoDir Path is: " + photoDir); System.out.println("videoDir path is: " + videoDir); }}
Test results:
photoDir Path is: d:/temp/photovideoDir path is: d:/temp/video
2. Create the messagemanager class, message. properties, and test.
Messagemanager
Package com. Rah;
Import java. Io. ioexception;
Import java. Io. inputstream;
Import java. Text. messageformat;
Import java. util. properties;
Public class messagemanager {
Private Static final string properties_file = "/properties/message. properties ";
Private Static Properties prop = NULL;
Static {
Inputstream propstream = messagemanager. Class. getresourceasstream (properties_file );
Prop = new properties ();
Try {
Prop. Load (propstream );
} Catch (ioexception e ){
// Todo auto-generated Catch Block
System. Out. println ("failed to read the file ");
}
}
Public static string getproperty (string messagecode ){
Return prop. getproperty (messagecode );
}
Public static string getproperty (string messagecode, string arg1 ){
Object [] ARGs = new object [1];
ARGs [0] = arg1;
Return getformatmessage (messagecode, argS );
}
Public static string getproperty (string messagecode, string arg1, string arg2 ){
Object [] ARGs = new object [2];
ARGs [0] = arg1;
ARGs [1] = arg2;
Return getformatmessage (messagecode, argS );
}
Public static string getproperty (string messagecode, string arg1, string arg2, string arg3 ){
Object [] ARGs = new object [3];
ARGs [0] = arg1;
ARGs [1] = arg2;
ARGs [2] = arg3;
Return getformatmessage (messagecode, argS );
}
Private Static string getformatmessage (string messagecode, object [] ARGs ){
String argmessage = getproperty (messagecode );
Return messageformat. Format (argmessage, argS );
}
}
Message. Properties
MSG_E00001=password is not correct
MSG_E00002=country is {0}
MSG_E00003=country is {0} provice is {1}
MSG_E00004=country is {0} provice is {1} city is {2}
Test
package com.rah;public class Test { public static void main(String[] args) { System.out.println("MSG_E00001 data is: " + MessageManager.getProperty("MSG_E00001")); System.out.println("MSG_E00002 data is: " + MessageManager.getProperty("MSG_E00002", "CHINA")); System.out.println("MSG_E00003 data is: " + MessageManager.getProperty("MSG_E00003", "CHINA", "JIANGXI")); System.out.println("MSG_E00004 data is: " + MessageManager.getProperty("MSG_E00004", "CHINA", "JIANGXI", "SHANGRAO")); }}
Test results:
MSG_E00001 data is: password is not correctMSG_E00002 data is: country is CHINAMSG_E00003 data is: country is CHINA provice is JIANGXI MSG_E00004 data is: country is CHINA provice is JIANGXI city is SHANGRAO
3. Loger log output is actually a little encapsulation of log4j, which is convenient for developers to use
Download log4j-1.2.13.jar
public class Logger { private static org.apache.log4j.Logger logger = org.apache.log4j.Logger .getLogger(org.apache.log4j.Logger.class); public static void debug(String message) { logger.debug(message); } public static void debug(String message, Throwable ex) { logger.debug(message, ex); } public static void info(String message) { logger.info(message); } public static void info(String message, Throwable ex) { logger.info(message, ex); } public static void error(String message) { logger.error(message); } public static void error(String message, Throwable ex) { logger.error(message, ex); } public static void fatal(String message) { logger.fatal(message); } public static void fatal(String message, Throwable ex) { logger.fatal(message, ex); } public static void warn(String message) {http://i.cnblogs.com/EditPosts.aspx?opt=1 logger.warn(message); } public static void warn(String message, Throwable ex) {http://i.cnblogs.com/EditPosts.aspx?opt=1 logger.warn(message, ex); }}
Iv. Analysis of the differences between class. getresourceasstream () and Class. getclassloader (). getresourceasstream ()
The netizens will surely find that the two tests above use the class respectively. getresourceasstream (), and class. getclassloader (). getresourceasstream (). in fact, I didn't pay attention to it at the beginning. I found different methods when I checked the API, so I used different methods to try them.
Class. getresourceasstream () will specify the path of the loaded resource to be consistent with the path of the package where the current class is located.
If the above messagemanager class is written as getresourceasstream ("message. properties ") then it will only be in ciom. search under the "*" package. We start with "/", and check the root path of the classpath.Find (SRC root directory) getresourceasstream ("/properties/message. properties") and create the properties directory under the src directory.Message. properties file.
Classloader. gettresourceasstream () is searched from the root path of classpath no matter whether or not "/" is in front of the resource to be searched.
Like the configutil class getresourceasstream ("/rah. properties") and ("Rah. properties") above, they are all directly looking for the graphic. properties file from the src directory.
Final supplement:
The program runs the file that is finally compiled into. Class. Everything in the src directory is compiled in the bin directory.
Configuration of logger and message error messages in the project