Java 文字ストリーム

文字ストリーム(character stream)

java.io パッケージのクラスのうち、文字ストリーム(character stream)関連クラスは次のとおりである。

  • Reader
    • BufferedReader
    • InputStreamReader
      • FileReader
  • Writer
    • BufferedWriter
    • OutputStreamWriter
      • FileWriter

Reader/Writer 抽象クラス

Writer クラスと Reader クラスは抽象クラスであるため、実際のオブジェクトを生成することはできない。

Reader 抽象クラス

Reader クラスは、文字ストリームの入力機能を提供する。

Reader の主なメソッド

メソッド 説明
abstract void close() throws IOException 文字入力ストリームを閉じる。
void mark(int limit) throws IOException 文字入力ストリームの現在位置をマークする。
boolean markSupported() 文字ストリームが mark() メソッドをサポートしているかどうかを返す。
int read() throws IOException 文字入力ストリームから単一の文字を読み取る。
int read(char buf[]) throws IOException 文字入力ストリームから buf[] サイズ分を読み取って buf に保存し、読み取った文字数を返す。
abstract int read(char buf[], int off, int len) throws IOException 文字入力ストリームから len 分を読み取り、buf[] の off 位置に保存し、読み取った文字数を返す。
int read(CharBuffer target) throws IOException CharBuffer 型の target に文字列を読み込む。
boolean ready() throws IOException 文字入力ストリームが準備できているかどうかを返す。
void reset() throws IOException 文字入力ストリームを mark された位置に戻す。
long skip(long l) throws IOException 指定された数 l 分の文字をスキップする。

Writer 抽象クラス

Writer クラスは、文字ストリームの出力機能を提供する。

Writer の主なメソッド

メソッド 説明
Writer append(char c) throws IOException Writer に Character c を追加する。
Writer append(CharSequence csq) throws IOException Writer に CharSequence csq を追加する。
Writer append(CharSequence csq, int start, int end) throws IOException Writer に CharSequence csq の start から end までの文字を追加する。
abstract void close() throws IOException 文字出力ストリームを閉じる。
abstract void flush() throws IOException バッファに残った出力ストリームを出力する。
void write(String str) throws IOException 指定された文字列 str を出力する。
void write(char buf[]) throws IOException buf の内容を出力する。
void write(char buf[], int off, int len) throws IOException buf の off 位置から len 分の文字を出力する。
void write(String str, int off, int len) throws IOException 指定された文字列 str にある文字を off 位置から len 分出力する。