Io stream demo--"J2SE"

Source: Internet
Author: User

Conversion Flow:

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

InputStreamReader needs to be inputstream with the socket.

OutputStreamWriter needs to be outputstream with the socket.

A transform stream can specify its encoding collection at construction time.

Import java.io.*;p ublic class Testtransform1{public static   void Main (string[] args) {Try{outputstreamwriter osw= New OutputStreamWriter (//character stream flows to the bridge of the byte stream new FileOutputStream ("D:\\bak\\char.txt"));//file output stream is used to write data to file or FileDescriptor output stream Osw.write ("Microsoft IBMSUNAPPLEHP"); System.out.println (Osw.getencoding ()); Osw.close (); osw=new OutputStreamWriter (New FileOutputStream ("d:\\bak\\ Char.txt ", True)," iso8859_1 "); Osw.write (" Microsoft IBMSUNAPPLEHP "); System.out.println (Osw.getencoding ());//Returns the name of the character encoding used by this stream. Osw.close ();} catch (IOException e) {e.printstacktrace ();//print out exceptions and deeper invocation information. }}}

Import java.io.*;p ublic class testtransform2{public static void Main (string[] args) {InputStreamReader isr=new InputStreamReader (system.in); BufferedReader br=new BufferedReader (ISR); String S=null;try{s=br.readline ();//Enter a number while (S!=null) {if (S.equalsignorecase ("Exit")) break;//this String with another String comparison, regardless of case System.out.println (S.touppercase ());//Use the default locale rule to convert all characters in this String to uppercase. S=br.readline ();//     reads a line of text. }br.close ();} catch (IOException e) {e.printstacktrace ();//print out all the call conditions and error messages}}}

Data flow:

DataInputStream and DataOutputStream inherit from InputStream and OutputStream respectively, and it belongs to the processing stream. Need to be nested on InputStream and outputstream types of node streams.

DataInputStream and DataOutputStream provide a way to access the machine-independent Java primitive type data.

Import java.io.*;p ublic class testdatastream{public static void Main (string[] args) {Bytearrayoutputstream baos=new Bytearrayoutputstream ();//This class implements an output stream in which the data is written to a byte array//Creates a new data output stream and writes the data to the specified underlying output stream. DataOutputStream dos=new DataOutputStream (BAOs); Try{dos.writedouble (Math.random ());d Os.writeboolean (true); Bytearrayinputstream bais=new Bytearrayinputstream (Baos.tobytearray ()); System.out.println (Bais.available ());D atainputstream dis=new datainputstream (Bais); System.out.println (Dis.readdouble ()); System.out.println (Dis.readboolean ());d os.close ();d is.close (); catch (IOException e) {e.printstacktrace ();}}}

Io stream demo--"J2SE"

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.