Java中的Java.io.BufferedReader类

作者: Arvin Chen 分类: Java 来源: Break易站(www.breakyizhan.com)
  •   Java I/O总结

    Java中的Java.io.BufferedReader类

    从字符输入流中读取文本,缓冲字符以提供字符,数组和行的高效读取。

    • 缓冲区大小可以被指定,或者可以使用默认大小。默认值对于大多数目的而言足够大。
    • 通常,由Reader构成的每个读取请求都会导致相应的读取请求由底层字符或字节流组成。
    • 因此,建议将BufferedReader包装在read()操作可能代价高昂的Reader中,如FileReaders和InputStreamReaders。
    • 使用DataInputStreams进行文本输入的程序可以通过用适当的BufferedReader替换每个DataInputStream来进行本地化。

    构造函数:

    • BufferedReader(Reader in):创建使用默认大小输入缓冲区的缓冲字符输入流。
    • BufferedReader(Reader in,int sz):创建一个使用指定大小的输入缓冲区的缓冲字符输入流。

    方法:

    • void close():关闭该流并释放与it.Once相关的所有系统资源的流已关闭,再调用read(),ready(),mark(),reset()方法,或skip()调用将抛出IOException 。关闭以前关闭的流不起作用。
      Syntax :public void close()
                 throws IOException
      Throws:
      IOException
    • 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.
      Throws:
      IllegalArgumentException - If readAheadLimit is < 0
      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, as an integer in the range 0 to 65535 (0x00-0xffff),
      or -1 if the end of the stream has been reached
      Throws:
      IOException
    • int read(char [] cbuf,int off,int len):将字符读入数组的一部分。
      该方法实现Reader类的相应读取方法的通用契约。作为额外的便利,它通过重复调用底层流的读取方法尝试读取尽可能多的字符。这种迭代式读取会一直持续到下列条件之一成立为止:

      • 指定的字符数已被读取,
      • 基础流的读取方法返回-1,表示文件结束,或
      • 基础流的就绪方法返回false,表示进一步的输入请求会阻塞。

      如果基础流上的第一次读取返回-1以指示文件结束,则此方法返回-1。否则,此方法将返回实际读取的字符数。

      Syntax :public int read(char[] cbuf,
             int off,
             int len)
               throws IOException
      Parameters:
      cbuf - Destination buffer
      off - Offset at which to start storing 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
    • String readLine():读取一行文本。一行被认为是由换行符('\ n'),回车符('\ r')或一个回车符后面紧跟的一个回车符换行。
      Syntax :public String readLine()
                      throws IOException
      Returns:
      A String containing the contents of the line, not including any 
      line-termination characters, 
      or null 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, false otherwise. 
      Note that returning false does not guarantee that the next read will block.
      Throws:
      IOException
    • void reset():将流重置为最近的标记。
      Syntax :public void reset()
                 throws IOException
      Throws:
      IOException
    • 长跳过(长ñ):跳过字符。
      • Syntax :public long skip(long n)
                  throws IOException
        Parameters:
        n - The number of characters to skip
        Returns:
        The number of characters actually skipped
        Throws:
        IllegalArgumentException
        IOException
    //Java program demonstrating BufferedReader methods
    import java.io.BufferedReader;
    import java.io.FileReader;
    import java.io.IOException;
    class BufferedReaderDemo
    {
    	public static void main(String[] args) throws IOException 
    	{
    		FileReader fr = new FileReader("file.txt");
    		BufferedReader br = new BufferedReader(fr);
    		char c[]=new char[20];
    
    		//illustrating markSupported() method
    		if(br.markSupported())
    		{
    			System.out.println("mark() method is supported");
    		    
    		    //illustrating mark method
    			br.mark(100);
    		}
    		
    		/*File Contents
    		* This is first line
    		this is second line
    		*/
    		
    		//skipping 8 characters
    		br.skip(8);
    
    		//illustrating ready() method
    		if(br.ready())
    		{
    			//illustrating readLine() method
    			System.out.println(br.readLine());
    
    			//illustrating read(char c[],int off,int len)
    			br.read(c);
    			for (int i = 0; i &lt;20 ; i++)
    			{
    				System.out.print(c[i]);
    			}
    			System.out.println();
    
    			//illustrating reset() method
    			br.reset();
    			for (int i = 0; i &lt;8 ; i++) 
    			{
    				//illustrating read() method
    				System.out.print((char)br.read());
    			}
    		}
    	}
    }
    
    

    输出:

    mark() method is supported
    first line
    this is second line
    This is
  •   Java I/O总结
  •   本文标题:Java中的Java.io.BufferedReader类 - Break易站
    转载请保留页面地址:https://www.breakyizhan.com/java/4271.html
      微信返利机器人
      免费:淘宝,京东,拼多多优惠券
      腾讯,爱奇艺,优酷的VIP视频免费解析,免费看
      即刻扫描二维码,添加微信机器人!

    发表笔记

    电子邮件地址不会被公开。 必填项已用*标注