皇家方舟

          工作中常用的小方法集合

          獲取本機IP
          public static String getLocalHostIp() throws SocketException
          ?{
          ??Enumeration netInterfaces = NetworkInterface.getNetworkInterfaces();
          ??while(netInterfaces.hasMoreElements())
          ??{
          ???NetworkInterface ni = (NetworkInterface)netInterfaces.nextElement();
          ???Enumeration<InetAddress> inetAddresses = ni.getInetAddresses();
          ???while(inetAddresses.hasMoreElements())
          ???{
          ????InetAddress ip = inetAddresses.nextElement();
          ????if(!ip.isLoopbackAddress())
          ????{
          ?????return ip.getHostAddress();
          ????}
          ???}
          ??}
          ??return null;
          ?}

          獲取本機MAC
          public static String getLocalMac() throws IOException
          ?{
          ??Process p1 = Runtime.getRuntime().exec("ipconfig?? /all");
          ??BufferedReader br = new BufferedReader(
          ????????????new InputStreamReader(
          ??????????????????p1
          ???????????????????.getInputStream()));
          ??String buf = br.readLine();
          ??while(buf != null)
          ??{
          ???if(buf.contains("Physical Address"))
          ???{
          ????int i = buf.indexOf("Physical Address") + 36;
          ????return buf.substring(i).trim();
          ???}
          ???buf = br.readLine();
          ??}
          ??return "000000";
          ?}

          posted on 2007-04-26 18:12 阿輝 閱讀(417) 評論(2)  編輯  收藏 所屬分類: 學(xué)習日志

          Feedback

          # 如何從數(shù)組中獲取子數(shù)組 2007-04-27 00:03 阿輝

          /**
          * 取源數(shù)組中從第一個元素開始長度為length的子數(shù)組。
          * @param src
          * 源數(shù)組
          * @param length
          * 目標數(shù)組的長度
          * @return 子數(shù)組
          */
          public static byte[] getSubArray(byte[] src, int length)
          {
          ByteBuffer buf = ByteBuffer.wrap(src);
          buf.position(0);
          byte[] subBuf = new byte[length];
          buf.get(subBuf);
          return subBuf;
          }  回復(fù)  更多評論   

          # 如何從數(shù)組中獲取子數(shù)組 2007-04-27 00:06 阿輝

          上面的方法未對任何異常情況進行處理,如:length<0或者length大于源數(shù)組的長度,以及源數(shù)組為null等情況,使用者可根據(jù)各自的需要再進行不同的處理。  回復(fù)  更多評論   


          My Links

          Blog Stats

          常用鏈接

          留言簿(1)

          隨筆分類

          隨筆檔案

          搜索

          積分與排名

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 漾濞| 沾益县| 城步| 绥中县| 府谷县| 普定县| 安岳县| 瓦房店市| 济南市| 杂多县| 淅川县| 嫩江县| 宜黄县| 三河市| 琼中| 康马县| 延寿县| 晋宁县| 宝鸡市| 香格里拉县| 金溪县| 寿光市| 宿州市| 凌源市| 准格尔旗| 涟水县| 屯门区| 漾濞| 泸定县| 华宁县| 新营市| 马尔康县| 怀来县| 天柱县| 揭阳市| 甘孜| 法库县| 宜川县| 高密市| 上饶县| 苍山县|