zoukankan      html  css  js  c++  java
  • Android ping命令 -- Runtime

    代码:

     1     public String Run(String _strsIp) throws Exception
     2     {
     3         String strRst = "";
     4         try
     5         {
     6             String str = "ping -c 1 "+_strsIp;
     7             Runtime runtime = Runtime.getRuntime();
     8             Process process = runtime.exec(str);
     9 
    10             //等待进程执行完毕
    11             if(process.waitFor() != 0)
    12             {
    13                 //如果进程运行结果不为0,表示进程是错误退出的
    14                 //获得进程实例的错误输出
    15                 InputStream streamErr = process.getErrorStream();
    16                 //do something
    17                 
    18                 strRst = InputStream2String(streamErr);
    19                 if (streamErr != null)
    20                     streamErr.close();
    21                 
    22                 if (strRst.isEmpty())
    23                 {
    24                     InputStream streamIn = process.getInputStream();
    25                     strRst = InputStream2String(streamIn);
    26                     if (streamIn != null)
    27                         streamIn.close();
    28                     
    29                     if (strRst.isEmpty())
    30                         strRst = "Failed : ping failed . ";
    31                 }
    32                 return strRst;
    33             }
    34             
    35             InputStream streamIn = process.getInputStream();
    36             strRst = InputStream2String(streamIn);
    37             if (streamIn != null)
    38                 streamIn.close();
    39             
    40             if (strRst.isEmpty())
    41                 strRst = "Succeed : ping failed . ";
    42         }
    43         catch (Exception ex)
    44         {
    45             //ex.printStackTrace();
    46             StringWriter sw = new StringWriter();
    47             PrintWriter pw = new PrintWriter(sw);
    48             ex.printStackTrace(pw);
    49             strRst = "Err : "+sw.toString();
    50         }
    51         return strRst;
    52     }
  • 相关阅读:
    文件的类型
    读取文件,并按原格式输出文件内容的三种方式
    react hook代码框架
    器具的行为模式
    设计模式
    cpu 内存 机器语言 汇编 高级语言 平台之间的关系
    操作系统之内存
    操作系统之文件
    操作系统之IO
    七层模型之应用层
  • 原文地址:https://www.cnblogs.com/codeskilla/p/4953117.html
Copyright © 2011-2022 走看看