LoginSignup
0
0

More than 5 years have passed since last update.

enumを利用してcsvファイルを固定長レコードファイルに変換

Posted at

enumあまり使ったことなかったので練習で作ってみました。
実用性は不明です。
javaのバージョンは6。

・ソース

SamleFixedLengthFromCsv.java
package prottype;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;

import au.com.bytecode.opencsv.CSVReader;

public class SamleFixedLengthFromCsv {

    // レコード定義:文字数
    enum recordDef {
        col01(8), col02(24), col03(24), col04(3);

        private int length;

        private recordDef(int length) {
            this.length = length;
        }

        public int getLength() {
            return this.length;
        }
    }

    // レコード定義:(デフォルト文字,padding文字)
    enum recordVal {
        col01("0,0"), col02(", "), col03(", "), col04("---,-");

        private recordVal(String val) {
            this.val = val;
        }

        private String val;

        public void setVal(String val) {
            this.val = val;
        }

        public String getVal() {
            return this.val;
        }
    }

    /**
     * mainクラス
     */
    public static void main(String[] args) throws IOException {
        SamleFixedLengthFromCsv proc = new SamleFixedLengthFromCsv();
        proc.execute();
    }

    /**
     * 実行
     */
    protected void execute() {
        try {
            mainProc();
        } catch (Exception e) {
            System.out.println("ファイル出力に失敗・・・。");
        }
    }

    /**
     * メインプロセス
     */
    public void mainProc() throws Exception {
        PrintWriter pw = null;
        CSVReader reader = null;
        try {
            File file = new File("./test.dat");

            reader = new CSVReader(new FileReader(new File("./test.csv")));

            file.delete();
            file.createNewFile();
            pw = new PrintWriter(new BufferedWriter(new FileWriter(file)));

            String[] record;
            while ((record = reader.readNext()) != null) {
                recordVal.col01.setVal(record[0] + ",0");
                recordVal.col02.setVal(record[1]+", ");
                recordVal.col03.setVal(record[2]+", ");
                recordVal.col04.setVal(record[3]+ ",-");
                pw.print(createFixedRecords() +  System.getProperty("line.separator") );
            }
            pw.close();
            reader.close();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (pw != null) {
                pw.close();
            }
            if (reader != null) {
                reader.close();
            }
        }
    }


    /**
     * 固定長レコード作成
     */
    private String createFixedRecords() {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < recordVal.values().length; i++) {
            sb.append(createFixedColumn(i));
        }
        return sb.toString();
    }

    /**
     * 項目を固定長に変換
     */
    private String createFixedColumn(int defNum) {
        StringBuilder tempPad = new StringBuilder();
        StringBuilder val = new StringBuilder(recordVal.values()[defNum].getVal().split(",")[0]);
        StringBuilder pad = new StringBuilder(recordVal.values()[defNum].getVal().split(",")[1]);

        if (val.length() < recordDef.values()[defNum].length) {
            for (int i = val.length(); i < recordDef.values()[defNum].length; i++) {
                tempPad.append(pad);
            }
        }

        // 数値は右詰めそれ以外は左詰め
        try {
            Integer.parseInt(val.toString());
            return tempPad.append(val).toString();
        } catch(Exception e) {
            return val.append(tempPad.toString()).toString();
        }
    }
}

・実行結果

before

test.csv(変換元ファイル)
"1","戦艦","金剛","JPN"
"2","重巡洋艦","高雄","JPN"
"3","駆逐艦","時雨","JPN"

after

test.csv(変換元ファイル)
00000001戦艦                      金剛                      JPN
00000002重巡洋艦                    高雄                      JPN
00000003駆逐艦                     時雨                      JPN

enumもどんどん使って慣れたいですね。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0