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