Java Learning Day 21st (use of IO stream)

Source: Internet
Author: User

IO Stream Classification:
A: Flow Direction
Input stream Read Data
Output stream writes out data
B: Data type
BYTE stream
BYTE input stream
BYTE output stream
Character Stream
Character input stream
Character output stream
Attention:
A: If we do not specify according to what points, by default according to the data type points.
B: It is recommended to use a byte stream unless the file is opened in a Windows-brought notepad that we can read.

Structure

FileOutputStream Write Data

FileOutputStream fos = new FileOutputStream ("F.txt")

Fos.write ("Hello". GetBytes ());//Note that the write is a byte array

fos.close//This step must be, release resources, you can be automatically recovered by the system

Next about writing the data, using the Write method to write each write the data is re-written, sometimes we need to append the write, at this time can use, fileoutputstream fos = new FileOutputStream ("F.txt", True )

FileInputStream reading data

FileInputStream fis = new FileInputStream ("Fos.txt");

Mode 1 This way is not recognized in Chinese
int by = 0;
while ((By=fis.read ())!=-1) {
System.out.print ((char) by);
}

Mode 2 This way is more efficient than the previous one, as it is one time to read multiple data in the buffer
byte[] bys = new byte[1024];
int len = 0;
while ((Len=fis.read (bys))!=-1) {
System.out.print (New String (Bys,0,len));
}

With the comparison of the two methods, we can see that the input and output of the buffer is more efficient, so there is a byte class of the buffer in Java.

FileOutputStream fos = new FileOutputStream ("F.txt");

Bufferedoutputstream fos = new Bufferoutputstream (FOS);

Both can be combined

Bufferedoutputstream fos = new Bufferoutputstream (New FileOutputStream ("F.txt"));

The other methods used are the same.

Java Learning Day 21st (use of IO 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.