Java basic review: IO stream

Source: Internet
Author: User

Input stream: file ---> Program

Output stream: Program ----> File

 

Four abstract streams in Java:

Input stream: InputStream (byte input stream) Reader (character input stream)

Output stream: OutputStream (byte output stream) Writer (character output stream)

Differences:

Text files can only be operated by words for images, audio and video files.

 

Procedure:

1) obtain the source or target of the IO operation

2) channel for IO operations

3) perform IO operations

4) Disable IO

 

OutputStream:

Import java. io. file; import java. io. fileOutputStream; import java. io. IOException; import java. io. outputStream; public class OuputStreamDemo {public static void main (String [] args) throws IOException {// 1. find the target File file = new File ("d:/Test/test.txt") of the stream; // 2. create an output stream pipeline and connect to the target OutputStream OS = new FileOutputStream (file); // 3. the input output // byte output stream can only write data one by one directly. write ('\ n'); for (int I = 0; I <20; I ++) {OS. write (I) ;}string str = "hello, world"; byte [] B = str. getBytes (); OS. write (B); // 4. disable operating OS. close ();}}

InputStream:

Import java. io. file; import java. io. fileInputStream; import java. io. IOException; import java. io. inputStream; public class InputStreamDemo {public static void main (String [] args) throws IOException {File file = new File ("D:/Test/cmd.txt "); inputStream is = new FileInputStream (file); byte [] buff = new byte [1024]; int len = 0; while (len = is. read (buff ))! =-1) {String str = new String (buff, 0, len); System. out. println (str);} // garbled characters are displayed in Chinese. The Chinese character is double byte, and the English character is single byte is. close ();}}

 

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.