Difference between Java byte stream and hidden stream, java byte hidden stream

Source: Internet
Author: User

Difference between Java byte stream and hidden stream, java byte hidden stream

The use of byte streams is very similar to that of byte streams. Are there other differences between the two in addition to the differences in the operation code?

In fact, the byte stream itself does not use a buffer (memory) during operations, but directly operates on the file itself. The swap stream uses a buffer during operations and operates on the file through the buffer, as shown in 12-6.

The following compares two write operations, but the output stream is not closed after the operations of the byte stream and the upload stream are completed.

Example: use byte stream not to close the execution

Java code
  1. Package org. lxh. demo12.byteiodemo;
  2. Import java. io. File;
  3. Import java. io. FileOutputStream;
  4. Import java. io. OutputStream;
  5. Public class OutputStreamDemo05 {
  6. Public static void main (String [] args) throws Exception {// thrown Exception, not processed
  7. // Step 2: use the File class to find an object
  8. File f = new File ("d:" + File. separator + "test.txt"); // declare the File object
  9. // Step 2: instantiate the parent class object through subclass
  10. OutputStream out = null;
  11. // Prepare an output object
  12. Out = new FileOutputStream (f );
  13. // Instantiate through object Polymorphism
  14. // Step 4: Perform the write operation
  15. String str = "Hello World !!! ";
  16. // Prepare a string
  17. Byte B [] = str. getBytes ();
  18. // Convert string to byte array
  19. Out. write (B );
  20. // Output content
  21. // Step 4: Close the output stream
  22. // Out. close ();
  23. // Not closed at this time
  24. }
  25. }


Program running result:

In this case, the byte stream operation is not disabled, but the output content still exists in the file, proving that the byte stream directly operates on the file itself. Next we will continue to use the progress stream and observe the effect.

Example: do not close the execution using the livestream

Java code
  1. Package org. lxh. demo12.chariodemo;
  2. Import java. io. File;
  3. Import java. io. FileWriter;
  4. Import java. io. Writer;
  5. Public class WriterDemo03 {
  6. Public static void main (String [] args) throws Exception {// thrown Exception, not processed
  7. // Step 2: use the File class to find an object
  8. File f = new File ("d:" + File. separator + "test.txt"); // declare the File object
  9. // Step 2: instantiate the parent class object through subclass
  10. Writer out = null;
  11. // Prepare an output object
  12. Out = new FileWriter (f );
  13. // Instantiate through object Polymorphism
  14. // Step 4: Perform the write operation
  15. String str = "Hello World !!! ";
  16. // Prepare a string
  17. Out. write (str );
  18. // Output content
  19. // Step 4: Close the output stream
  20. // Out. close ();
  21. // Not closed at this time
  22. }
  23. }


Program running result:




After the program runs, it will find that there is no content in the file. This is because the buffer zone is used during the merge stream operation, and the buffer content is forcibly output when the merge stream is disabled, but if the program is not closed, the content in the buffer zone cannot be output, so it is concluded that the swap stream uses a buffer zone, while the byte stream does not use a buffer zone.

Q: What is a buffer zone?

What is the buffer zone? What role does it play?

A: The buffer zone can be simply understood as a memory area.

The buffer can be understood as a special memory.

In some cases, if a program frequently operates a resource (such as a file or database), the performance will be very low. To improve the performance, you can temporarily read part of the data into a block area of the memory, and then directly read the data from this area, because the read speed will be faster, which can improve the program performance.

In the batch stream operation, all characters are formed in the memory. Before the output, all the content is temporarily saved in the memory, so the buffer zone is used for temporary data storage.

You can use the flush () method in the Writer class to output all the contents of the producer stream without shutting down.

Example: Forced buffer clearing

Java code
  1. Package org. lxh. demo12.chariodemo;
  2. Import java. io. File;
  3. Import java. io. FileWriter;
  4. Import java. io. Writer;
  5. Public class WriterDemo04 {
  6. Public static void main (String [] args) throws Exception {// The Exception is thrown and not processed.
  7. // Step 2: use the File class to find an object
  8. File f = new File ("d:" + File. separator + "test.txt"); // declare File
  9. Object
  10. // Step 2: instantiate the parent class object through subclass
  11. Writer out = null;
  12. // Prepare an output object
  13. Out = new FileWriter (f );
  14. // Instantiate through object Polymorphism
  15. // Step 4: Perform the write operation
  16. String str = "Hello World !!! ";
  17. // Prepare a string
  18. Out. write (str );
  19. // Output content
  20. Out. flush ();
  21. // Forcibly clear the content in the buffer
  22. // Step 4: Close the output stream
  23. // Out. close ();
  24. // Not closed at this time
  25. }
  26. }



Program running result:

At this point, the content already exists in the file, further proving that the content is saved in the buffer zone. This should be paid special attention to in future development.

Q: Is the byte stream good or the byte stream good?

After learning the basic operations of the byte stream and the primary stream, I have understood the differences in the operation process. Are you sure you want to use the byte stream or the primary stream in development?

A: It is better to use byte streams.

Before answering this question, I would like to explain to you the concept that all files are stored in bytes on the hard disk or during transmission, including images, characters are formed only in the memory. Therefore, byte streams are widely used during development.


The main difference between byte stream and byte stream is their processing method.

Stream classification:
1. Java byte stream
InputStream is the ancestor of all byte input streams, while OutputStream is the ancestor of all byte output streams.
2. Java producer stream
Reader is the ancestor of all input strings, while writer is the ancestor of all output strings.
InputStream, OutputStream, Reader, and writer are all abstract classes. So new cannot be used directly.



Byte streams are the most basic. All InputStream and OutputStream subclasses are used to process binary data. They are processed by byte.
However, in reality, a lot of data is text, and the concept of character stream is proposed. It is processed by the encode of the virtual machine, that is, Character Set conversion.
The two are associated through InputStreamReader and OutputStreamWriter. In fact, they are associated through byte [] and String.
In actual development, the Chinese character problems are actually caused by inconsistent conversion between the primary stream and the word throttling.

When the bytes are converted to bytes streams, the bytes [] is actually converted to strings,
Public String (byte bytes [], String charsetName)
There is a key parameter character set encoding, which is usually omitted. Then the system uses the lang of the operating system.
When the bytes stream is actually converted into byte,
Byte [] String. getBytes (String charsetName)
The same is true.

As for java. io, there are many other streams, mainly to improve performance and ease of use,

Such as BufferedInputStream and PipedInputStream.

Notes for learning Java !!!

If you have any questions or want to obtain learning resources during the learning process, join the Java learning exchange group with the group number 618528494.Let's learn Java together!

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.