Java 21-2 character output stream

Source: Internet
Author: User
Tags flushes

Character output stream: OutputStreamWriter

Construction method: Altogether 4, say 2 commonly used

A:outputstreamwriter (OutputStream out): Converts the byte stream data to a character stream according to the default encoding (eclise default is the GBK encoding table)

B:outputstreamwriter (OutputStream out,string charsetname): Converts the byte stream data to a character stream based on the specified encoding

A:outputstreamwriter (OutputStream out): Converts the byte stream data to a character stream based on the default encoding

1         New OutputStreamWriter (new  fileoutputstream (2                 "A.txt")); 3         Out.write ("China is our de motherland"); 4         5         Out.close ();

B:outputstreamwriter (OutputStream out,string charsetname): Converts the byte stream data to a character stream based on the specified encoding

1         New OutputStreamWriter (new  fileoutputstream (2                 //  designation UTF-83         //  Write Data 4         osw.write ("China is our Motherland"); 5 6         // Freeing Resources 7         Osw.close ();

Method of character output stream:

* public void write (int c): Write a character
b* public void Write (char[] cbuf): Write an array of characters
c* public void Write (char[] cbuf,int off,int len): Write part of a character array
d* public void Write (String str): Write a string
e* public void Write (string str,int off,int len): Write part of a string

First create a character output stream object

New OutputStreamWriter (new  fileoutputstream (                "Osw.txt"));

* public void write (int c): Write a character

Osw.write (' a ');//See A
Osw.write (97);//This is to see W, because this is a character stream, will automatically find the value of the character according to the Code table

b* public void Write (char[] cbuf): Write an array of characters

1 char[] chs = {' A ', ' B ', ' C ', ' d ', ' e '}; 2         Osw.write (CHS);

c* public void Write (char[] cbuf,int off,int len): Write part of a character array

1 osw.write (chs,1,3);

d* public void Write (String str): Write a string

Osw.write ("I love Sleeping");


e* public void Write (string str,int off,int len): Write part of a string

Osw.write ("I Love Sleeping", 2, 3);

Note: The character stream has a method similar to Xxx.close () that is different from the byte stream: Xxx.flush ();

So, what is the difference between close () and flush ():

A:close () Closes the stream object, but flushes the buffer first. After closing, the stream object cannot continue to be used.
B:flush () Simply flushes the buffer, and after it refreshes, the stream object can continue to be used. The Flush method is called when you want the data in the Bufferedoutstream to be output immediately.

That is, close () can only be used at the end of the stream, because once used, the stream object is gone.

and, flush () can be used on the way, because it is just a refresh function

Java 21-2 character output stream

Related Article

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.