1 ImportJava.io.BufferedReader; 2 Importjava.io.IOException; 3 ImportJava.io.InputStream; 4 ImportJava.io.InputStreamReader; 5 6 /** 7 * Determine the network connection status. 8 * 9 */ Ten Public classNetstate { One A Public BooleanIsconnect () { - BooleanConnect =false; -Runtime runtime =Runtime.getruntime (); the process process; - Try { -Process = Runtime.exec ("ping" + "www.baidu.com"); -InputStream is =Process.getinputstream (); +InputStreamReader ISR =NewInputStreamReader (IS); -BufferedReader br =NewBufferedReader (ISR); +String line =NULL; AStringBuffer SB =NewStringBuffer (); at while(line = Br.readline ())! =NULL) { - Sb.append (line); - } -System.out.println ("Return value is:" +SB); - Is.close (); - Isr.close (); in Br.close (); - to if(NULL! = SB &&!sb.tostring (). Equals ("")) { +String logstring = ""; - if(Sb.tostring (). IndexOf ("TTL") > 0) { the //Network unblocked *Connect =true; $}Else { Panax Notoginseng //Network is not unblocked -Connect =false; the } + } A}Catch(IOException e) { the E.printstacktrace (); + } - returnConnect; $ } $ - Public Static voidMain (string[] args) { -Netstate netstate =Newnetstate (); the System.out.println (Netstate.isconnect ()); - Wuyi } the}
Java determines whether the network is healthy by pinging