皇家方舟

          工作中常用的小方法集合

          獲取本機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)  編輯  收藏 所屬分類: 學習日志

          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)

          隨筆分類

          隨筆檔案

          搜索

          積分與排名

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 桦川县| 崇阳县| 贵溪市| 鹿邑县| 五河县| 三都| 拜城县| 东阿县| 桂林市| 札达县| 毕节市| 叙永县| 北海市| 平江县| 泰安市| 永丰县| 买车| 西宁市| 望谟县| 南陵县| 土默特左旗| 辽宁省| 呼和浩特市| 翼城县| 攀枝花市| 南岸区| 北安市| 大埔县| 鞍山市| 小金县| 清水河县| 巴里| 封开县| 长寿区| 高邑县| 保定市| 连南| 罗城| 灵石县| 永清县| 伊宁县|