皇家方舟

工作中常用的小方法集合

获取本机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 阿辉 阅读(395) 评论(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)

随笔分类

随笔档案

搜索

积分与排名

最新评论

阅读排行榜

评论排行榜