LoginSignup
3

More than 5 years have passed since last update.

【Java:備忘録】ファイルの読み込み&書き込むをするPG

Last updated at Posted at 2016-05-30

概要:
引数1で入力ファイルを指定し、引数2で出力するフルパスを指定するプログラム.

業務で良くある?データの加工処理が必要になった場合のテンプレ。
コード内の「args1で指定したフルパスへの出力処理~」の箇所に加工するコードを書き込む。

サンプルでは読み込んだファイルのダブルクォーテーションを削除する処理を行っている。

実行する際は
SAMPLE 入力ファイルのフルパス 出力ファイルのフルパス
で実行可能。

テンプレートとして使えそうなので記載。
バッチから起動する&System.out.printでログも出力出来ます。
System.out.printが組み込まれているのはその為.
System.out.printを日時を加工したメソッドを用意すればよかったと今では後悔。

何か初期処理が必要であれば、initializeメソッド等に書き込む(予定。書き込む事は未だなかったけど。)

ソース:

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.Calendar;


public class SAMPLE {

    /** システム日付 */
    public static String nowDay = null;

    /** システム時刻 */
    public static String nowDate = null;

    /**
     * @param args
     */
    public static void main(String[] args) {

        // ファイルの読込と、出力ファイル生成処理.
        try {
            // 初期処理
            initialize();

            System.out.println(nowDate + "Process Start");
            System.out.println(nowDate + "初期処理 終了");

            // 出力対象となるデータレコード
            StringBuffer bf = new StringBuffer();

            // 引数がない場合は処理を終了させる.
            if (args.length != 2) {
                System.out.println(nowDate + "引数は2つ指定してください.");
                System.out.println(nowDate + "処理を中断します....");
                System.exit(1);
            }

            // 読み込むファイルの名前
            String inFile = args[0];                // 入力ファイルのフルパス
            String outFile = args[1];               // 出力ファイルのフルパス

            // ファイルオブジェクトの生成
            File inputFile = new File(inFile);

            // 入力ストリームの生成(文字コード指定)
            FileInputStream fis = new FileInputStream(inputFile);
            InputStreamReader isr = new InputStreamReader(fis,"SJIS");
            BufferedReader br = new BufferedReader(isr);

            String msg = null;

            System.out.println(nowDate + "各種宣言処理まで完了");

            // args0で読込したファイルをループする。その際、ダブルクォーテーションを外していく..
            while ( ( msg = br.readLine()) != null ) {
                // 現在読み込んでいるHDレコード作成
                bf.append(msg.replace("\"", ""));
                bf.append("\r\n");
            }

            System.out.println(nowDate + "入力ファイル読込処理 完了");

            // args1で指定したフルパスへの出力処理
            // 処理をしたデータを片っ端から突っ込む。
            FileOutputStream fos = new FileOutputStream(outFile);
            OutputStreamWriter osw = new OutputStreamWriter(fos,"SJIS");
            PrintWriter pw = new PrintWriter(osw);

            pw.print(bf.toString());

            System.out.println(nowDate + "出力ファイルへの書込完了");

            // 後始末
            pw.close();
            br.close();

        // エラーがあった場合は、スタックトレースを出力
        } catch(Exception e) {
            e.printStackTrace();
            System.out.println(nowDate + "エラーが発生しました。処理を中止します。");
            System.exit(1);
        }

        System.out.println(nowDate + "Process End");
    }

    /**
     * 初期処理.
     *
     */
    private static void initialize() {

        // 日付取得・時刻取得
        Calendar now = Calendar.getInstance();
        int yyyy = now.get(Calendar.YEAR);
        int mm = now.get(Calendar.MONTH) + 1;
        int dd = now.get(Calendar.DATE);
        int h = now.get(Calendar.HOUR_OF_DAY);
        int m = now.get(Calendar.MINUTE);
        int s = now.get(Calendar.SECOND);

        nowDate = String.valueOf(yyyy) + "/" + String.valueOf(mm) + "/" + String.valueOf(dd) + " " + String.valueOf(h) + ":" + String.valueOf(m) + ":" + String.valueOf(s) + ":";
    }

}


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
3