Differences between byte stream and byte stream

Source: Internet
Author: User

Byte streams are very similar to bytes streams.In addition to operationsCodeIn addition,Are there other differences?
In fact, the byte stream itself does notThe buffer zone (memory) is used for direct operations on the file itself, while the swap stream uses a buffer zone during operations and operates through the buffer zone.Composition,.

The following compares the write operations on two files, but does not close the operation after the byte stream and the upload stream are completed.Output stream.
Example: use byte stream not to close the execution

Java code
  1. PackageOrg. lxh. demo12.byteiodemo;
  2. ImportJava. Io. file;
  3. ImportJava. Io. fileoutputstream;
  4. ImportJava. Io. outputstream;
  5. Public ClassOutputstreamdemo05 {
  6. Public Static VoidMain (string [] ARGs)ThrowsException {// Exception thrown, not handled 
  7. // Step 2: use the File class to find an object 
  8. File F =NewFile ("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 =NewFileoutputstream (f );
  13. // Instantiate through object Polymorphism 
  14. // Step 4: Perform the write operation 
  15. String STR ="Hello world !!! ";
  16. // Prepare a string 
  17. ByteB [] = 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. }


ProgramRunning 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. PackageOrg. lxh. demo12.chariodemo;
  2. ImportJava. Io. file;
  3. ImportJava. Io. filewriter;
  4. ImportJava. Io. Writer;
  5. Public ClassWriterdemo03 {
  6. Public Static VoidMain (string [] ARGs)ThrowsException {// Exception thrown, not handled 
  7. // Step 2: use the File class to find an object 
  8. File F =NewFile ("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 =NewFilewriter (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, because the buffer zone is used during the batch stream operation, and When closing the primary stream, it will forcibly output the content in the buffer. However, if the program is not closed, the content in the buffer cannot be output. Therefore, it is concluded that the primary stream uses a buffer, the byte stream does not use a buffer.

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. PackageOrg. lxh. demo12.chariodemo;
  2. ImportJava. Io. file;
  3. ImportJava. Io. filewriter;
  4. ImportJava. Io. Writer;
  5. Public ClassWriterdemo04 {
  6. Public Static VoidMain (string [] ARGs)ThrowsException {// Exception throw not to handle 
  7. // Step 2: use the File class to find an object 
  8. File F =NewFile ("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 =NewFilewriter (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.

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.