Util

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Map;

public class Util{
	
	private static Map debugTimers = new HashMap();
	
	private static final String QUOTE = "'";

	public static void startTimer(String timerId){
		debugTimers.put(timerId, (new Date()));
	}
	
	public static String ReplaceText(String sInput, String sTarget, String sReplace) {
		int iLen = 0;
		
		if(sInput==null){
			return "";
		}
		String sRtnStr = sInput;		
		do {
			iLen = sInput.indexOf(sTarget, iLen);
			if (iLen == -1)
				break;
			sRtnStr = sInput.substring(0, iLen) + sReplace
					+ sInput.substring(iLen + sTarget.length());
			iLen += sReplace.length();
			sInput = sRtnStr;
		} while (true);
		return sRtnStr.substring(0, sRtnStr.length());
	}
	
	public static void stopTimer(String timerId){
		debug("\"" + timerId + "\" in " + (new Date().getTime() - ((Date)debugTimers.get(timerId)).getTime()) + " ms.");
		debugTimers.put(timerId, null);
	}
	
	public static void clickTimer(String timerId){
		if(debugTimers.get(timerId) == null)
			startTimer(timerId);
		else
			stopTimer(timerId);
	}
	
	/**
	 * get "a,b,c" from String[] of "a","b","c" 
	 */
	public static String join(String[] strings){
		if(strings == null)
			return "";
		StringBuffer buffer = new StringBuffer();
		for(int i = 0; i < strings.length; i ++){
			buffer.append(strings[i]);
			if(i+1 != strings.length)
				buffer.append(",");
		}
		return buffer.toString();
	}
	
	public static String join(String[] strs, String delim){
		return join(strs, delim);
	}
	/**
	 * join a collection of objects using each object's toString() method
	 */
	public static String join(Collection s, String delimiter) {
        StringBuffer buffer = new StringBuffer();
        Iterator iter = s.iterator();
        while (iter.hasNext()) {
            buffer.append(iter.next());
            if (iter.hasNext()) {
                buffer.append(delimiter);
            }
        }
        return buffer.toString();
    }
	
	public static String[] arrayList_2_StringArray(ArrayList al){
		if (isEmpty(al))
			return null;
		String[] out = new String[al.size()];
		for(int i=0; i

 

你可能感兴趣的:(Java)