Classe StringReader em Java é uma classe de fluxo de caracteres cuja origem é uma string. Ele herda a classe Reader.Fechar o StringReader não é necessário porque os recursos do sistema, como soquetes de rede e arquivos, não são usados. Vamos verificar mais pontos sobre a classe StringReader em Java.
Declarar classe StringReader em Java
public class StringReader extends Reader
Construtor na classe Java StringReader
O construtor usado com a classe StringReader em Java é mencionado abaixo:
StringReader(String s) : Creates a new string reader.
Métodos na classe Java StringReader
Os métodos na classe StringReader em Java são mencionados abaixo:
| Método | Descrição |
|---|---|
| leitura interna() | Lê um único caractere |
| int read(char[] cbuf int off int len) | Lê caracteres em uma parte de um array |
| booleano pronto() | Informa se este fluxo está pronto para ser lido |
| marca booleanaSuportada() | Informa se a marca de suporte ao stream |
| marca nula (int readAheadLimit) | Marca a marca presente na posição presente no stream |
| anular redefinição() | Redefine o fluxo para a marca mais recente ou para o início da string se nunca tiver sido marcado. |
| salto longo (ns longo) | Redefine o número especificado de caracteres em um fluxo |
| vazio fechar() | Fecha o fluxo |
1. leitura interna()
Lê um único caractere.
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)
Lê caracteres em uma parte de um array.
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. booleano pronto()
Informa se este fluxo está pronto para ser lido.
Syntax : public boolean ready() throws IOException Returns: True if the next read() is guaranteed not to block for input Throws: IOException
4. marca booleanaSuportada()
Informa se este fluxo suporta a operação mark() que ele suporta.
Syntax : public boolean markSupported() Returns: true if and only if this stream supports the mark operation.
5. marca nula (int readAheadLimit)
Marca a posição atual no fluxo. As chamadas subsequentes para reset() reposicionarão o fluxo até este ponto.
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. redefinição nula()
Redefine o fluxo para a marca mais recente ou para o início da string se nunca tiver sido marcado.
Syntax : public void reset() throws IOException Throws: IOException
7. salto longo(ns longo)
Ignora o número especificado de caracteres no fluxo. Retorna o número de caracteres que foram ignorados. O parâmetro ns pode ser negativo mesmo que o método skip da superclasse Reader gere uma exceção neste caso. Valores negativos de ns fazem com que o fluxo retroceda. Valores de retorno negativos indicam um retrocesso. Não é possível retroceder além do início da string. Se a string inteira foi lida ou ignorada, este método não terá efeito e sempre retornará 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. fechamento vazio()
Fecha o fluxo e libera quaisquer recursos do sistema associados a ele. Depois que o fluxo for fechado, outras invocações read() ready() mark() ou reset() lançarão uma IOException. Fechar um fluxo anteriormente fechado não tem efeito.
Syntax : public void close()
Exemplo
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(); } }
Saída
Mark method is supported forGeeksGeeks
Criar questionário