⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 execcmd.java

📁 java支持的短信平台
💻 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("执行完毕");
    }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -