IO stream-----(byte stream)

Source: Internet
Author: User

The stream ending with stream is called a universal flow (byte stream), otherwise a character stream

Overview of 1.java streams:

A file is usually made up of a string of bytes or characters, and the sequence of bytes composing the file is called a byte stream, and the sequence of characters that make up the file is called a character stream.

In 2.Java, the direction of the flow can be divided into: input stream and output stream

Input stream: An input stream is the process of loading data from a file or other input device into memory

Output stream: The output stream is to save the in-memory data to a file or other input device

?????? 3. If the file is made up of bytes or characters, then loading the file into memory or outputting the file to a file requires support from both the input stream and the output stream, then the input and output streams are divided into two types in the Java language:

byte input stream, byte output stream character input stream, character output stream

3.1 InputStream (Byte input stream): InputStream is an abstract class, all implements the InputStream class is the byte input stream, the main understanding suddenly class can:

Example:

public class test{

public static void Main (string[] args) throws exception{

Create an input stream

InputStream in=new FileInputStream ("C:/a.txt");

Define a temporary variable

int temp=0;

while ((Temp=in.read ())!=-1) {

System.out.println ((char) temp);

}

}

}

3.2 outputstream (Byte output stream): OutputStream is an abstract class, all implementations of this class are byte output streams

If the close () method is called, the Flush () method is called automatically, but it is recommended to manually display the flush () method

Example:

public class Test {

public static void Main (string[] args) {
Create an output byte stream
OutputStream out = null;
try {
out = new FileOutputStream ("C:/b.txt");
Out.write (123);
Flush or close must be called
Out.flush ();
} catch (Exception e) {
E.printstacktrace ();
}finally {
try {
Out.close ();
} catch (IOException e) {
E.printstacktrace ();
}
}
SYSTEM.OUT.PRINTLN ("Success");
}
}

jdk1.8 new feature auto-close:

Example:

public class test{

public static void Main (string[] args) {

Try (Outputsreeam out =new fileoutputstream ("C:/c.txt");) {

Create an output byte stream

Out.write (1);

The flush method must be called

Out.flush ();

}catch (Exception e) {

E.printstacktrace ();

}

SYSTEM.OUT.PRINTLN (Success);

}

}

IO stream-----(byte stream)

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.