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("執(zhí)行完畢");
??? }
}
? 這個(gè)代碼是我最近的一個(gè)project中,用來(lái)調(diào)用gawk的處理結(jié)果,并在web上顯示
在web中顯示的部分就是jsp了.這個(gè)就沒(méi)有必要貼了,
? awk處理文本的效率確實(shí)不錯(cuò).