//将servicebus.xml内容解析为字符串
//"servdfbus.xml","UTF-8"
String strBusinessServiceBusXML=Utility.readTextResource(strServiceBusXMLFile,encoding);
package com.cdoframework.cdolib.base; import java.io.BufferedReader; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.OutputStream; import java.io.Reader; import java.io.Serializable; import java.io.StringReader; import java.net.InetAddress; import java.net.NetworkInterface; import java.net.Socket; import java.net.SocketException; import java.net.UnknownHostException; import java.nio.channels.FileChannel; import java.text.DecimalFormat; import java.util.ArrayList; import java.util.Calendar; import java.util.Enumeration; import java.util.HashSet; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Set; public class Utility { protected static DecimalFormat decFormat = new DecimalFormat(); public static boolean hasSameString(String[] strsString) { HashSet hsString = new HashSet(); for (int i = 0; i < strsString.length; i++) { if (!hsString.add(strsString[i])) { return true; } } return false; } public static int findString(String[] strsString, String strValue) { for (int i = 0; i < strsString.length; i++) { if (strsString[i].equals(strValue)) { return i; } } return -1; } public static String connectString(char chSeperator, String[] strsString) { StringBuilder strbOutput = new StringBuilder(); for (int i = 0; i < strsString.length; i++) { if (i > 0) { strbOutput.append(chSeperator); } strbOutput.append(strsString[i]); } return strbOutput.toString(); } public static String[] splitString(String strSource, char chSeperator) { ArrayList alOutput = new ArrayList(); StringBuilder strbString = new StringBuilder(); for (int i = 0; i < strSource.length(); i++) { char chChar = strSource.charAt(i); if (chChar == chSeperator) { alOutput.add(strbString.toString()); strbString = new StringBuilder(); } else { strbString.append(chChar); } } alOutput.add(strbString.toString()); String[] strsOutput = new String[alOutput.size()]; for (int i = 0; i < alOutput.size(); i++) { strsOutput[i] = ((String) alOutput.get(i)); } return strsOutput; } public static String[] splitString(String strSource, char chSeperator, boolean bRepeated) { ArrayList alOutput = new ArrayList(); StringBuilder strbString = new StringBuilder(); char chLastChar = '\000'; for (int i = 0; i < strSource.length(); i++) { char chChar = strSource.charAt(i); if (chChar == chSeperator) { if (bRepeated) { if (chChar != chLastChar) { alOutput.add(strbString.toString()); strbString = new StringBuilder(); } } else { alOutput.add(strbString.toString()); strbString = new StringBuilder(); } } else { strbString.append(chChar); } chLastChar = chChar; } alOutput.add(strbString.toString()); String[] strsOutput = new String[alOutput.size()]; for (int i = 0; i < alOutput.size(); i++) { strsOutput[i] = ((String) alOutput.get(i)); } return strsOutput; } public static String[] readLine(String str) { List list = new ArrayList(10); BufferedReader reader = new BufferedReader(new StringReader(str)); String strContent = null; try { while ((strContent = reader.readLine()) != null) { list.add(strContent); } } catch (IOException e) { return null; } finally { try { reader.close(); } catch (IOException localIOException2) { } } String[] strReturn = new String[list.size()]; for (int i = 0; i < list.size(); i++) { strReturn[i] = ((String) list.get(i)); } return strReturn; } public static String encodingText(String strText, String strFromCoding, String strToCoding) { if (strFromCoding.equalsIgnoreCase(strToCoding)) { return strText; } try { return new String(strText.getBytes(strFromCoding), strToCoding); } catch (Exception e) { } return null; } public static String noNull(String strValue) { if (strValue == null) { return ""; } return strValue; } public static String readTextFile(String strFile) { FileInputStream stream = null; InputStreamReader reader = null; try { stream = new FileInputStream(strFile); reader = new InputStreamReader(stream); char[] chsData = new char[(int) stream.getChannel().size()]; int nReadSize = reader.read(chsData, 0, chsData.length); String str = new String(chsData, 0, nReadSize); return str; } catch (Exception e) { return null; } finally { try { if (reader != null) { reader.close(); } if (stream != null) { stream.close(); } } catch (Exception localException3) { } } throw localObject; } public static String readTextFile(String strFile, String strCoding) { FileInputStream stream = null; InputStreamReader reader = null; try { stream = new FileInputStream(strFile); reader = new InputStreamReader(stream, strCoding); char[] chsData = new char[(int) stream.getChannel().size()]; int nReadSize = reader.read(chsData, 0, chsData.length); String str = new String(chsData, 0, nReadSize); return str; } catch (Exception e) { return null; } finally { try { if (reader != null) { reader.close(); } if (stream != null) { stream.close(); } } catch (Exception localException3) { } } throw localObject; } public static String readTextResource(String strFile, String strCoding) { InputStream stream = null; InputStreamReader reader = null; try { stream = Resources.getResourceAsStream(strFile); reader = new InputStreamReader(stream, strCoding); StringBuilder strbContent = new StringBuilder(); char[] chsData = new char[1024]; while (true) { int nReadSize = reader.read(chsData, 0, chsData.length); if (nReadSize <= 0) { break; } strbContent.append(new String(chsData, 0, nReadSize)); } String str = strbContent.toString(); return str; } catch (Exception e) { return null; } finally { try { if (reader != null) { reader.close(); } } catch (Exception localException5) { } if (stream != null) { try { stream.close(); } catch (Exception localException6) { } } } throw localObject; } public static String readTextResource(String strFile) { InputStream stream = null; InputStreamReader reader = null; try { stream = Resources.getResourceAsStream(strFile); reader = new InputStreamReader(stream); StringBuilder strbContent = new StringBuilder(); char[] chsData = new char[10240]; while (true) { int nReadSize = reader.read(chsData, 0, chsData.length); if (nReadSize <= 0) { break; } strbContent.append(new String(chsData, 0, nReadSize)); } String str = strbContent.toString(); return str; } catch (Exception e) { return null; } finally { try { if (reader != null) { reader.close(); } } catch (Exception localException5) { } if (stream != null) { try { stream.close(); } catch (Exception localException6) { } } } throw localObject; } public static boolean IsInstanceOf(Object obj, String strClassName) { try { return Class.forName(strClassName).isInstance(obj); } catch (Exception e) { } return false; } public static byte[] readFile(String strFile) { FileInputStream stream = null; try { stream = new FileInputStream(strFile); byte[] bysData = new byte[(int) stream.getChannel().size()]; stream.read(bysData); byte[] arrayOfByte1 = bysData; return arrayOfByte1; } catch (Exception e) { return null; } finally { try { if (stream != null) { stream.close(); } } catch (Exception localException3) { } } throw localObject; } public static void writeFile(String strFile, byte[] bysData) { FileOutputStream stream = null; try { stream = new FileOutputStream(strFile); stream.write(bysData, 0, bysData.length); } catch (Exception e) { return; } finally { try { if (stream != null) { stream.close(); } } catch (Exception localException2) { } } } public static void writeFile(File file, byte[] bysData) { FileOutputStream stream = null; try { stream = new FileOutputStream(file); stream.write(bysData, 0, bysData.length); } catch (Exception e) { return; } finally { try { if (stream != null) { stream.close(); } } catch (Exception localException2) { } } } public static void closeStream(InputStream stream) { if (stream != null) { try { stream.close(); } catch (Exception localException) { } } } public static void closeStream(OutputStream stream) { if (stream != null) { try { stream.close(); } catch (Exception localException) { } } } public static void close(Socket socket) { if (socket != null) { try { socket.close(); } catch (Exception localException) { } } } public static Return moveFile(String strFromFile, String strToFile) { File fileFrom = new File(strFromFile); File fileTo = new File(strToFile); if (!fileFrom.renameTo(fileTo)) { return Return.valueOf(-1, "Move file failed"); } return Return.OK; } public static String getHostName() { String strHostName = null; try { InetAddress netAddress = InetAddress.getLocalHost(); strHostName = netAddress.getHostName(); } catch (Exception localException) { } return strHostName; } public static String getIPAddress() { String strIPAddress = null; try { InetAddress netAddress = InetAddress.getLocalHost(); strIPAddress = netAddress.getHostAddress(); } catch (Exception localException) { } return strIPAddress; } public static String getLocalIp() { String strIp = null; try { InetAddress netAddress = InetAddress.getLocalHost(); strIp = netAddress.getHostAddress(); if (!strIp.startsWith("127")) { return strIp; } } catch (UnknownHostException netInterfaces) { Enumeration netInterfaces = null; try { netInterfaces = NetworkInterface.getNetworkInterfaces(); } catch (SocketException e) { return strIp; } InetAddress ip = null; while (netInterfaces.hasMoreElements()) { NetworkInterface ni = (NetworkInterface) netInterfaces.nextElement(); Enumeration enumInetAddress = ni.getInetAddresses(); while (enumInetAddress.hasMoreElements()) { ip = (InetAddress) enumInetAddress.nextElement(); strIp = ip.getHostAddress(); if ((!strIp.startsWith("127")) && (!strIp.startsWith("l")) && (!strIp.startsWith("L"))) { return strIp; } } } } return strIp; } public static byte[] hexStringToBytes(String strHexString) { String strDigital = "0123456789ABCDEF"; byte[] bytes = new byte[strHexString.length() / 2]; for (int i = 0; i < bytes.length; i++) { int temp = strDigital.indexOf(strHexString.substring(2 * i, (2 * i) + 1)) * 16; temp += strDigital.indexOf(strHexString.substring((2 * i) + 1, (2 * i) + 2)); bytes[i] = (byte) (temp & 0xFF); } return bytes; } public static String bytesToHexString(byte[] bysBytes) { String strDigital = "0123456789ABCDEF"; StringBuilder sb = new StringBuilder(""); byte[] bs = bysBytes; for (int i = 0; i < bs.length; i++) { int bit = (bs[i] & 0xF0) >> 4; sb.append(strDigital.substring(bit, bit + 1)); bit = bs[i] & 0xF; sb.append(strDigital.substring(bit, bit + 1)); } return sb.toString(); } public static void closeReader(Reader reader) { if (reader == null) { return; } try { reader.close(); } catch (Exception localException) { } } public static String getExceptionMessage(Exception e) { StringBuilder strbMessage = new StringBuilder(1024); StackTraceElement[] ste = e.getStackTrace(); strbMessage.append(e.getMessage()); for (int i = 0; i < ste.length; i++) { strbMessage.append("\r\n"); strbMessage.append(ste[i].toString()); } return strbMessage.toString(); } public static String makeSameCharString(char ch, int nLength) { char[] chsOutput = new char[nLength]; for (int i = 0; i < nLength; i++) { chsOutput[i] = ch; } return new String(chsOutput); } public static String format(Object obj, String strFormat) throws Exception { if (IsInstanceOf(obj, "java.lang.String")) { String strObj = (String) obj; if ((strFormat == null) || (strFormat.length() == 0)) { return strObj; } String[] strsFormatItem = splitString(strFormat, ','); int nLength = 0; char chFill = ' '; int nFillAt = 0; if (strsFormatItem.length >= 1) { nLength = Integer.parseInt(strsFormatItem[0]); } if (strsFormatItem.length >= 2) { if (strsFormatItem[1].length() != 1) { throw new Exception("Unsupported format: " + strFormat); } chFill = strsFormatItem[1].charAt(0); } if (strsFormatItem.length >= 3) { nFillAt = Integer.parseInt(strsFormatItem[2]); if ((nFillAt != 0) && (nFillAt != 1)) { throw new Exception("Unsupported format: " + strFormat); } } else { throw new Exception("Unsupported format: " + strFormat); } if (strObj.length() >= nLength) { return strObj; } String strOutput = ""; if (nFillAt == 0) { strOutput = makeSameCharString(chFill, nLength - strObj.length()) + strObj; } else { strOutput = strObj + makeSameCharString(chFill, nLength - strObj.length()); } return strOutput; } if ((IsInstanceOf(obj, "java.lang.Byte")) || (IsInstanceOf(obj, "java.lang.Integer")) || (IsInstanceOf(obj, "java.lang.Long"))) { String strObj = obj.toString(); if ((strFormat == null) || (strFormat.length() == 0)) { return strObj; } String[] strsFormatItem = splitString(strFormat, ','); int nLength = 0; char chFill = ' '; int nFillAt = 0; if (strsFormatItem.length >= 1) { nLength = Integer.parseInt(strsFormatItem[0]); } if (strsFormatItem.length >= 2) { if (strsFormatItem[1].length() != 1) { throw new Exception("Unsupported format: " + strFormat); } chFill = strsFormatItem[1].charAt(0); } if (strsFormatItem.length >= 3) { nFillAt = Integer.parseInt(strsFormatItem[2]); if ((nFillAt != 0) && (nFillAt != 1)) { throw new Exception("Unsupported format: " + strFormat); } } else { throw new Exception("Unsupported format: " + strFormat); } if (strObj.length() >= nLength) { return strObj; } String strOutput = ""; if (nFillAt == 0) { strOutput = makeSameCharString(chFill, nLength - strObj.length()) + strObj; } else { strOutput = strObj + makeSameCharString(chFill, nLength - strObj.length()); } return strOutput; } if (IsInstanceOf(obj, "EZLib.Base.EZDateTime")) { return ((DateTime) obj).toString(strFormat); } throw new Exception("Unsupported format: " + strFormat); } public static String formatArray(Object objArray, int nIndex, String strFormat) throws Exception { if (!objArray.getClass().isArray()) { throw new Exception("Object not an array"); } Object obj = null; if (IsInstanceOf(objArray, "[B")) { byte[] bysObj = (byte[]) objArray; obj = new Byte(bysObj[nIndex]); } else if (IsInstanceOf(objArray, "[I")) { int[] nsObj = (int[]) objArray; obj = new Integer(nsObj[nIndex]); } else if (IsInstanceOf(objArray, "[J")) { long[] lsObj = (long[]) objArray; obj = new Long(lsObj[nIndex]); } else if (IsInstanceOf(objArray, "[Ljava.lang.String")) { String[] strsObj = (String[]) objArray; obj = strsObj[nIndex]; } else if (IsInstanceOf(objArray, "[LEZLib.Base.EZDateTime")) { DateTime[] dtsObj = (DateTime[]) objArray; obj = dtsObj[nIndex]; } else { throw new Exception("Unsupported array: " + objArray.getClass().getName()); } if (IsInstanceOf(obj, "java.lang.String")) { String strObj = (String) obj; if ((strFormat == null) || (strFormat.length() == 0)) { return strObj; } String[] strsFormatItem = splitString(strFormat, ','); int nLength = 0; char chFill = ' '; int nFillAt = 0; if (strsFormatItem.length >= 1) { nLength = Integer.parseInt(strsFormatItem[0]); } if (strsFormatItem.length >= 2) { if (strsFormatItem[1].length() != 1) { throw new Exception("Unsupported format: " + strFormat); } chFill = strsFormatItem[1].charAt(0); } if (strsFormatItem.length >= 3) { nFillAt = Integer.parseInt(strsFormatItem[2]); if ((nFillAt != 0) && (nFillAt != 1)) { throw new Exception("Unsupported format: " + strFormat); } } else { throw new Exception("Unsupported format: " + strFormat); } if (strObj.length() >= nLength) { return strObj; } String strOutput = ""; if (nFillAt == 0) { strOutput = makeSameCharString(chFill, nLength - strObj.length()) + strObj; } else { strOutput = strObj + makeSameCharString(chFill, nLength - strObj.length()); } return strOutput; } if ((IsInstanceOf(obj, "java.lang.Byte")) || (IsInstanceOf(obj, "java.lang.Integer")) || (IsInstanceOf(obj, "java.lang.Long"))) { String strObj = obj.toString(); if ((strFormat == null) || (strFormat.length() == 0)) { return strObj; } String[] strsFormatItem = splitString(strFormat, ','); int nLength = 0; char chFill = ' '; int nFillAt = 0; if (strsFormatItem.length >= 1) { nLength = Integer.parseInt(strsFormatItem[0]); } if (strsFormatItem.length >= 2) { if (strsFormatItem[1].length() != 1) { throw new Exception("Unsupported format: " + strFormat); } chFill = strsFormatItem[1].charAt(0); } if (strsFormatItem.length >= 3) { nFillAt = Integer.parseInt(strsFormatItem[2]); if ((nFillAt != 0) && (nFillAt != 1)) { throw new Exception("Unsupported format: " + strFormat); } } else { throw new Exception("Unsupported format: " + strFormat); } if (strObj.length() >= nLength) { return strObj; } String strOutput = ""; if (nFillAt == 0) { strOutput = makeSameCharString(chFill, nLength - strObj.length()) + strObj; } else { strOutput = strObj + makeSameCharString(chFill, nLength - strObj.length()); } return strOutput; } if (IsInstanceOf(obj, "EZLib.Base.EZDateTime")) { return ((DateTime) obj).toString(strFormat); } throw new Exception("Unsupported format: " + strFormat); } public static String writeTempFile(String strPrefix, String strPostfix, byte[] bysContent) { File fileTemp = null; try { fileTemp = File.createTempFile(strPrefix, strPostfix); writeFile(fileTemp, bysContent); } catch (Exception e) { return null; } return fileTemp.getAbsolutePath(); } public static boolean isLeapYear(int nYear) { if ((nYear % 100) == 0) { return (nYear % 400) == 0; } return (nYear % 4) == 0; } public static boolean checkDate(String strValue) { int nLength = strValue.length(); if (nLength == 0) { return true; } if (nLength != 10) { return false; } if ((strValue.charAt(4) != '-') && (strValue.charAt(7) != '-')) { return false; } String strYear = strValue.substring(0, 4); String strMonth = strValue.substring(5, 7); String strDay = strValue.substring(8); if ((!isIntText(strYear)) || (!isIntText(strMonth)) || (!isIntText(strDay))) { return false; } int nMonth = Integer.parseInt(strMonth); if ((nMonth < 1) || (nMonth > 12)) { return false; } int nDay = Integer.parseInt(strDay); if ((nDay < 1) || (nDay > 31)) { return false; } if ((nMonth == 1) || (nMonth == 3) || (nMonth == 5) || (nMonth == 7) || (nMonth == 8) || (nMonth == 10) || (nMonth == 12)) { return true; } if ((nMonth == 4) || (nMonth == 6) || (nMonth == 9) || (nMonth == 11)) { return nDay <= 30; } int nYear = Integer.parseInt(strYear); if ((!isLeapYear(nYear)) && (nDay > 28)) { return false; } return (!isLeapYear(nYear)) || (nDay <= 29); } public static boolean checkTime(String strValue) { int nLength = strValue.length(); if (nLength == 0) { return true; } if (nLength != 8) { return false; } if ((strValue.charAt(2) != ':') && (strValue.charAt(5) != ':')) { return false; } String strHour = strValue.substring(0, 2); String strMinute = strValue.substring(3, 5); String strSecond = strValue.substring(6); if ((!isIntText(strHour)) || (!isIntText(strMinute)) || (!isIntText(strSecond))) { return false; } int nHour = Integer.parseInt(strHour); if ((nHour < 0) || (nHour > 23)) { return false; } int nMinute = Integer.parseInt(strMinute); if ((nMinute < 0) || (nMinute > 59)) { return false; } int nSecond = Integer.parseInt(strSecond); return (nSecond >= 0) && (nSecond <= 59); } public static boolean checkDateTime(String strValue) { if (strValue.length() != 19) { return false; } if (strValue.charAt(10) != ' ') { return false; } String strDate = strValue.substring(0, 10); if (!checkDate(strDate)) { return false; } String strTime = strValue.substring(11); return checkTime(strTime); } public static boolean isDateArray(String[] strsDate) { if (strsDate == null) { return false; } for (int i = 0; i < strsDate.length; i++) { if (!strsDate[i].matches("([0-9]{4}-[0-9]{2}-[0-9]{2})?")) { return false; } } return true; } public static boolean isTimeArray(String[] strsTime) { if (strsTime == null) { return false; } for (int i = 0; i < strsTime.length; i++) { if (!strsTime[i].matches("([0-9]{2}:[0-9]{2}:[0-9]{2})?")) { return false; } } return true; } public static boolean isDateTimeArray(String[] strsDateTime) { if (strsDateTime == null) { return false; } for (int i = 0; i < strsDateTime.length; i++) { if (!strsDateTime[i].matches( "([0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2})?")) { return false; } } return true; } public static int findMatchedChar(int nIndex, String strText) { if (nIndex < 0) { return -1; } char[] chsText = strText.toCharArray(); char chChar = chsText[nIndex]; int nCount = 0; int nStartIndex = -1; int nEndIndex = -1; char chFind = ' '; switch (chChar) { case '(': chFind = ')'; break; case '{': chFind = '}'; break; case '[': chFind = ']'; break; case ')': chFind = '('; break; case '}': chFind = '{'; break; case ']': chFind = '['; break; default: return -1; } int nLength = chsText.length; switch (chChar) { case '(': case '[': case '{': for (int i = nIndex + 1; i < nLength; i++) { char ch = chsText[i]; if (ch == chChar) { nCount++; } else { if (ch != chFind) { continue; } if (nCount == 0) { nEndIndex = i; break; } nCount--; } } return nEndIndex; case ')': case ']': case '}': for (int i = nIndex - 1; i >= 0; i--) { char ch = chsText[i]; if (ch == chChar) { nCount++; } else { if (ch != chFind) { continue; } if (nCount == 0) { nStartIndex = i; break; } nCount--; } } return nStartIndex; } return -1; } public static int findMatchedChar(int nIndex, char[] chsText) { if (nIndex < 0) { return -1; } char chChar = chsText[nIndex]; int nCount = 0; int nStartIndex = -1; int nEndIndex = -1; char chFind = ' '; switch (chChar) { case '(': chFind = ')'; break; case '{': chFind = '}'; break; case '[': chFind = ']'; break; case ')': chFind = '('; break; case '}': chFind = '{'; break; case ']': chFind = '['; break; default: return -1; } int nLength = chsText.length; switch (chChar) { case '(': case '[': case '{': for (int i = nIndex + 1; i < nLength; i++) { char ch = chsText[i]; if (ch == chChar) { nCount++; } else { if (ch != chFind) { continue; } if (nCount == 0) { nEndIndex = i; break; } nCount--; } } return nEndIndex; case ')': case ']': case '}': for (int i = nIndex - 1; i >= 0; i--) { char ch = chsText[i]; if (ch == chChar) { nCount++; } else { if (ch != chFind) { continue; } if (nCount == 0) { nStartIndex = i; break; } nCount--; } } return nStartIndex; } return -1; } public static String[] getDirFileList(String strDirOrFile) { File file = null; try { file = new File(strDirOrFile); String[] strsFile = (String[]) null; if (file.isFile()) { strsFile = new String[1]; strsFile[0] = strDirOrFile; return strsFile; } File[] filesList = file.listFiles(); strsFile = new String[filesList.length]; for (int i = 0; i < filesList.length; i++) { String strFile = filesList[i].getAbsolutePath(); strsFile[i] = strFile; } return strsFile; } catch (Exception e) { } return null; } public static String mapToString(Map map) { Iterator iterator = map.keySet().iterator(); StringBuilder sb = new StringBuilder(); String key = null; String value = null; while (iterator.hasNext()) { key = (String) iterator.next(); value = map.get(key).toString(); sb.append(key).append("=").append(value).append(";"); } return sb.toString(); } public static int getSecondOfDay() { Calendar cal = Calendar.getInstance(); int nReturn = cal.get(13); nReturn += (cal.get(12) * 60); nReturn += (cal.get(11) * 3600); return nReturn; } public static int getWeekDay() { Calendar cal = Calendar.getInstance(); int nReturn = cal.get(7); return nReturn - 1; } public static String formatIPV4(String strIp) { if (strIp == null) { return null; } String[] strs = strIp.split("\\."); StringBuilder sb = new StringBuilder(); for (int i = 0; i < strs.length; i++) { if (i > 0) { sb.append("."); } int len = strs[i].length(); if (len == 1) { sb.append("00"); } else if (len == 2) { sb.append("0"); } sb.append(strs[i]); } return sb.toString(); } public static int getFirstWord(String strText, StringBuilder strWord) { strWord.setLength(0); int nIndex = 0; for (int i = 0; i < strText.length(); i++) { char chChar = strText.charAt(i); if ((chChar == ' ') || (chChar == '\t')) { if (strWord.length() > 0) { break; } nIndex++; } else { strWord.append(chChar); } } if (strWord.length() == 0) { nIndex = 0; } return nIndex; } public static String subStr(String strSource, int nIndex, int nLength) { int nSize = strSource.length(); if (nIndex >= nSize) { return ""; } if ((nIndex + nLength) >= nSize) { return strSource.substring(nIndex); } return strSource.substring(nIndex, nIndex + nLength); } public static boolean isIntText(String strText) { strText = strText.trim(); if (strText.length() == 0) { return false; } for (int i = 0; i < strText.length(); i++) { if ((strText.charAt(i) < '0') || (strText.charAt(i) > '9')) { return false; } } return true; } public static boolean isIntTextSet(String strText) { strText = strText.trim(); if (strText.length() == 0) { return false; } for (int i = 0; i < strText.length(); i++) { if (((strText.charAt(i) < '0') || (strText.charAt(i) > '9')) && (strText.charAt(i) != ' ')) { return false; } } return true; } public static String intToString(long nValue) { Long intTemp = new Long(nValue); return intTemp.toString(); } public static String intToString(long nValue, int nLength) { StringBuilder strbFormat = new StringBuilder(32); for (int i = 0; i < nLength; i++) { strbFormat.append('0'); } synchronized (decFormat) { decFormat.applyPattern(strbFormat.toString()); return decFormat.format(nValue); } } public static boolean isNumberText(String strText, int nDigitCount) { if (strText.length() == 0) { return false; } int nDotCount = 0; int nDotPos = strText.length(); for (int i = 0; i < strText.length(); i++) { if (strText.charAt(i) == '.') { if (nDotCount > 0) { return false; } if ((i + 3) < strText.length()) { return false; } nDotCount++; nDotPos = i; } else if ((strText.charAt(i) < '0') || (strText.charAt(i) > '9')) { return false; } } return true; } public static boolean isNumberText(String strText) { if (strText.length() == 0) { return false; } for (int i = 0; i < strText.length(); i++) { if ((strText.charAt(i) < '0') || (strText.charAt(i) > '9')) { return false; } } return true; } public static long numberTextToInteger(String strText, int nDigitCount) throws Exception { if (strText.length() == 0) { throw new Exception(""); } long lValue = 0L; int nDotCount = 0; int nDotPos = strText.length(); for (int i = 0; i < strText.length(); i++) { if (strText.charAt(i) == '.') { if (nDotCount > 0) { throw new Exception(""); } if ((i + nDigitCount + 1) < strText.length()) { throw new Exception(""); } nDotCount++; nDotPos = i; } else { if ((strText.charAt(i) < '0') || (strText.charAt(i) > '9')) { throw new Exception(""); } if ((nDotCount > 0) && ((i - nDotPos) > nDigitCount)) { throw new Exception(""); } lValue = (lValue * 10L) + (strText.charAt(i) - '0'); } } if (nDotCount == 0) { for (int i = 0; i < nDigitCount; i++) { lValue *= 10L; } return lValue; } for (int i = 0; i < (nDigitCount - (strText.length() - nDotPos - 1)); i++) { lValue *= 10L; } return lValue; } public static String integerToNumberText(long lValue, int nDigitCount) { String strDigit = ""; long lTemp = lValue; for (int i = 0; i < nDigitCount; i++) { if (lTemp >= 0L) { strDigit = (lTemp % 10L) + strDigit; } else { strDigit = (-lTemp % 10L) + strDigit; } lTemp /= 10L; } if (strDigit.length() > 0) { if ((lTemp == 0L) && (lValue < 0L)) { return 45L + lTemp + 46L + strDigit; } return lTemp + '.' + strDigit; } return lTemp + strDigit; } public static Serializable deepClone(Serializable obj) { Serializable objOutput = null; byte[] bysObject = (byte[]) null; ObjectOutputStream out = null; ByteArrayOutputStream streamOutput = new ByteArrayOutputStream(); try { out = new ObjectOutputStream(streamOutput); out.writeObject(obj); bysObject = streamOutput.toByteArray(); } catch (Exception e) { return null; } finally { try { out.close(); } catch (Exception localException3) { } try { streamOutput.close(); } catch (Exception localException4) { } } ByteArrayInputStream streamInput = new ByteArrayInputStream(bysObject); ObjectInputStream in = null; try { in = new ObjectInputStream(streamInput); objOutput = (Serializable) in.readObject(); } catch (Exception e) { return null; } finally { try { in.close(); } catch (Exception localException9) { } try { streamInput.close(); } catch (Exception localException10) { } } return objOutput; } public static int compareLong(Object objValue1, Object objValue2) { Long value1 = parseLongValue(objValue1); Long value2 = parseLongValue(objValue2); return value1.compareTo(value2); } public static int compareDouble(Object objValue1, Object objValue2) { Double value1 = Double.valueOf(objValue1.toString()); Double value2 = Double.valueOf(objValue2.toString()); return value1.compareTo(value2); } public static int compareString(Object objValue1, Object objValue2) { String strValue1 = objValue1.toString(); String strValue2 = objValue2.toString(); return strValue1.compareTo(strValue2); } public static Object parseObjectValue(int nType, Object source) { if (source == null) { return null; } switch (nType) { case 8: return source.toString(); case 4: return parseIntegerValue(source); case 5: return parseLongValue(source); case 11: return parseDateTimeValue(source); case 9: return parseDateValue(source); case 10: return parseTimeValue(source); case 6: return parseFloatValue(source); case 7: return parseDoubleValue(source); case 1: return parseBooleanValue(source); case 2: return parseByteValue(source); case 3: return parseShortValue(source); case 102: return parseByteArrayValue(source); } throw new RuntimeException("invalid type " + nType); } public static String parseStingValue(Object source) { if (source == null) { return null; } return source.toString(); } public static Integer parseIntegerValue(Object source) { if (source == null) { return null; } if ((source instanceof Integer)) { return (Integer) source; } return Integer.valueOf(Long.valueOf(source.toString()).intValue()); } public static int[] parseIntegerArrayValue(Object source) { if (source == null) { return null; } if ((source instanceof int[])) { return (int[]) source; } if ((source instanceof Object[])) { Object[] objs = (Object[]) source; int[] values = new int[objs.length]; for (int i = 0; i < objs.length; i++) { values[i] = Long.valueOf(objs[i].toString()).intValue(); } return values; } return new int[] { Long.valueOf(source.toString()).intValue() }; } public static Long parseLongValue(Object source) { if (source == null) { return null; } if ((source instanceof Long)) { return (Long) source; } return new Long(source.toString()); } public static long[] parseLongArrayValue(Object source) { if (source == null) { return null; } if ((source instanceof int[])) { return (long[]) source; } if ((source instanceof Object[])) { Object[] objs = (Object[]) source; long[] values = new long[objs.length]; for (int i = 0; i < objs.length; i++) { values[i] = new Long(objs[i].toString()).longValue(); } return values; } return new long[] { new Long(source.toString()).longValue() }; } public static String parseDateTimeValue(Object source) { if (source == null) { return null; } if ((source instanceof String)) { String strValue = source.toString(); if (checkDateTime(strValue)) { return strValue; } if (checkDate(strValue)) { return strValue + " 00:00:00"; } throw new RuntimeException("Invalid date format " + source); } if ((source instanceof java.util.Date)) { java.util.Date temp = (java.util.Date) source; return new DateTime(temp.getYear(), temp.getMonth(), temp.getDate(), temp.getHours(), temp.getMinutes(), temp.getSeconds()).toString(); } if ((source instanceof DateTime)) { return source.toString(); } if ((source instanceof Date)) { return ((Date) source).toString() + " 00:00:00"; } throw new RuntimeException("Invalid date format " + source); } public static String parseDateValue(Object source) { if (source == null) { return null; } if ((source instanceof String)) { String strValue = source.toString(); if (checkDate(strValue)) { return strValue; } if (checkDateTime(strValue)) { return strValue.substring(0, 10); } throw new RuntimeException("Invalid date format"); } if ((source instanceof java.util.Date)) { java.util.Date temp = (java.util.Date) source; return new Date(temp.getYear(), temp.getMonth(), temp.getDate()).toString(); } if ((source instanceof Date)) { return source.toString(); } if ((source instanceof DateTime)) { return source.toString().substring(0, 10); } throw new RuntimeException("Invalid date format " + source); } public static String parseTimeValue(Object source) { if (source == null) { return null; } if ((source instanceof String)) { String strValue = source.toString(); if (checkTime(strValue)) { return strValue; } if (checkDateTime(strValue)) { return strValue.substring(11); } throw new RuntimeException("Invalid date format"); } if ((source instanceof java.util.Date)) { java.util.Date temp = (java.util.Date) source; return new Time(temp.getHours(), temp.getMinutes(), temp.getSeconds()).toString(); } if ((source instanceof Time)) { return source.toString(); } if ((source instanceof DateTime)) { return source.toString().substring(11); } throw new RuntimeException("Invalid date format " + source); } public static Float parseFloatValue(Object source) { if (source == null) { return null; } if ((source instanceof Float)) { return (Float) source; } return Float.valueOf(Double.valueOf(source.toString()).floatValue()); } public static float[] parseFloatArrayValue(Object source) { if (source == null) { return null; } if ((source instanceof float[])) { return (float[]) source; } if ((source instanceof Object[])) { Object[] objs = (Object[]) source; float[] values = new float[objs.length]; for (int i = 0; i < objs.length; i++) { values[i] = Double.valueOf(objs[i].toString()).floatValue(); } return values; } return new float[] { Double.valueOf(source.toString()).floatValue() }; } public static Double parseDoubleValue(Object source) { if (source == null) { return null; } if ((source instanceof Double)) { return (Double) source; } return Double.valueOf(source.toString()); } public static double[] parseDoubleArrayValue(Object source) { if (source == null) { return null; } if ((source instanceof double[])) { return (double[]) source; } if ((source instanceof Object[])) { Object[] objs = (Object[]) source; double[] values = new double[objs.length]; for (int i = 0; i < objs.length; i++) { values[i] = Double.valueOf(objs[i].toString()).floatValue(); } return values; } return new double[] { Double.valueOf(source.toString()).doubleValue() }; } public static Boolean parseBooleanValue(Object source) { if (source == null) { return null; } if ((source instanceof Boolean)) { return (Boolean) source; } String strValue = source.toString(); if ("true".equalsIgnoreCase(strValue)) { return Boolean.TRUE; } if ("false".equalsIgnoreCase(strValue)) { return Boolean.FALSE; } if (Long.parseLong(strValue) == 0L) { return Boolean.FALSE; } return Boolean.TRUE; } public static boolean[] parseBooleanArrayValue(Object source) { if (source == null) { return null; } if ((source instanceof boolean[])) { return (boolean[]) source; } if ((source instanceof Boolean[])) { Boolean[] bs = (Boolean[]) source; boolean[] bsArr = new boolean[bs.length]; for (int i = 0; i < bs.length; i++) { bsArr[i] = bs[i].booleanValue(); } return bsArr; } return null; } public static Boolean[] parseBooleanObjectArrayValue(Object source) { if (source == null) { return null; } if ((source instanceof Boolean[])) { return (Boolean[]) source; } if ((source instanceof boolean[])) { boolean[] bs = (boolean[]) source; Boolean[] bsArr = new Boolean[bs.length]; for (int i = 0; i < bs.length; i++) { bsArr[i] = new Boolean(bs[i]); } return bsArr; } return null; } public static Short parseShortValue(Object source) { if (source == null) { return null; } if ((source instanceof Byte)) { return (Short) source; } return Short.valueOf(Long.valueOf(source.toString()).shortValue()); } public static short[] parseShortArrayValue(Object source) { if (source == null) { return null; } if ((source instanceof short[])) { return (short[]) source; } if ((source instanceof Object[])) { Object[] objs = (Object[]) source; short[] values = new short[objs.length]; for (int i = 0; i < objs.length; i++) { values[i] = Long.valueOf(objs[i].toString()).shortValue(); } return values; } return new short[] { Long.valueOf(source.toString()).shortValue() }; } public static Byte parseByteValue(Object source) { if (source == null) { return null; } if ((source instanceof Byte)) { return (Byte) source; } return Byte.valueOf(Long.valueOf(source.toString()).byteValue()); } public static byte[] parseByteArrayValue(Object source) { if (source == null) { return null; } if ((source instanceof byte[])) { return (byte[]) source; } return source.toString().getBytes(); } public static String numberToStringWithFixedLength(int value, int length) { String temp = ""; for (int i = 0; i < length; i++) { temp = temp + "0"; } DecimalFormat df = new DecimalFormat(temp); return df.format(value); } }
package com.cdoframework.cdolib.base; import java.io.File; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.Reader; import java.net.URL; import java.net.URLConnection; import java.util.Properties; public class Resources { private static ClassLoader defaultClassLoader; public static ClassLoader getDefaultClassLoader() { return defaultClassLoader; } public static void setDefaultClassLoader(ClassLoader defaultClassLoader) { defaultClassLoader = defaultClassLoader; } public static URL getResourceURL(String resource) throws IOException { return getResourceURL(getClassLoader(), resource); } public static URL getResourceURL(ClassLoader loader, String resource) throws IOException { URL url = null; if (loader != null) { url = loader.getResource(resource); } if (url == null) { url = ClassLoader.getSystemResource(resource); } if (url == null) { throw new IOException("Could not find resource " + resource); } return url; } public static InputStream getResourceAsStream(String resource) throws IOException { return getResourceAsStream(getClassLoader(), resource); } public static InputStream getResourceAsStream(ClassLoader loader, String resource) throws IOException { InputStream in = null; if (loader != null) { in = loader.getResourceAsStream(resource); } if (in == null) { in = ClassLoader.getSystemResourceAsStream(resource); } if (in == null) { throw new IOException("Could not find resource " + resource); } return in; } public static Properties getResourceAsProperties(String resource) throws IOException { Properties props = new Properties(); InputStream in = null; String propfile = resource; in = getResourceAsStream(propfile); props.load(in); in.close(); return props; } public static Properties getResourceAsProperties(ClassLoader loader, String resource) throws IOException { Properties props = new Properties(); InputStream in = null; String propfile = resource; in = getResourceAsStream(loader, propfile); props.load(in); in.close(); return props; } public static Reader getResourceAsReader(String resource) throws IOException { return new InputStreamReader(getResourceAsStream(resource)); } public static Reader getResourceAsReader(ClassLoader loader, String resource) throws IOException { return new InputStreamReader(getResourceAsStream(loader, resource)); } public static File getResourceAsFile(String resource) throws IOException { return new File(getResourceURL(resource).getFile()); } public static File getResourceAsFile(ClassLoader loader, String resource) throws IOException { return new File(getResourceURL(loader, resource).getFile()); } public static InputStream getUrlAsStream(String urlString) throws IOException { URL url = new URL(urlString); URLConnection conn = url.openConnection(); return conn.getInputStream(); } public static Reader getUrlAsReader(String urlString) throws IOException { return new InputStreamReader(getUrlAsStream(urlString)); } public static Properties getUrlAsProperties(String urlString) throws IOException { Properties props = new Properties(); InputStream in = null; String propfile = urlString; in = getUrlAsStream(propfile); props.load(in); in.close(); return props; } public static Class classForName(String className) throws ClassNotFoundException { Class clazz = null; try { clazz = getClassLoader().loadClass(className); } catch (Exception localException) { } if (clazz == null) { clazz = Class.forName(className); } return clazz; } public static Object instantiate(String className) throws ClassNotFoundException, InstantiationException, IllegalAccessException { return instantiate(classForName(className)); } public static Object instantiate(Class clazz) throws InstantiationException, IllegalAccessException { return clazz.newInstance(); } private static ClassLoader getClassLoader() { if (defaultClassLoader != null) { return defaultClassLoader; } return Thread.currentThread().getContextClassLoader(); } }
相关推荐
假设有一个名为`file_list.txt`的文本文件,其中每一行包含一个待修改的文件名和新的文件名,我们可以创建一个批处理文件,逐行读取这个列表并执行改名操作。 2. **PowerShell**:PowerShell是Windows的高级命令行...
如果需要更复杂的逻辑,如基于文件内容或元数据改名,还可以利用`os.stat()`获取文件信息,或者使用`fileinput`模块读取文件内容。 对于压缩包内的文件,例如提供的"Rename"文件,这可能是一个包含示例代码或者配置...
Bulk Rename Utility是一款Windows平台下的文件批量重命名工具,由Jim Willsher开发,其功能十分强大,能够根据用户设定的多样化标准对文件或文件夹进行批量更名。通过这款工具,用户可以选择添加前缀或后缀、替换...
在编程环境中,如Python或Perl,可以通过读取文件、修改内容然后写回的方式实现批量操作。这些编程语言提供了强大的字符串处理和文件操作功能,适合处理复杂的内容修改需求。 总的来说,批量文件修改和批量命名是...
3. **磁盘碎片整理**:整理硬盘上的文件碎片,提高文件读写速度。 4. **注册表清理**:安全地清理无效的注册表项,防止系统错误和崩溃。 5. **安全防护**:可能包含基本的防病毒或恶意软件扫描功能,保护电脑免受...
5. **SETUP.INI** - 配置文件,包含了安装过程的设置和指令,安装程序会读取这个文件来了解如何执行安装步骤。 6. **WIZ.Z** - 可能是一个压缩的安装向导文件,用于在安装过程中提供图形化的用户界面和指导。 7. *...
标题 "Cabinet File Utility Classes (142KB)" 暗示这可能是一个与处理Cabinet(.CAB)文件相关的程序库。Cabinet 文件是 Microsoft Windows 中用于存储压缩数据的一种格式,通常用于软件安装包中。这个程序库包含了...
- Excel电子表格文件:LabVIEW与Microsoft Excel的交互主要依赖于“Excel VIs”,这些是NI提供的库,允许创建、修改和读取Excel文件。通常涉及的VIs有“Create Workbook”,“Add Worksheet”,“Write Range”等。...
- CAB文件由一系列的" cabinet"段组成,每个段包含一个或多个"文件"记录。 - 每个文件记录包含一个或多个压缩的数据块,以及文件的元数据,如文件名、大小等。 - CAB文件还支持错误检测和恢复,通过使用CRC校验和...
ISO文件,也称为ISO映像,是磁盘或光盘内容的精确复制,以单个文件的形式存在。这种文件通常用于备份光盘内容、分发软件或传输大量数据。ISO文件的扩展名通常是".iso",表明它符合ISO 9660标准。创建ISO文件的目的是...
7. **文件代号**:文件代号是一个 16 进制数值,用于标识文件,便于后续的文件读写操作。当通过 DOS 功能调用成功打开或创建文件后,会返回一个文件代号。 8. **标准设备代号**: - 0:标准输入设备 - 1:标准...
理解并掌握如何制作和使用YAFFS2文件系统对于开发和维护基于NAND闪存的嵌入式系统至关重要,因为这种文件系统能够有效应对闪存的特性,如有限的擦写次数和随机读写性能。通过以上步骤,开发者可以定制自己的系统镜像...
对于 create 命令,读取文件名、文件类型和文件信息,并将其存储在 map 中。对于 open 命令,读取文件名,并根据文件类型执行不同的操作。如果是数据块,则显示文件信息,如果是快捷方式,则继续打开快捷方式指向的...
例如,使用FileStream读取文件内容,然后通过HttpResponse的OutputStream流将其写入到HTTP响应中。同时,为了处理大文件,可以考虑使用Stream的Read方法进行分块读取,以避免一次性加载整个文件到内存中。 在ASP...
Linux中的文件名由字母、数字、下划线和圆点组成,区分大小写,可以包含扩展名以帮助识别文件类型。每个文件都由文件系统维护,文件系统在存储设备上以树形结构组织文件和目录。 目录是用来管理和组织文件的特殊...
例如,可以通过读取文件夹中的所有图片文件,然后按照预设规则(如添加序列号、日期、自定义前缀等)生成新的文件名,最后使用os.rename()函数进行重命名。 此外,还有许多第三方软件和工具专为批量重命名设计,如...
- **FilenameUtils**:用于处理文件名相关的操作,如获取文件扩展名、修改文件名等。 - **ResourceUtils**:用于处理资源定位和加载的任务。 - **TempFileCreator**:用于创建临时文件。 这些工具类通过提供一系列...
开发者自行编写的一个简单版本的批量改名工具,可能是基于Python、批处理脚本或者其他编程语言实现的,它的功能主要是读取指定目录下的文件,按照特定规则(例如去除"_0X"后缀)批量修改文件的名称。 批量改名工具...
9. **文件内容读写**:提供方便的方法来读取和写入文件内容,包括读取整个文件到字符串,或者写字符串到文件。 10. **临时文件和目录**:方便地创建临时文件和目录。 11. **对象序列化和反序列化**:简化了对象到...