您的位置:首页 > 编程语言 > Java开发

java调用其它程序之返回值

2008-01-22 22:44 483 查看
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;  
      }    
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息