J2SE5.0 New Features ProcessBuilder

xiaoxiao2021-03-06  44

This example uses J2SE5.0's ProcessBuilder class to perform external programs, which is more convenient to Runtime.exec, which can set environment variables. Here is the address of the physical network card under Windows: package com.kuaff.jdk5package; import java.io.ioException; import java.io.inputstream; import java.util.ArrayList; import java.util.list; public class ProcessBuilderShow {public static list getPhysicalAddress () {Process p = null; // physical NIC list list address = new ArrayList (); try {// perform ipconfig / all command p = new ProcessBuilder ( "ipconfig", "/ all"). start ();} catch (ooException e) {return address;} Byte [] b = new byte [1024]; stringbuffer sb = new stringbuffer (); // Read process output INPUTSTREAM IN = P.GETINPUTSTREAM (); try {while (in.read (b)> 0) {sb.append (new string (b));}} catch (ooException E1) {} finally {Try {in. Close ();} catch (ooException E2) {}} // The following analysis output value, get the physical network card string Rtvalue = sb.substring (0); int i = r = RtValue.indexof ("Physical address...... "); While (i> 0) {RtValue = RtValue.substring (i " physical address........... ".length ()); address.add (RtValue.substring (0,18)); i = = rse.indexof (" Physical address............: ");} Return Address;} public static Void main (string [] args) {list address = processbuildershow.getPhysicalAddress (); for (String address) {system.out.printf ("Physical Network Card Address:% S% N", ADD); }

转载请注明原文地址:https://www.9cbs.com/read-79683.html

New Post(0)