Java. Io discussion of byte stream and byte stream

Source: Internet
Author: User
Io is divided into two streams

Byte stream inputstream outputstream

Stream reader writer

They are all abstract classes.

Implementation
Byte stream fileinputstream fileoutputstream
Stream filereader filewriter

You can use inputsteamreader outputstreamwriter to convert byte streams into bytes streams.

Convert to bufferdreader bufferedwriter they have Buffer

For example, reading a file from byte stream input to bytes Stream Input
Define a byte stream: Java code

  1. Fileinputstream =NewFileinputstream ("D:/text.txt"); // defines a byte stream pointing to D:/text. txt
  2. Inputstreamreader =NewInputstreamreader (fileinputstream );
  3. // Change byte Transfer to inputstreamreader
  4. Bufferedreader =NewBufferedreader (inputsteamreader );
  5. // Convert inputstreamreader to bufferedreader with Cache
Fileinputstream = new fileinputstream ("D:/text.txt"); // defines a point to D:/text. TXT byte stream inputstreamreader = new inputstreamreader (fileinputstream); // byte stream to bytes bufferedreader = new bufferedreader (inputsteamreader); // inputstreamreader to buffer bufferedreader

You can assign the read content to the character Java code.

  1. String Ss =NewString ();
  2. String S;
  3. While(S = bufferedreader. Readline ())! =Null){
  4. SS + = s;
  5. }
String ss = new String();String s;while((s = bufferedReader.readLine())!=null){ss += s;}

For example, write a file to output Java code from the byte stream to the bytes stream.

  1. Fileoutputstream =NewFileoutputstream ("D:/text.txt"); // defines
  2. Point to the D:/text. txt file
  3. Outputstreamwriter =NewOutputstreamwriter (fileoutputstream );
  4. Bufferedwriter =NewBufferedwriter (outputstreamwriter );
  5. Bufferedwriter. Write (s );
  6. Bufferedwriter. Close ();
  7. Outputstreamwriter. Close ();
  8. Fileoutputstream. Close ();
Related Article

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.