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
-
- PackageOrg. lxh. demo12.byteiodemo;
-
- ImportJava. Io. file;
-
- ImportJava. Io. fileoutputstream;
- ImportJava. Io. outputstream;
-
- Public ClassOutputstreamdemo05 {
-
- Public Static VoidMain (string [] ARGs)ThrowsException {// Exception thrown, not handled
- // Step 2: use the File class to find an object
-
- File F =NewFile ("D :"+ File. Separator +"Test.txt");// Declare the file object
-
- // Step 2: instantiate the parent class object through subclass
- Outputstream out =Null;
-
- // Prepare an output object
-
- Out =NewFileoutputstream (f );
-
- // Instantiate through object Polymorphism
-
- // Step 4: Perform the write operation
- String STR ="Hello world !!! ";
-
- // Prepare a string
-
- ByteB [] = Str. getbytes ();
-
- // Convert string to byte array
-
- Out. Write (B );
- // Output content
-
- // Step 4: Close the output stream
-
- // Out. Close ();
-
- // Not closed at this time
-
- }
-
- }
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
-
- PackageOrg. lxh. demo12.chariodemo;
-
- ImportJava. Io. file;
-
- ImportJava. Io. filewriter;
-
- ImportJava. Io. Writer;
- Public ClassWriterdemo03 {
-
- Public Static VoidMain (string [] ARGs)ThrowsException {// Exception thrown, not handled
- // Step 2: use the File class to find an object
-
- File F =NewFile ("D :"+ File. Separator +"Test.txt");// Declare the file object
-
- // Step 2: instantiate the parent class object through subclass
- Writer out =Null;
-
- // Prepare an output object
-
- Out =NewFilewriter (f );
-
- // Instantiate through object Polymorphism
- // Step 4: Perform the write operation
-
- String STR ="Hello world !!! ";
-
- // Prepare a string
-
- Out. Write (STR );
-
- // Output content
- // Step 4: Close the output stream
-
- // Out. Close ();
-
- // Not closed at this time
-
- }
-
- }
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
-
- PackageOrg. lxh. demo12.chariodemo;
-
- ImportJava. Io. file;
-
- ImportJava. Io. filewriter;
- ImportJava. Io. Writer;
-
- Public ClassWriterdemo04 {
-
- Public Static VoidMain (string [] ARGs)ThrowsException {// Exception throw not to handle
- // Step 2: use the File class to find an object
-
- File F =NewFile ("D :"+ File. Separator +"Test.txt");// Declare File
-
- Object
-
- // Step 2: instantiate the parent class object through subclass
- Writer out =Null;
-
- // Prepare an output object
-
- Out =NewFilewriter (f );
-
- // Instantiate through object Polymorphism
- // Step 4: Perform the write operation
-
- String STR ="Hello world !!! ";
-
- // Prepare a string
-
- Out. Write (STR );
-
- // Output content
- Out. Flush ();
-
- // Forcibly clear the content in the buffer
-
- // Step 4: Close the output stream
-
- // Out. Close ();
-
- // Not closed at this time
- }
-
- }
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.