皇家方舟

          工作中常用的小方法集合

          獲取本機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 阿輝 閱讀(416) 評論(2)  編輯  收藏 所屬分類: 學習日志

          Feedback

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

          /**
          * 取源數組中從第一個元素開始長度為length的子數組。
          * @param src
          * 源數組
          * @param length
          * 目標數組的長度
          * @return 子數組
          */
          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;
          }  回復  更多評論   

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

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


          My Links

          Blog Stats

          常用鏈接

          留言簿(1)

          隨筆分類

          隨筆檔案

          搜索

          積分與排名

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 廉江市| 陆川县| 香河县| 永新县| 潜江市| 漠河县| 沾化县| 红安县| 潞西市| 大方县| 北宁市| 建瓯市| 高州市| 永城市| 洛宁县| 甘泉县| 威远县| 资中县| 三台县| 遂川县| 彰武县| 正安县| 南雄市| 大洼县| 咸丰县| 宁波市| 富平县| 介休市| 文昌市| 浙江省| 烟台市| 芒康县| 五大连池市| 泰来县| 利川市| 青川县| 小金县| 宣威市| 宜良县| 古浪县| 常山县|