The bottom of the Java character Stream is also implemented using a byte stream, so the byte stream is converted to a character stream at a time. The conversion stream is mainly used tooutputstreamwriter: Convert byte output stream to character output stream formInputStreamReader: Converts the input byte stream into a character stream input form
1.outputstreamwriter (byte output stream---character output stream)
OutputStreamWriter is a bridge of bytes flowing to a character stream. If you do not specify a character set encoding, the decoding process uses the default character encoding of the platform
Construction Method:outputstreamwriter os = new OutputStreamWriter (outputstream out);//Constructs a default encoding setoutputstreamwriter os= New OutputStreamWriter (OutputStream out,string charsetname);// Constructs a OutputStreamWriter class that specifies the encoding set. Example code:
- public static void Main (string[] args) {
- try {
- Building a byte output stream
- OutputStream os=new FileOutputStream ("L:\\test.txt");
- String datastring= "Study hard and keep up!" ";
- Creating a character output stream from a byte output stream
- Writer w=new outputstreamwriter (OS);
- W.write (datastring);//Output to File
- Close the stream
- W.close ();
- Os.close ();
- } catch (FileNotFoundException e) {
- E.printstacktrace ();
- } catch (IOException e) {
- TODO auto-generated Catch block
- E.printstacktrace ();
- }
- }
Copy Code 2.inputstreamreader (byte input stream--character input stream)
InputStreamReader converts a byte stream to a character stream. is a bridge of bytes flowing to a character stream. If you do not specify a character set encoding, the decoding process uses the default character encoding for the platform.
Construction Method:InputStreamReader ISR = new InputStreamReader (InputStream in);//Constructs a default encodingInputStreamReader ISR = new InputStreamReader (InputStream in,string charsetname);//Constructs a InputStreamReader class that specifies the encoding set.
Example code:
- public static void Main (string[] args) {
- try {
- Building a byte input stream
- InputStream is=new FileInputStream ("L:\\test.txt");
- Building character input streams through byte input streams
- Reader r=new InputStreamReader (IS);
- By the way, use the buffer stream.
- BufferedReader br=new BufferedReader (R);
- Read a row of data
- System.out.println (Br.readline ());
- Close the stream
- Br.close ();
- R.close ();
- Is.close ();
- } catch (FileNotFoundException e) {
- E.printstacktrace ();
- } catch (IOException e) {
- E.printstacktrace ();
- }
- }
Copy CodeOperation Result:
"Conversion stream OutputStreamWriter and inputstreamreader for JAVA io streams"