皇家方舟

          工作中常用的小方法集合

          獲取本機(jī)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;
          ?}

          獲取本機(jī)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 阿輝 閱讀(425) 評(píng)論(2)  編輯  收藏 所屬分類: 學(xué)習(xí)日志

          Feedback

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

          /**
          * 取源數(shù)組中從第一個(gè)元素開始長度為length的子數(shù)組。
          * @param src
          * 源數(shù)組
          * @param length
          * 目標(biāo)數(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ù)  更多評(píng)論   

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

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


          My Links

          Blog Stats

          常用鏈接

          留言簿(1)

          隨筆分類

          隨筆檔案

          搜索

          積分與排名

          最新評(píng)論

          閱讀排行榜

          評(píng)論排行榜

          主站蜘蛛池模板: 宝丰县| 墨竹工卡县| 科技| 永丰县| 天门市| 肃宁县| 江油市| 安图县| 治县。| 开原市| 衡东县| 加查县| 莎车县| 双江| 朝阳区| 黎城县| 秦皇岛市| 鱼台县| 沙坪坝区| 邵东县| 淮北市| 黑山县| 彭阳县| 六枝特区| 尉氏县| 安图县| 华池县| 林州市| 体育| 尤溪县| 深水埗区| 邓州市| 合阳县| 利川市| 成安县| 双鸭山市| 三亚市| 东乡族自治县| 华蓥市| 罗江县| 祥云县|