java 积累资料

1.java lookandfeel获得本地系统的UI风格

 

try{

   String lookAndFeel = javax.swing.UIManager.getSystemLookAndFeelClassName();
   UIManager.setLookAndFeel(lookAndFeel);

}catch(Exception e){

     e.printStackTrace();

}

 

2.java获得操作系统类型

 

String osName = (String) AccessController.doPrivileged(
                new GetPropertyAction("os.name"));
System.out.println("操作系统为:" + osName);

 

3.java判断ip地址

 

Pattern pattern = Pattern.compile( "^((\\d|[1-9]\\d|1\\d\\d|2[0-4]\\d|25[0-5]|[*])\\.){3}(\\d|[1-9]\\d|1\\d\\d|2[0-4]\\d|25[0-5]|[*])$" );   

return pattern.matcher( str ).matches();

 

4.java获取系统IP地址和MAC地址

 

package cn.com.test;   

  

import java.io.BufferedReader;   

import java.io.IOException;   

import java.io.InputStreamReader;   

  

public class CCSFDs {   

    public static void main(String[] args) throws Exception {   

           

        String osname = System.getProperty("os.name");   

        System.out.println("你的操作系统是" + osname);   

        System.out.println("你的IP是" + getWIndowsIp());   

        System.out.println("你的mac地址 " + getLocalWindowsMAC());   

  

    }   

  

    private static String getLocalWindowsMAC() throws Exception {   

        Runtime rt = Runtime.getRuntime();   

        Process process = rt.exec("ipconfig /all");   

        BufferedReader in = new BufferedReader(new InputStreamReader(process   

                .getInputStream()));   

        String s;   

        while ((s = in.readLine()) != null) {   

            s = s.trim();   

            if (s.startsWith("Physical Address")) {   

                s = s.substring("Physical Address".length()).trim();   

                int pos = s.indexOf(": ");   

              if (pos > -1) {   

                    String mac = s.substring(pos + ": ".length()).trim();   

                    return mac;   

                }   

            }   

        }   

        return null;   

    }   

    private static String getWIndowsIp(){   

        String ip ="";   

        Runtime rt = Runtime.getRuntime();   

        Process process = null;   

        try {   

            process = rt.exec("ipconfig /all");   

            BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));   

            String s;   

            while ((s = in.readLine()) != null) {   

                s = s.trim();   

                if (s.startsWith("IP Address")) {   

                    s = s.substring("IP Address".length()).trim();   

                    int pos = s.indexOf(": ");   

                    if (pos > -1) {   

                        ip = s.substring(pos + ": ".length()).trim();   

                    }   

                }   

            }   

        } catch (IOException e) {   

            e.printStackTrace();   

        }   

        return ip;   

    }   

}

 

5.java获取linux操作系统的网卡信息(包括名称.....)

  

public class NetworkParameterDemo {

  public static void main(String[] args) throws Exception {
    Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces();
    while (en.hasMoreElements()) {
      NetworkInterface ni = en.nextElement();
      printParameter(ni);
  
    }
  }
  
  public static void printParameter(NetworkInterface ni) throws SocketException {
    System.out.println(" Name = " + ni.getName());
    System.out.println(" Display Name = " + ni.getDisplayName());
    System.out.println(" Is up = " + ni.isUp());
    System.out.println(" Support multicast = " + ni.supportsMulticast());
    System.out.println(" Is loopback = " + ni.isLoopback());
    System.out.println(" Is virtual = " + ni.isVirtual());
    System.out.println(" Is point to point = " + ni.isPointToPoint());
    System.out.println(" Hardware address = " + ni.getHardwareAddress());
    System.out.println(" MTU = " + ni.getMTU());
  
    System.out.println("\nList of Interface Addresses:");
    List<InterfaceAddress> list = ni.getInterfaceAddresses();
    Iterator<InterfaceAddress> it = list.iterator();
  
    while (it.hasNext()) {
      InterfaceAddress ia = it.next();
      System.out.println(" Address = " + ia.getAddress());
      System.out.println(" Broadcast = " + ia.getBroadcast());
      System.out.println(" Network prefix length = " + ia.getNetworkPrefixLength());
      System.out.println("");
    }
  }
}

 

 

你可能感兴趣的:(java,exception,UI,String)