What is the case analysis in Java caused by using byte stream and character stream not to close the flow?

Source: Internet
Author: User

Package COM.HEPHEC;

Import Java.io.File;

Import Java.io.FileOutputStream;

Import Java.io.OutputStream;


public class outputstreamtest{

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

OutputStream out=new FileOutputStream (New File ("E:" +file.separator+ "test.txt"));

String str= "Zhangsan";

Byte[] B=str.getbytes ();//Convert a string to a byte array

Out.write (b);

Out.close ();//Not closed stream

}

}

Result:zhangsan

Although the byte stream is not closed, there is still content output in the file, which proves that the byte stream is directly manipulating the file itself.

Package COM.HEPHEC;

Import Java.io.File;

Import Java.io.FileWriter;

Import Java.io.Writer;


public class outputstreamtest{

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

Writer writer=new FileWriter (New File ("E:" +file.separator+ "test.txt"));

String str= "Zhangsan";

Byte[] B=str.getbytes ();//Convert a string to a byte array

Writer.write (b);

Writer.close ();//Not closed stream

}

}

Result

//When the program runs, nothing is found because the character stream operation uses a buffer, and when the character stream is closed, the contents of the buffer are forced to be output, but the contents of the buffer cannot be output if the program is not closed .

What is the case analysis in Java caused by using byte stream and character stream not to close the flow?

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.