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