免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1314 | 回复: 1
打印 上一主题 下一主题

紧急询问;java程序中如果调用写好的shell程序,要传递参数 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2004-09-01 10:40 |只看该作者 |倒序浏览
多谢各位!以前没做过,很紧急的,谢谢!!!

论坛徽章:
0
2 [报告]
发表于 2004-09-01 13:38 |只看该作者

紧急询问;java程序中如果调用写好的shell程序,要传递参数

  1. package test;
  2. import java.io.BufferedInputStream;
  3. import java.io.IOException;
  4. import java.io.InputStream;

  5. public class javashell {

  6.    static public void main(String[] args) {
  7.       javashell instance = new javashell();
  8.       instance.callCommand();
  9.    }
  10.    static void callCommand(){
  11.        String[] cmd = { "rsh", "sesun4", "cat /tmp/anyfile.txt " };
  12.        try {
  13.            Process ps = Runtime.getRuntime().exec(cmd);
  14.            System.out.print(loadStream(ps.getInputStream()));
  15.            //System.err.print(loadStream(ps.getErrorStream()));
  16.        }catch(IOException ioe){
  17.            ioe.printStackTrace();
  18.        }         
  19.    }
  20.    // read an input-stream into a String
  21.    static String loadStream(InputStream in) throws IOException {
  22.       int ptr = 0;
  23.       int i = 0 ;
  24.       StringBuffer buffer = null ;
  25.       try{
  26.          in = new BufferedInputStream(in);
  27.          buffer = new StringBuffer();
  28.          while( (ptr = in.read()) != -1 ) {
  29.             i ++ ;
  30.             buffer.append((char)ptr);
  31.             //System.out.println(i + "                " + (char)ptr);
  32.             if((i%1000)==0)
  33.                System.out.println(i + "        " + (char)ptr);
  34.                System.out.println(buffer);
  35.          }
  36.          System.out.println("char set" + i);
  37.       }catch(IOException ioe){
  38.          ioe.printStackTrace();
  39.       }
  40.       return buffer.toString();
  41.    }
  42. }
复制代码

cmd 数组换成你要执行的命令即可!
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP