📜  Java Java类

📅  最后修改于: 2022-05-13 01:54:53.735000             🧑  作者: Mango

Java Java类

这是一个字符流类,其源是一个字符串。
构造函数:

  • StringReader(String s) :创建一个新的字符串阅读器。

    方法 :

    • void close() :关闭流并释放与之关联的任何系统资源。一旦流被关闭,进一步的 read()、ready()、mark() 或 reset() 调用将抛出 IOException。关闭以前关闭的流没有效果。
      Syntax :public void close()
      
    • void mark(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
    • boolean markSupported() :判断此流是否支持 mark() 操作,它支持。
      Syntax :public boolean markSupported()
      Returns:
      true if and only if this stream supports the mark operation.
    • 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
    • int read(char[] cbuf, int off, int len) :字符读入数组的一部分。
      Syntax :public int read(char[] cbuf,
             int 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
    • boolean ready() :判断该流是否准备好被读取。
      Syntax :public boolean ready()
                    throws IOException
      Returns:
      True if the next read() is guaranteed not to block for input
      Throws:
      IOException
    • void reset() :将流重置为最近的标记,如果从未标记过,则重置为字符串的开头。
      Syntax :public void reset()
                 throws IOException
      Throws:
      IOException 
    • long skip(long ns) :跳过流中指定数量的字符。返回跳过的字符数。
      ns 参数可能为负数,即使 Reader 超类的 skip 方法在这种情况下会抛出异常。 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

    程序 :

    //Java program demonstrating StringReader methods
    import java.io.IOException;
    import java.io.StringReader;
    class StringReaderDemo
    {
        public static void main(String[] args) throws IOException
        {
            StringReader str = new StringReader("GeeksforGeeks");
            char c[]=new char[7];
      
            //illustrating markSupported()
            if(str.markSupported())
            {
                System.out.println("Mark method is supported");
                // 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 off,int len)
                str.read(c);
                for (int i = 0; i <7 ; i++) 
                {
                    System.out.print(c[i]);
                }
            }
              
            //illustrating reset() method
            str.reset();
              
            for (int i = 0; i < 5; i++) 
            {
                System.out.print((char)str.read());
            }
              
            //illustrating close()
            str.close();
        }
    }
    

    输出 :

    Mark method is supported
    forGeeksGeeks