Stream reader and writer (filter reader and filter writer)

Source: Internet
Author: User

InputstreamreaderAndOutputstreamwriterClass is equivalent to the decorator on the input and output streams, and the byte-oriented interface is changed to the character-oriented interface. After that, you can use character-oriented filter readers or filter writers on top of them. Like a filter stream, many sub-classes can perform specific filtering, including:

  • Bufferedreader

  • Bufferedwriter

  • Linenumberreader

  • Pushbackreader

  • Printwriter

The bufferedreader and bufferedwriter classes are character-based and correspond to the byte-oriented bufferedinputstream and bufferedoutputstream classes. Bufferedinputstream and bufferedoutputstream use an internal byte array as the buffer. Correspondingly, bufferedreader and bufferedwriter use an internal character array as the buffer.

When the program reads data from bufferedreader, the text is obtained from the buffer, rather than directly reading data from the underlying input stream or other source files. When the buffer zone is empty, it will be filled with as much text as possible, although not all of the text is required immediately, so that later reading speed can be faster. When the program writes a bufferedwriter, the text is placed in the buffer. Text is moved to the underlying output stream or other targets only when the buffer is full or when the writer refreshes the output explicitly, which makes writing much faster.

Bufferedreader and bufferedwriter also have common methods associated with the reader and writer, such as read (), ready (), writer (), and close (). Both classes have two constructors. You can chain bufferedreader or bufferedwriter to an underlying reader or writer and set the buffer size. If no size is set, the default size is used.8192Character:

public BufferedReader(Reader in,int bufferSize)public BufferedReader(Reader in)public BufferedWriter(Writer out)public BufferedWriter(Writer out,int bufferSize)

We use bufferedreader to refactor the example in the previous section:

Package IO; import Java. io. bufferedreader; import Java. io. file; import Java. io. fileinputstream; import Java. io. ioexception; import Java. io. inputstreamreader; public class inputstreamreadertest {public static void main (string [] ARGs) {// note here try (bufferedreader reader = new bufferedreader (New inputstreamreader (New fileinputstream (new file ("/home/fuhd/text"), "utf8 "), 1024) {int C; stringbuffer sb = new Stringbuffer (); While (C = reader. Read ())! =-1) {sb. append (char) C);} system. out. println (sb. tostring ();} catch (ioexception e) {e. printstacktrace ();}}}

The bufferedreader class also has a Readline () method, whichRead a line of textAnd return as a string:

public String readLine() throws IOException

This methodAlternativeIn datainputstreamAbandonedOfReadline() Method, which is basically the same as the behavior of this method. The main difference is that by concatenating bufferedreader to inputstreamreader, you can use the correct character set to read rows, instead of using the default encoding method of the platform. Example:

Package IO; import Java. io. bufferedreader; import Java. io. file; import Java. io. fileinputstream; import Java. io. ioexception; import Java. io. inputstreamreader; public class inputstreamreadertest {public static void main (string [] ARGs) {try (bufferedreader reader = new bufferedreader (New inputstreamreader (New fileinputstream (new file ("/home/fuhd/text"), "utf8"), 1024) {system. out. println (reader. readline (); // read a row directly here} catch (ioexception e) {e. printstacktrace ();}}}

This bufferedwriter class adds a new method not available for its superclasses, namedNewline() Is also used to write a row:

public void newLine() throws IOException

This methodOutputInsertA platform-relatedLine SeparatorString. The system attribute of line. Separator determines what the string is: it may beLine feedIn Mac OS 9EnterIn WindowsCarriage return/line feed. Because the network protocol usually specifies the required line terminatorDo not use this method in Network ProgrammingAnd explicitly write the row Terminator required by the Protocol. In most cases, all the Terminator is a carriage return/line feed pair.

Stream reader and writer (filter reader and filter writer)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.