Клас StringReader в Java е клас от символен поток, чийто източник е низ. Той наследява Reader Class.Затварянето на StringReader не е необходимо, защото не се използват системни ресурси като мрежови сокети и файлове. Нека проверим още точки за StringReader Class в Java.
Декларирайте клас StringReader в Java
public class StringReader extends Reader
Конструктор в Java StringReader Class
Конструкторът, използван с StringReader Class в Java, е споменат по-долу:
StringReader(String s) : Creates a new string reader.
Методи в Java StringReader Class
Методите в StringReader Class в Java са споменати по-долу:
| Метод | Описание |
|---|---|
| int read() | Чете един знак |
| int read(char[] cbuf int off int len) | Чете знаци в част от масив |
| булев готов() | Показва дали този поток е готов за четене |
| булево markSupported() | Показва дали знак за поддръжка на поток |
| void mark (int readAheadLimit) | Маркира присъстващия знак в позицията, присъстваща в потока |
| void reset() | Нулира потока до най-новата маркировка или до началото на низа, ако никога не е бил маркиран. |
| дълго пропускане (дълго ns) | Нулира определен брой знаци в поток |
| void close() | Затваря потока |
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. boolean ready()
Показва дали този поток е готов за четене.
Syntax : public boolean ready() throws IOException Returns: True if the next read() is guaranteed not to block for input Throws: IOException
4. boolean markSupported()
Показва дали този поток поддържа операцията mark(), която поддържа.
Syntax : public boolean markSupported() Returns: true if and only if this stream supports the mark operation.
5. празен знак (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 reset()
Нулира потока до най-новата маркировка или до началото на низа, ако никога не е бил маркиран.
Syntax : public void reset() throws IOException Throws: IOException
7. дълго прескачане (дълго ns)
Пропуска определения брой знаци в потока. Връща броя на пропуснатите знаци. Параметърът ns може да е отрицателен, въпреки че методът за прескачане на суперкласа Reader хвърля изключение в този случай. Отрицателните стойности на 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() или reset() ще хвърлят 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
Създаване на тест