• Showing Page History #72408
  • Showing Page History #72410

BatchTset

概要

以下は、バッチ処理のサンプルです。test/junkutil/sample に入っています。

サンプル

package junkutil.sample;

import java.util.List;
import java.util.Map;

import junkutil.common.FileUtil;
import junkutil.common.OptGets;
import junkutil.set.LzList;

/**
 * 引数に与えられたファイルの中で、英字で始まる行だけを出力するプログラム。
 * 引数 -h でヘッダーを出力する。
 * @author Hiroyuki Shiota
 */
public class BatchTest {
    public static void main(String [] args) {
        try {
            OptGets optGets = new OptGets("h");
            Map opts = optGets.parse(args);
            if (opts.get("h") == Boolean.TRUE) {
                System.out.println("====BatchTest Heaader====");
            }
            List argList = optGets.getArgList();
            for (int i = 0; i < argList.size(); i++) {
                String filename = (String)argList.get(i);
                if (!FileUtil.exists(filename)) {
                    System.out.println("File not found: " + filename);
                    continue;
                }
                List lines = FileUtil.readLines(filename, "EUC-JP");
                List grep = new LzList(lines).grep("^[a-zA-Z]").list();
                for (int j = 0; j < grep.size(); j++) {
                    System.out.println((String)grep.get(j));
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

クロージャ

package junkutil.sample;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import junkutil.common.FileUtil;
import junkutil.common.OptGets;
import junkutil.set.LzList;
import junkutil.set.LzListClosure;
import junkutil.set.LzMap;
import junkutil.set.LzMapClosure;

/**
 * クロージャを活用する
 * @author Hiroyuki Shioa
 */
public class BatchTest2 {

    /**
     * Mapを出力するクロージャ
     */
    private static class MapDump implements LzMapClosure {
        public boolean process(Object key, Object value) {
            if (value == null) return false;
            System.out.println(key + "=" + value);
            return false;
        };
    }

    /**
     * ファイルを出力するクロージャ
     */
    private static class FileDataDump implements LzListClosure {
        public boolean process(int i, int len, Object obj) {
            String filename = (String)obj;
            if (!FileUtil.exists(filename)) {
                System.out.println("File not found: " + filename);
                return false;
            } else {
                System.out.println("File: " + filename);
                List lines = null;
                try {
                    lines = FileUtil.readLines(filename, "Shift_JIS");
                    new LzList(lines).grep("^[a-zA-Z]").forEach(new ListDump());
                } catch (IOException e) {
                    lines = null;
                }
                return true;
            }
        }
    }

    /**
     * リストを出力するクロージャ
     */
    private static class ListDump implements LzListClosure {
        public boolean process(int i, int len, Object obj) {
            System.out.println(obj);
            return false;
        }
    }

    /**
     * メイン
     * @param args
     * @throws Exception
     */
    public static void main(String [] args) throws Exception {
        args = new String[] { "-h", "testdata/readFile.txt", "testdata/writeFile.txt", "Nothing.txt" };
        OptGets optGets = new OptGets("abch");
        Map opts = optGets.parse(args);
        if (opts.get("h") == Boolean.TRUE) {
            System.out.println("====BatchTest Heaader====");
        }
        List argList = optGets.getArgList();
        LzMap optsMap = new LzMap(opts);
        optsMap.forEach(optsMap.sortedKeys(), new MapDump()); //Mapをダンプ
        new LzList(argList).forEach(new FileDataDump()); //ファイル情報をダンプ
    }
}