java调用其它程序之返回值

来源:互联网 发布:公益网络平台 编辑:程序博客网 时间:2024/04/28 17:04
Process   process   =   Runtime.getRuntime().exec("cmd   /c   dir");  
   
              System.out.println(loadStream(process.getInputStream()));   //load   the   stream  
              System.out.println(loadStream(process.getErrorStream()));   //load   the   stream  
   
      static   String   loadStream(InputStream   in)   throws   IOException  
      {  
          int   ptr   =   0;  
          in   =   new   BufferedInputStream(in);  
          StringBuffer   buffer   =   new   StringBuffer();  
          while   (   (ptr   =   in.read())   !=   -1)  
          {  
              buffer.append(   (char)   ptr);  
          }  
          return   buffer.toString();  
      }  
---------------------------
刚才那个loadStream方法,如果dir里面有汉字则显示为乱码。应将该方法修改为如下:  
      static   String   loadStream(InputStream   in)   throws   IOException  
      {  
          int   ptr   =   0;  
          in   =   new   BufferedInputStream(in);  
          StringBuffer   buffer   =   new   StringBuffer();  
          while   (   (ptr   =   in.read())   !=   -1)  
          {  
              buffer.append(   (char)   ptr);  
          }  
          String   aa=buffer.toString();  
            byte[]   temp=   aa.getBytes("ISO-8859-1");  
            String   result=new   String(temp);  
          return   result;  
      }    
原创粉丝点击