JunkUtil紹介ページ - サンプル(バッチ処理)

ホームJunkUtil紹介ページ>サンプル(バッチ処理)

概要

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

サンプル

package junkutil.sample;

import java.io.IOException;
import java.util.Iterator;
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 {
    /**
     * メイン
     * @param args
     */
    public static void main(String [] args) {
        args = new String[] { "-h", "testdata/readFile.txt", "testdata/writeFile.txt", "Nothing.txt" };
        try {
            OptGets optGets = new OptGets("abch");
            Map opts = optGets.parse(args);
            if (opts.get("h") == Boolean.TRUE) {
                System.out.println("====BatchTest Heaader====");
            }
            //Mapを出力する
            for (Iterator i = opts.keySet().iterator(); i.hasNext(); ) {
                Object key = i.next();
                System.out.println(key.toString() + "=" + opts.get(key).toString());
            }
            //ファイルを出力する
            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;
                } else {
                    System.out.println("File: " + filename);
                    List lines = FileUtil.readLines(filename, "Shift_JIS");
                    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 (IOException 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;

/**
 * 引数に与えられたファイルの中で、英字で始まる行だけを出力するプログラム。
 * 引数 -h でヘッダーを出力する。クロージャを活用する。
 * @author Hiroyuki Shioa
 */
public class BatchTest2 {

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

    /**
     * リストを出力するクロージャ
     */
    private static class ListDump implements LzListClosure {
        public boolean process(int i, int len, Object obj) {
            System.out.println(obj);
            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) {
                    e.printStackTrace();
                }
                return true;
            }
        }
    }

    /**
     * メイン
     * @param args
     */
    public static void main(String [] args) {
        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();
        new LzMap(opts).forEach(new MapDump()); //Mapをダンプ
        new LzList(argList).forEach(new FileDataDump()); //ファイル情報をダンプ
    }
}

Copyright bluecrow