Java IO (iv)-conversion flow

Source: Internet
Author: User

Convert stream

InputStreamReader and OutputStreamWriter are used for conversion between byte data and character data.

InputStreamReader need and InputStream socket (convert InputStream to InputStreamReader)

OutputStreamWriter need and OutputStream socket (convert OutputStreamWriter to OutputStream)

A transform stream can specify its encoding collection at construction time, for example:

New InputStreamReader (system.in, "ios8859_1");

Example One,

     Public Static voidMain (string[] args) {Try{outputstreamwriter OSW=NewOutputStreamWriter (NewFileOutputStream ("D:\\bak\\char.txt")); Osw.write ("MISCALKJSDLASJDLAJSLKDALKSD");            System.out.println (Osw.getencoding ());                        Osw.close (); OSW=NewOutputStreamWriter (NewFileOutputStream ("D:\\bak\\char.txt",true), "Iso8859_1");//true, append Osw.write ("SADASDASDASDASD");            System.out.println (Osw.getencoding ());        Osw.close (); } Catch(Exception e) {e.printstacktrace (); }            }

Results:

utf8//system default encoding Iso8859_1

Example Two,

     Public Static voidMain (string[] args) {Try{InputStreamReader ISR=NewInputStreamReader (system.in); BufferedReader BR=NewBufferedReader (ISR); String s=NULL; S=Br.readline ();  while(s! =NULL) {                if("Exit". Equalsignorecase (s)) Break;                System.out.println (S.touppercase ()); S=Br.readline ();        } br.close (); } Catch(Exception e) {e.printstacktrace (); }    }

Results:

Add111add111exit

Java IO (iv)-conversion flow

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.