"JAVA I/o" character output stream writer brief summary

Source: Internet
Author: User

Steal a lazy, directly refer to the previous character input stream reader form, in fact, reader and writer are similar;

Character output stream writer

This article makes a brief summary of the character output stream writer in the Java I/O stream:

In a sense, each character output stream class has a corresponding purpose, as follows:

    • Character output stream base class: Writer
    • BYTE stream character streams: OutputStreamWriter //toward byte output stream uppercase characters
    • Character file write Related: FileWriter //write characters in file
    • Character array-to-character stream: Chararraywriter //characters in an array
    • Thread internal communication (pipeline): PipedWriter //write characters to pipe
    • String to character stream:StringWriter //writes characters to StringBuffer, constructs a string
    • Buffered stream:bufferedwriter //can decorate other character output stream, increase buffering function
    • Filter Flow: Filterwriter //can decorate other character output stream, increase filtering function
    • Formatted write character:printwriter: //can decorate other character output stream, increase the function of formatted write data, etc.

Then look at the composition structure of writer (including the character input stream reader);

The following will be a detailed description of each character output stream;

OutputStreamWriter

OutputStreamWriter inherits the character stream abstract class writer, each construction method contains a byte stream OutputStream, which allows us to write characters into the byte output stream ;

PS: Whether it is the processing of the byte stream in the construction method (Streamdecoder.foroutputstreamwriter), or the reading of the character stream (Streamdecoder.write), the bottom layer is implemented through the Streamdecoder class, interested in in-depth understanding ~

For example, for the convenience of reading, do not do exception handling:

 PackageCom.pichen.io;Importjava.io.IOException;ImportJava.io.OutputStreamWriter;ImportJava.io.Writer; Public classIotest { Public Static voidMain (string[] args)throwsIOException {//a character output stream is obtained through the "standard" byte output streamWriter writer =NewOutputStreamWriter (System.out); //write characters to a character streamWriter.write ("hello~"); //Close the streamWriter.close (); }}
FileWriter

FileWriter inherits the character stream OutputStreamWriter , writes the characters to the local file , and views its construction method, is to generate a FileOutputStream byte stream object based on the parameters passed in, and then call the constructor of the parent class OutputStreamWriter to get the character stream;

To give a simple example:

 PackageCom.pichen.io;ImportJava.io.FileWriter;Importjava.io.IOException;ImportJava.io.Writer; Public classIotest { Public Static voidMain (string[] args)throwsIOException {//get character output streamWriter writer =NewFileWriter ("C:\\Test.txt"); //Write charactersWriter.write (' a '); //refreshes the buffer of the streamWriter.flush (); //Close the streamWriter.close (); }}
Chararraywriter

CharArrayReader can let us write characters in the character array , in the construction method can specify the character array size, and subsequent in the write process, will automatically increase the character array;

To give a simple example:

 PackageCom.pichen.io;ImportJava.io.CharArrayWriter;Importjava.io.IOException; Public classIotest { Public Static voidMain (string[] args)throwsIOException {//Create a character stream that specifies the initial buffer sizeChararraywriter writer =NewChararraywriter (6); //Write charactersWriter.write ("AAABBB"); //To get an array of characters from the character output streamSystem.out.println (Writer.tochararray ()); //Close the streamWriter.close (); }}
PipedWriter

You can create a character stream pipeline through PipedWriter and Pipedreader, and you can communicate between threads through a pipeline, note: Be sure to have two threads in the same JVM;

PipedWriter is typically used in conjunction with Pipedreader, where one thread writes data through PipedWriter to the pipe, another thread reads data from the pipe through Pipedreader, and reads and writes blocks threads, as in the following example:

 PackageCom.pichen.io;Importjava.io.IOException;ImportJava.io.PipedReader;ImportJava.io.PipedWriter;ImportJava.util.concurrent.ExecutorService;Importjava.util.concurrent.Executors; Public classIotest { Public Static voidMain (string[] args)throwsIOException {FinalPipedWriter PW =NewPipedWriter (); FinalPipedreader PR =NewPipedreader (PW); Executorservice es= Executors.newfixedthreadpool (2); Es.execute (NewRunnable () {@Override Public voidrun () {Try{pw.write ("Hello~"); } Catch(IOException e) {e.printstacktrace ();        }            }        }); Es.execute (NewRunnable () {@Override Public voidrun () {Char[] Cbuffer =New Char[6]; Try {                    //can cause thread blockingPr.read (cbuffer, 0, 6); } Catch(IOException e) {e.printstacktrace ();            } System.out.println (Cbuffer);    }        }); }}
BufferedWriter

This character stream can be used to decorate other character output stream, can provide character output buffer for other character output stream, write once at a time, avoid one byte at a time to write data to the external media, here used in the design pattern of the adorner mode, you can refer to my previous writing,

Http://www.cnblogs.com/chenpi/p/5173818.html

The character stream that is decorated can have more behaviors, such as newline method;

To use an example, read the external file:

 PackageCom.pichen.io;ImportJava.io.BufferedWriter;ImportJava.io.FileWriter;Importjava.io.IOException; Public classIotest { Public Static voidMain (string[] args)throwsIOException {//Create a buffered character output stream, where the buffer size is set to 10kBufferedWriter writer =NewBufferedWriter (NewFileWriter ("C:\\Test.txt"), 10 * 1024); //write character to bufferWriter.write ("AAA");        Writer.newline (); Writer.write ("BBB"); //Close the streamWriter.close (); }}
Filterwriter

Abstract class Filterwriter is to implement a custom filter output character stream of the base class, from the implementation of the source code, just simply cover all the methods in writer, feel no eggs, because there is already an abstract class writer;

StringWriter

Character output stream, you can construct a string, which is implemented internally by StringBuffer,

Give a simple example of use

 PackageCom.pichen.io;Importjava.io.IOException;ImportJava.io.StringWriter; Public classIotest { Public Static voidMain (string[] args)throwsIOException {stringwriter writer=NewStringWriter (); Writer.write ("Hello"); Writer.write ("World~");    System.out.println (Writer.tostring ()); }}
PrintWriter

Allows formatting data to be written to the character output stream, as shown in the following example, to write formatted data to the console;

 PackageCom.pichen.io;Importjava.io.IOException;ImportJava.io.PrintWriter; Public classIotest { Public Static voidMain (string[] args)throwsIOException {printwriter writer=NewPrintWriter (System.out); Writer.write ("Hello"); Writer.write ("World~"); writer.printf ("%5D", 101);    Writer.close (); }}

"JAVA I/o" character output stream writer brief summary

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.