サンプルプログラム

import java.io.*;

// コマンドライン引数で与えられたファイル名のテキストファイルの内容を標準出力に出力する
public class TextViewer {
    private BufferedReader in = null;
    public TextViewer(String filename, String encoding) {
	// ファイルを開く
        try {
	    in = new BufferedReader(
		     new InputStreamReader(
		         new FileInputStream(filename), encoding));
        }
        catch (IOException e) {
            System.err.println("ファイルが開けません: " + filename);
            System.exit(1);  // 終了コード1で終了 (0以外は異常終了)
        }
    }
    public void print() {
	// ファイルの読み込みと出力
        try {
	    String line;
	    while(true) {
		line = in.readLine();
		if (line == null)
		    break;
                System.out.println(line);
            }
        }
        catch (IOException e) {
            System.err.println("ファイルの読み込み途中でエラーが発生しました。");
        }

	// ファイルを閉じる
        try {
            in.close();
        }
        catch (IOException e) {
            System.err.println("ファイルを閉じるときにエラーが発生しました。");
        }
    }
    public static void main(String args[]) {
        TextViewer viewer = new TextViewer(args[0], "JISAutoDetect");
	viewer.print();
    }
}