1. Output stack information to the log file
Sometimes, when we run locally through the IDE, running a post-report error can see detailed stack information in the console. However, when the log output to a file, when encountering problems, but can not display the specific stack information, it is difficult to find specific problems and solve the problem. The following method is to output the stack information after the error to the log file.
/*** Output stack information to the log file *@authorgaoqing * 2015-2-1 *@paramT Exception Object *@returnStack Information*/ Public StaticString gettrace (Throwable t) {StringWriter StringWriter=NewStringWriter (); PrintWriter writer=NewPrintWriter (StringWriter); T.printstacktrace (writer); StringBuffer Buffer=Stringwriter.getbuffer (); returnbuffer.tostring (); } //Call Method: Try () {}catch (IOException e) e.printstacktrace (); //Logger.error (Gettrace (e));//output exception to file}
2. Rounding data of type Double
/*** Rounding of data of type Double *@authorgaoqing * 2014-12-2 *@paramInitValue Initial value *@paramscalenum number of digits to keep the decimal point *@paramTclass The Class of the data type returned after the number of decimal places is reserved@returnthe value after rounding T*/ Public Static<T> T Rounddoulevalue (Double initvalue,intScalenum, class<t>Tclass) {T T=NULL; String ClassName=tclass.getname (). toLowerCase (); if(InitValue = =NULL) { //The return type is: integer if(Classname.contains ("int") ) {T= (T) integer.valueof (0); //return type: Floating-point number}Else if(Classname.contains ("Dou") ) {T= (T) double.valueof (0.0); } }Else{BigDecimal BigDecimal=NewBigDecimal (InitValue); BigDecimal Scaledbigdecimal=Bigdecimal.setscale (Scalenum, bigdecimal.round_half_up); //The return type is: integer if(Classname.contains ("int") {Integer Tempinteger=NewInteger (Scaledbigdecimal.intvalue ()); T=(T) Tempinteger; //return type: Floating-point number}Else if(Classname.contains ("Dou") ) {Double tempdouble=NewDouble (Scaledbigdecimal.doublevalue ()); T=(T) tempdouble; } } returnT; }
3. Determine whether a thread with the specified name is executing in the current running environment
/*** Get all thread sets currently running * 2015-02-02 *@authorgaoqing *@returnthreads All thread sets that are currently running*/ Private Staticthread[] Getthreads () {//get the group where the current thread is locatedThreadgroup Currentthreadgroup =Thread.CurrentThread (). Getthreadgroup (); //top-Level group objects for thread groupsThreadgroup Topthreadgroup =Currentthreadgroup; //get the top-level group object for a thread group while(Currentthreadgroup! =NULL) {Topthreadgroup=Currentthreadgroup; Currentthreadgroup=currentthreadgroup.getparent (); } //estimating the number of threads in a thread group intPretopthreadgroupsize = Topthreadgroup.activecount () * 2; Thread[] Prethreads=NewThread[pretopthreadgroupsize]; //to copy a thread from a thread group into Prethreads intActualthreadsize =topthreadgroup.enumerate (prethreads); Thread[] Actualthreads=NewThread[actualthreadsize]; //Copy the thread in the estimated thread set to the actual size of the thread setSystem.arraycopy (prethreads, 0, actualthreads, 0, actualthreadsize); returnactualthreads; }
/*** Determine if the specified thread exists *@authorGaoqing * 2015-2-2 *@paramthreadname Thread Name *@returnisjudgespecifythreadexist whether the specified thread has an identity (default: false, not present)*/ Public Static Booleanjudgespecifythreadexist (String threadname) {BooleanIsjudgespecifythreadexist =false; //get all running set of threadsthread[] Threads =threadutils.getthreads (); if(ThreadName! =NULL&& threadname.length ()! = 0) { if(Threads! =NULL&& Threads.length! = 0) { for(Thread thread:threads) {if(Threadname.equals (Thread.getname ())) {Isjudgespecifythreadexist=true; } } } } returnisjudgespecifythreadexist; }
4. Get the content in the specified URL address
Public Staticstring geturlcontent (string url) {string str_data= ""; URL Urlobj=NULL; HttpURLConnection Connection=NULL; Reader Reader=NULL; Try{urlobj=Newurl (URL); Connection=(HttpURLConnection) urlobj.openconnection (); //determine if the current link is available intResponsecode =Connection.getresponsecode (); if(Responsecode = = 200) { //Connection.setrequestmethod ("POST"); //Connection.setdooutput (TRUE); //connection.setusecaches (false);Connection.connect (); InputStream InputStream=Connection.getinputstream (); Reader=NewInputStreamReader (InputStream, CharSet); BufferedReader BufferedReader=NewBufferedReader (reader); String Str=NULL; StringBuffer SB=NewStringBuffer (); while(str = bufferedreader.readline ())! =NULL) {sb.append (Str.trim ()); } str_data=sb.tostring (); //Determines whether the str_data is empty, and if it is empty, the output is an empty URL if("". Equals (str_data) | |Str_data.isempty ()) {log.info (URL+ "The link content is empty! "); } if(Reader! =NULL) { Try{reader.close (); } Catch(IOException e) {}}if(Connection! =NULL) {connection.disconnect (); } }Else{log.info ("Current link:" + URL + "not available!" "); } } Catch(IOException e) {log.info ("Current link:" + URL + "not available!" "); E.printstacktrace (); } finally { if(Reader! =NULL) { Try{reader.close (); } Catch(IOException e) {}}if(Connection! =NULL) {connection.disconnect (); } } returnStr_data; }
5. Deep Copy Objects
/** 1, instantiating an object * 2, serializing the current object into the object stream * 3, removing the current object from the object stream * 4, outputting the information of the object after the deep copy*/ //1. Instantiate an objectSerializableobj initialobj =NewSerializableobj ("Gaoqing",NewInteger (29)); //2. Serializes the current object into the object stream (the data is ultimately stored in the lowest stream)Bytearrayoutputstream BOS =NewBytearrayoutputstream (); ObjectOutputStream Oos=NewObjectOutputStream (BOS); Oos.writeobject (Initialobj); //3. Remove the current object from the object streamBytearrayinputstream bis =NewBytearrayinputstream (Bos.tobytearray ()); ObjectInputStream Ois=NewObjectInputStream (bis); Serializableobj Seriobj=(Serializableobj) ois.readobject (); System.out.println (seriobj);
Developing common code blocks