Khan's Notebook GCC/GNU/Linux Delphi/Window Java/Anywhere

路漫漫,长修远,我们不能没有钱
随笔 - 172, 文章 - 0, 评论 - 257, 引用 - 0
数据加载中……

java执行外部指令,并接收中文结果

package com.khan.util;


import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
//import java.io.File;

/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2006</p>
 * <p>Company: </p>
 * @author not attributable
 * @version 1.0
 */


public class ExecCmd {

    public ExecCmd() {

    }


    // read an input-stream into a String
    public static String loadStream(InputStream in, String charset) throws IOException {
        int char_set_len = 2;
        if (null == charset) {
            charset = "GBK";
        }

        if (charset.equals("utf8") || charset.equals("UTF8")
            || charset.equals("utf-8") || charset.equals("UTF-8")) {
            char_set_len = 3;
        }

        int ptr = 0;
        byte[] char_buff = new byte[char_set_len];
        in = new BufferedInputStream(in);

        StringBuffer buffer = new StringBuffer();
        while ((ptr = in.read()) != -1) {

            if (ptr >= 0x80) { //中文处理
                char_buff[0] = (byte) ptr;
                for (int i = 1; i < char_set_len; i++) {
                    char_buff[i] = (byte) in.read();
                    if (char_buff[i] == -1) {
                        break;
                    }
                }
                buffer.append(new String(char_buff, charset));
                continue;
            }

            buffer.append((char) ptr);
        }
        return buffer.toString();
    }


    static public void main(String[] args) {
        long l = 0;
        String str = "";
        String[] cmd1 = {"cmd.exe",
                        "/c",
                        "dir",
                        "/b",
                        "/s",   "e:\\*.log"};
                        //args[0] +"\\*.log"};

        String[] cmd = {"gawk",
                       "-F,",
                       //"\"END{print NR}\"",
                       "\"END{print substr($1, 0, 8), NR}\"",
                       "mo_pay.log"};
        //File dir = new File("D:\\Program Files\\gawk\\bin");

        try {
            Process ps = Runtime.getRuntime().exec(cmd1);
            String[] strs = loadStream(ps.getInputStream(), "GBK").split("\r\n");
            System.err.print(loadStream(ps.getErrorStream(), "GBK"));
            for (int i = 0; i < strs.length; i++) {
                cmd[3] = strs[i].trim();
                ps = Runtime.getRuntime().exec(cmd);
                str = loadStream(ps.getInputStream(), "GBK");
                str = str.substring(0, str.indexOf('\r'));

                l += Long.parseLong(str.split(" ")[1]);
                System.out.println(str);
                System.err.print(loadStream(ps.getErrorStream(), "GBK"));
            }
            System.out.println(l);
        } catch (IOException e) {
            e.printStackTrace();
        }
        //System.out.println("执行完毕");
    }

}



  这个代码是我最近的一个project中,用来调用gawk的处理结果,并在web上显示
在web中显示的部分就是jsp了.这个就没有必要贴了,
  awk处理文本的效率确实不错.

posted on 2006-09-28 10:07 Khan 阅读(1001) 评论(0)  编辑 收藏 引用 所属分类: 跨平台开发Java


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理