テキストファイル読み込み用クラス

これまでC# で作成してきた「SourceToHTML」を、VJ# に焼きなおしてみます。

Reader.jsl


package SourceToHtml;

//*******************************************************************************************************
// テキストファイル 読み込み用クラス
//*******************************************************************************************************
public class Reader
{
private static final char EOF = '\0';
private static final char NEWLINE = '\n';

private System.IO.StreamReader textReader;
private boolean IsOpen = false;
private String fileName = "";
private String readBuffer = "";
private int readIndex = -1;
private boolean FileExists = true;
//---------------------------------------------------------------------------------------------------
// 初期化
//---------------------------------------------------------------------------------------------------
public Reader(String fileName)
{
this.fileName = fileName;
}
//---------------------------------------------------------------------------------------------------
// 終了
//---------------------------------------------------------------------------------------------------
protected void Finalize()
{
Close();
}
private void Close()
{
//OPENしてたら
if (IsOpen)
{
//CLOSEする
textReader.Close();
textReader = null;
IsOpen = false;
}
}
//---------------------------------------------------------------------------------------------------
// 入力ファイルから1文字ずつ読む
//---------------------------------------------------------------------------------------------------
public char getChar()
{
//ファイルがなければ、EOFを返す
if (!FileExists) return EOF;

//まだOPENしてなかったら
if (!IsOpen)
{
//ファイルがあれば
if (System.IO.File.Exists(fileName))
{
//OPENする
textReader = new System.IO.StreamReader(fileName, System.Text.Encoding.GetEncoding("Shift_JIS"));
IsOpen = true;
}
else
{
//ファイルがなければ、EOFを返す
FileExists = false;
return EOF;
}
}

//現在位置が初期化されていたら (読み込み用バッファが空っぽ)
if (readIndex < 0)
{
//次の行を読む
readBuffer = textReader.ReadLine();

//次の行がなければ
if (readBuffer == null) return EOF;

//現在位置を行の先頭に位置づける
readIndex = 0;
}

//現在位置が読み込み用バッファの最後に達したら
if (readIndex >= readBuffer.get_Length())
{
//現在位置を初期化する
readIndex = -1;
//改行コードを返す
return NEWLINE;
}

//読み込み用バッファから1文字返し、現在位置を1つ進める
return readBuffer.get_Chars(readIndex++);
}
}