Classe StringReader en Java és una classe de flux de caràcters la font de la qual és una cadena. Hereta la classe Reader.El tancament del StringReader no és necessari perquè no s'utilitzen recursos del sistema com els endolls de xarxa i els fitxers. Comprovem més punts sobre la classe StringReader a Java.
Declarar la classe StringReader a Java
public class StringReader extends Reader
Constructor a la classe Java StringReader
El constructor utilitzat amb StringReader Class a Java s'esmenta a continuació:
StringReader(String s) : Creates a new string reader.
Mètodes a la classe Java StringReader
Els mètodes de la classe StringReader a Java s'esmenten a continuació:
| Mètode | Descripció |
|---|---|
| int read() | Llegeix un sol caràcter |
| int read(char[] cbuf int off int len) | Llegeix caràcters en una part d'una matriu |
| llest booleà () | Indica si aquest flux està llest per ser llegit |
| marca booleà admesa () | Indica si la marca de suport de la reproducció |
| marca nul (int readAheadLimit) | Marca la marca present a la posició present al flux |
| void reset() | Restableix el flux a la marca més recent o al començament de la cadena si no s'ha marcat mai. |
| salt llarg (ns llargs) | Restableix el nombre especificat de caràcters en un flux |
| void close() | Tanca el corrent |
1. int read()
Llegeix un sol caràcter.
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)
Llegeix caràcters en una part d'una matriu.
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. llest booleà ()
Indica si aquest flux està llest per ser llegit.
Syntax : public boolean ready() throws IOException Returns: True if the next read() is guaranteed not to block for input Throws: IOException
4. Boolean markSupported()
Indica si aquest flux admet l'operació mark() que fa.
Syntax : public boolean markSupported() Returns: true if and only if this stream supports the mark operation.
5. marca nul (int readAheadLimit)
Marca la posició actual al flux. Les trucades posteriors a reset() reposicionaran el flux fins a aquest punt.
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. Vod Reset()
Restableix el flux a la marca més recent o al començament de la cadena si no s'ha marcat mai.
Syntax : public void reset() throws IOException Throws: IOException
7. salt llarg (ns llargs)
Omet el nombre especificat de caràcters al flux. Retorna el nombre de caràcters que s'han omès. El paràmetre ns pot ser negatiu tot i que el mètode skip de la superclasse Reader genera una excepció en aquest cas. Els valors negatius de ns fan que el flux es salti cap enrere. Els valors de retorn negatius indiquen un salt cap enrere. No és possible saltar cap enrere més enllà del principi de la cadena. Si s'ha llegit o s'ha omès tota la cadena, aquest mètode no té cap efecte i sempre retorna 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. tancar buit()
Tanca el flux i allibera tots els recursos del sistema associats. Un cop tancat el flux, més invocacions read() ready() mark() o reset() llançaran una IOException. Tancar un flux tancat anteriorment no té cap efecte.
Syntax : public void close()
Exemple
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(); } }
Sortida
Mark method is supported forGeeksGeeks
Crea un qüestionari