皇家方舟

          工作中常用的小方法集合

          獲取本機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)

          隨筆分類

          隨筆檔案

          搜索

          積分與排名

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 竹溪县| 平湖市| 武平县| 潮安县| 黎平县| 昌乐县| 龙胜| 邳州市| 星座| 车致| 博野县| 吕梁市| 岢岚县| 台北市| 安达市| 宁波市| 调兵山市| 呼伦贝尔市| 县级市| 洛扎县| 和政县| 九龙县| 湖州市| 昌黎县| 谢通门县| 韶关市| 方山县| 永顺县| 房产| 沂源县| 佛坪县| 龙海市| 保靖县| 惠州市| 二手房| 抚顺市| 云阳县| 长宁县| 高台县| 山丹县| 阿拉尔市|