勝手に添削

つながル!で研究室のjavaゼミに参加している1年生がソースを公開していたので、勝手に添削してみた。つながル!に書くと醜いので、こちらにあげます。

修正前

import java.io.*;
import java.util.*;
import java.text.*;

public class Feelingood{
  public static void main(String args[]){
    
    Date date = new Date();
    DateFormat df = DateFormat.getDateTimeInstance(DateFormat.SHORT,DateFormat.LONG);
    
    try{
      FileOutputStream fs = new FileOutputStream(args[1] + ".txt");
      OutputStreamWriter os = new OutputStreamWriter(fs);
      BufferedWriter bw = new BufferedWriter(os);
      
      
      bw.write("今の気分は → " + args[0]);
      bw.newLine();
      bw.write("更新時刻 " + df.format(date));
      
      bw.close();
      
      
    } catch (ArrayIndexOutOfBoundsException e) {
      try{
        BufferedWriter bw2 = new BufferedWriter(
          new OutputStreamWriter(
            new FileOutputStream("エラー.txt")));
        
        bw2.write("ファイルの書き込みに失敗しました");
        bw2.newLine();
        
        if (args[0] == null){
          bw2.write("※気分が未記入です");
        } else if (args[1] == null) {
          bw2.write("※ファイル名が未記入です");
        } else if (args[0] == null && args[1] == null) {
          bw2.write("※ファイル名と気分が未記入です");
        }
        
        bw2.newLine();
        bw2.write(df.format(date));
        
        bw2.close();
        
        System.out.println("ファイルの書き込みに失敗しました");
        e.printStackTrace();
        
      } catch (IOException e2) {
        System.out.println("ファイルの書き込みに失敗しました");
        e2.printStackTrace();
      }
    } catch (IOException e1) {
      System.out.println("ファイルの書き込みに失敗しました");
      e1.printStackTrace();
    }
  }
}
気になったところ
  • ArrayIndexOutOfBoundsExceptionをcatchしている

この例外はRuntimeExceptionで正しくプログラムを書いていれば本来起こりえないものであるので、catchするべきではない。

  • finally句がない

まずBufferdWriterは必ずclose()しなければならない。
一見このプログラムはちゃんとclose()しているように見えるが、close()が呼ばれる前に、何か例外が起こった場合はその時点でcatch句に飛んでしまうのでclose()されない可能性がある。
そのような場合はfinally句を用いてclose()すればよい。

  • 条件分岐

ちゃんと読めばわかるはずなので割愛

  • mainの引数

正しくはmain(String[] args)


以上の点を踏まえて修正してみる。

修正後

import java.io.*;
import java.util.*;
import java.text.*;

public class Feelingood{
  public static void main(String[] args){
    
    Date date = new Date();
    DateFormat df = DateFormat.getDateTimeInstance(DateFormat.SHORT,DateFormat.LONG);
    BufferedWriter bw=null;
    try{
      if(args.length >=2 ){
        bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(args[1])));
        bw.write("今の気分は → " + args[0]);
      } else {
        bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("エラー.txt")));
        bw.write("引数が足りません");
        bw.newLine();
        bw.write("java Feelngood 気分を表す文字列 ファイル名 " );
        bw.newLine();
        bw.write("のように実行してください");
      }
      bw.newLine();
      bw.write("更新時刻 " + df.format(date));
    } catch (IOException e) {
      e.printStackTrace();
    } finally {
      try{
        if(bw !=null) bw.close();
      } catch (IOException e){
        e.printStackTrace();
      }
    }
  }
}

俺ならこう書くかな。
わざわざJDKを入れてコンパイルしてみたので、正しく動くはずです。