JavaのStringReaderクラス ソースが文字列である文字ストリーム クラスです。 Readerクラスを継承します。ネットワークソケットやファイルなどのシステムリソースは使用されないため、StringReader を閉じる必要はありません。 JavaのStringReaderクラスについてさらにポイントを確認してみましょう。
JavaでStringReaderクラスを宣言する
public class StringReader extends Reader
Java StringReader クラスのコンストラクター
Java の StringReader クラスで使用されるコンストラクターを以下に示します。
StringReader(String s) : Creates a new string reader.
Java StringReader クラスのメソッド
Java の StringReader クラスのメソッドを以下に示します。
| 方法 | 説明 |
|---|---|
| int read() | 単一文字を読み取ります |
| int read(char[] cbuf int off int len) | 文字を配列の一部に読み取ります |
| ブール値の準備完了() | このストリームを読み取る準備ができているかどうかを示します |
| ブールマークサポート済み() | ストリームサポートマークかどうかを示します |
| void マーク(int readAheadLimit) | ストリーム内に存在する位置に存在するマークをマークします |
| ボイドリセット() | ストリームを最新のマークにリセットするか、マークされていない場合は文字列の先頭にリセットします。 |
| ロングスキップ(ロングns) | ストリーム内の指定された数の文字をリセットします |
| ボイドクローズ() | ストリームを閉じます |
1. int read()
単一文字を読み取ります。
Syntax : public int read() throws IOException Returns: The character read or -1 if the end of the stream has been reached Throws: IOException
2. int read(char[] cbuf int off int len)
文字を配列の一部に読み取ります。
Syntax : public int read(char[] cbufint off int len) throws IOException Parameters: cbuf - Destination buffer off - Offset at which to start writing characters len - Maximum number of characters to read Returns: The number of characters read or -1 if the end of the stream has been reached Throws: IOException
3. ブール値のready()
このストリームを読み取る準備ができているかどうかを示します。
Syntax : public boolean ready() throws IOException Returns: True if the next read() is guaranteed not to block for input Throws: IOException
4. ブール値 markSupported()
このストリームが mark() 操作をサポートしているかどうかを示します。
Syntax : public boolean markSupported() Returns: true if and only if this stream supports the mark operation.
5. void マーク(int readAheadLimit)
ストリーム内の現在位置をマークします。後続のreset()の呼び出しにより、ストリームの位置がこのポイントに再設定されます。
Syntax : public void mark(int readAheadLimit) throws IOException Parameters: readAheadLimit - Limit on the number of characters that may be read while still preserving the mark. Because the stream's input comes from a string there is no actual limit so this argument must not be negative but is otherwise ignored. Throws: IllegalArgumentException IOException
6. void リセット()
ストリームを最新のマークにリセットするか、マークされていない場合は文字列の先頭にリセットします。
Syntax : public void reset() throws IOException Throws: IOException
7. ロングスキップ(ロングns)
ストリーム内の指定された数の文字をスキップします。スキップされた文字数を返します。この場合、Reader スーパークラスのスキップ メソッドが例外をスローしても、ns パラメータは負の値になる場合があります。 ns の値が負の場合、ストリームは逆方向にスキップされます。負の戻り値は、後方へのスキップを示します。文字列の先頭を越えて後方にスキップすることはできません。文字列全体が読み取られたかスキップされた場合、このメソッドは効果がなく、常に 0 を返します。
Syntax : public long skip(long ns) throws IOException Parameters: ns - The number of characters to skip Returns: The number of characters actually skipped Throws: IOException
8. void close()
ストリームを閉じ、それに関連付けられているシステム リソースを解放します。ストリームが閉じられると、さらに read()、ready()、mark() または replace() を呼び出すと IOException がスローされます。以前に閉じたストリームを閉じても効果はありません。
Syntax : public void close()
例
Java// Java program demonstrating StringReader methods import java.io.IOException; import java.io.StringReader; // Driver class class StringReaderDemo { // main function public static void main(String[] args) throws IOException { StringReader str = new StringReader( " GeeksforGeeks & quot;); char c[] = new char[7]; // illustrating markSupported() if (str.markSupported()) { System.out.println( " Mark method is supported & quot;); // illustrating mark() str.mark(100); } // illustrating skip() method str.skip(5); // whether this stream is ready to be read. if (str.ready()) { // illustrating read() method System.out.print((char)str.read()); // illustrating read(char cff[]int offint len) str.read(c); for (int i = 0; i & lt; 7; i++) { System.out.print(c[i]); } } // illustrating reset() method str.reset(); for (int i = 0; i & lt; 5; i++) { System.out.print((char)str.read()); } // illustrating close() str.close(); } }
出力
Mark method is supported forGeeksGeeks
クイズの作成