In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article will explain in detail the example analysis of the IO system in java. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.
The concept, classification and class diagram of 1.Java Io flow.
1.1 the concept of Java Io flow
The io of java is the basis of input and output, which can easily realize the input and output of data. In java, different input / output sources (keyboards, files, network connections, etc.) are abstractly expressed as "stream". Streaming allows java programs to access different input / output sources in the same way. Stram is ordered data from source to sink.
Note: java puts all traditional stream types under the java io package for input and output functions.
1.2 Classification of Io streams:
According to different classification methods, streams can be divided into different types. There are three commonly used categories:
1.2.1 according to the flow direction, it can be divided into input stream and output stream.
Input stream: data can only be read from it, not written to it.
Output stream: data can only be written to it, not read to it.
The input and output here involve a direction problem. For the data flow shown in figure 15.1, the data from memory to hard disk is usually called the output stream-that is, the input and output here are divided from the point of view of the memory in which the program is running.
Note: from the point of view of the hard disk, the data stream shown in figure 15.1 should be the input stream; but the division of the input / output stream is considered from the point of view of the memory in which the program is running, so the output stream is shown in figure 15.1. Not the input stream.
For the data flow shown in figure 15.2, the data flows from the server to the client through the network, in which case, the memory on the server side is responsible for outputting the data to the network, so the program on the server side uses the output stream; the memory on the client side is responsible for reading data from the network, so the program on the client side should use the input stream.
Note: the input stream of java is mainly based on InputStream and Reader, while the output stream is mainly based on outputStream and Writer. They are all abstract base classes and cannot be created directly.
1.2.2 according to the operation unit, it can be divided into byte stream and character stream.
The usage of byte stream and character stream is almost the same, except that the data unit operated by byte stream and character stream is different. The unit of byte stream operation is 8-bit bytes, and the character stream operates 16-bit characters.
The byte stream is mainly based on InputStream and outPutStream, while the character stream is mainly based on Reader and Writer.
1.2.3 according to the role of the flow, it is divided into node flow and processing flow.
A stream that can read / write data from / to a specific IO device (such as disk, network) is called a node stream. A node flow is also called a low-level flow. Figure 15.3 shows a schematic diagram of the node flow.
As you can see from figure 15.3, when using a node stream for input and output, the program connects directly to the actual data source and to the actual input / output node.
The processing flow is used to connect and encapsulate an existing stream and realize the read / write function of the data through the encapsulated stream. A processing flow is also called an advanced flow. Figure 15.4 shows a schematic diagram of the processing flow.
As you can see from figure 15.4, when using a processing stream for input / output, the program is not directly connected to the actual data source, not to the actual input and output nodes. One of the obvious benefits of using a processing flow is that as long as the same processing flow is used, the program can use exactly the same input / output code to access different data sources, as the node flow wrapped by the processing flow changes. the data source that the program actually accesses changes accordingly.
1.3 Analysis of the principle of flow and the classification table of commonly used streams:
1.3.1 Analysis of the principle of flow:
The java Io stream involves more than 40 classes, which look messy, but are actually very regular and closely related to each other. More than 40 classes of the Java Io stream are derived from the following four abstract class base classes.
InputStream/Reader: the base class of all input streams, the former is a byte input stream and the latter is a character input stream.
OutputStream/Writer: the base class of all output streams, the former being a byte output stream and the latter a character output stream.
For InputStream and Reader, they abstract the input device into a "water pipe" in which each "droplet" is arranged in turn, as shown in figure 15.5: as you can see from figure 15.5, the byte stream and the character stream are actually handled in a very similar way, except that they deal with different input / output units. The input stream uses an implicit recording pointer to indicate which "droplet" is currently being read, and the recording pointer moves from orientation whenever the program takes one or more "droplets" from InputStream or Reader; in addition, both InputStream and Reader provide some methods to control the movement of the recording pointer.
For OutputStream and Writer, they also abstract the output device into a "water pipe", except that there are no water droplets in the pipe, as shown in figure 15.6:
As shown in figure 15.6, when executing the output, the program is equivalent to putting "water droplets" into the water pipe of the output stream in turn, and the output stream also uses implicit pointers to identify the position where the current water droplets are about to be put, and the recording pointer automatically moves backward whenever the program outputs one or more water droplets to OutputStream or Writer.
Figure 15.5 and figure 15.6 show the basic conceptual model of java Io. In addition, Java's process flow model reflects the flexibility of Java input and output stream design. The function of processing flow is mainly reflected in the following two aspects.
Performance improvement: mainly by increasing the buffer to provide input and output efficiency.
Ease of operation: processing streams may provide a series of convenient ways to input and output large quantities of content at a time, rather than one or more "water droplets".
Processing streams can be "grafted" on top of any existing streams, which allows Java applications to access data streams from different input and output devices in a transparent manner using the same code. Figure 15.7 shows the model of the processing flow.
1.3.2 Classification table of streams commonly used in java input / output stream system
Classified byte input stream byte output stream character input stream character output stream abstract base class InputStreamOutputStreamReaderWriter access file FileInputStreamFileOutputStreamFileReaderFileWriter access array ByteArrayInputStreamByteArrayOutputStreamCharArrayReaderCharArrayWriter access pipeline PipedInputStreamPipedOutputStreamPipedReaderPipedWriter access string
StringReaderStringWriter buffer stream BufferedInputStreamBufferedOutputStreamBufferedReaderBufferedWriter conversion stream
InputStreamReaderOutputStreamWriter object stream ObjectInputStreamObjectOutputStream
Abstract base class FilterInputStreamFilterOutputStreamFilterReaderFilterWriter print stream
PrintStream
PrintWriter pushes back the input stream PushbackInputStream
PushbackReader
Special stream DataInputStreamDataOutputStream
Note: the class marked in bold in the table represents the node flow and must be directly associated with the specified physical node: the class marked in italics represents the abstract base class and cannot directly create an instance.
two。 Common usage of io streams
The following is an overview of the common Io stream features and usage, only clear about the characteristics and methods of each Io stream. In order to correctly select the corresponding IO stream for development in the face of different requirements.
2.1 Base class of Io system (InputStream/Reader,OutputStream/Writer)
The operation mode of byte stream and character stream is basically the same, but the data unit of operation is different-the operation unit of byte stream is byte, and the operation unit of character stream is character. So the byte stream and the character stream are grouped together.
InputStream and Reader are the abstract base classes of all input streams, and they cannot create instances to execute input by themselves, but they will become templates for all input streams, so their methods are available to all input streams.
The following three methods are included in InputStream.
Int read (); reads a single byte from the input stream (equivalent to taking a drop of water from the pipe shown in figure 15.5) and returns the read byte data (the byte data can be converted directly to the int type).
Int read (byte [] b) reads up to b.length bytes of data from the input stream and stores it in a byte array b, returning the actual number of bytes read.
Int read (byte [] b drawing int off,int len); reads up to len bytes from the input stream and stores them in array b. When it is placed in array b, it does not start from the beginning of the array, but from the off position and returns the actual number of bytes read.
The following three methods are included in Reader.
Int read (); reads a single character from the input stream (equivalent to taking a drop of water from the water pipe shown in figure 15.5) and returns the read character data (byte data can be directly converted to int type).
Int read (char [] b) reads up to b.length characters from the input stream and stores them in a byte array b, returning the actual number of characters read.
Int read (char [] b char int off,int len); read up to the data of len characters from the input stream and store it in array b. When you put it into array b, you don't start from the beginning of the array, but from the off position and return the actual number of characters read.
Comparing the methods provided by InputStream and Reader, it is not difficult to find that the functions of the two base classes are basically the same. Both InputStream and Reader abstract the input data into the water pipe shown in figure 15.5, so the program can read one "water drop" at a time through the read () method, or multiple "water droplets" through the read (char [] chuf) or read (byte [] b) method.
When using an array as a parameter in the read () method, we can understand that we use a "bamboo tube" to fetch water from the water pipe shown in figure 15.5.The parameters of the read (char [] cbuf) method shown in figure 15.8 can be understood as a "bamboo tube". Each time the program calls the input stream read (char [] cbuf) or read (byte [] b) method, it is equivalent to using a "bamboo tube" to extract a tube of "water droplets" from the input stream. After the program gets the "water droplets" in the "bamboo tube", it can be converted into the corresponding data.
The program repeats this "water collection" process many times until the end. How does the program judge that the water is taken at the end? Until the read (char [] chuf) or read (byte [] b) method returns-1, it indicates that the end point of the input stream has been reached.
Some methods for moving pointers provided by InputStream and Reader:
Void mark (int readAheadLimit); record a mark (mark) at the current position of the record pointer.
Boolean markSupported (); determines whether the input stream supports the mark () operation, that is, whether the record tag is supported.
Void reset (); reposition the record pointer for this stream to the location of the previous record mark (mark).
Long skip (long n); the record pointer moves forward n bytes per character.
OutputStream and Writer:
The usage of OutputStream and Writer is also very similar, using the model shown in figure 15.6 to execute input, and both streams provide the following three methods:
Void write (int c); outputs the specified byte / character to the output stream, where c represents either a byte or a character.
Void write (byte [] / char [] buf); outputs data from the byte array / character array to the specified output stream.
Void write (byte [] / char [] buf, int off,int len); outputs bytes / characters of length len from the off position in the byte array / character array to the output stream.
Because character streams operate directly on characters, Writer can use strings instead of character arrays, that is, taking String objects as parameters. Writer also contains the following two methods.
Void write (String str); outputs the characters contained in the str string to the specified output stream.
Void write (String str, int off, int len); outputs len characters in the str string starting from the off position to the specified output stream.
2.2 use of the base class file stream of the Io system (FileInputStream/FileReader, FileOutputStream/FileWriter)
As mentioned earlier, both InputStream and Reader are abstract classes and cannot create instances themselves, but they each have an input stream for reading files: FileInputStream and FileReader, both of which are node streams-- which are directly associated with the specified file. The following program demonstrates the use of FileInputStream and FileReader.
Use FileInputStream to read the file:
Public class MyClass {public static void main (String [] args) throws IOException {FileInputStream fis=null;try {/ / create byte input stream fis=new FileInputStream ("E:\\ learnproject\\ Iotest\\ lib\\ src\\ main\\ java\\ com\ Test.txt"); / / create a bamboo tube byte [] b=new byte [1024] with a length of 1024; / / the actual number of bytes used for storage / / the process of repeatedly fetching water using a loop while ((hasRead=fis.read (b)) > 0) {/ / take out the water droplets (bytes) from the bamboo tube and convert the byte array into a string for output System.out.print (new String);}} catch (IOException e) {e.printStackTrace ();} finally {fis.close ();}
Note: the above program finally uses fis.close () to close the file input stream, like JDBC programming, the open file IO resources do not belong to memory resources, garbage collection mechanism can not recover the resources, so it should be displayed to close the open IO resources. Java 7 rewrites all IO resource classes, all of which implement the AntoCloseable interface, so you can close these Io flows by automatically closing the resource's try statement.
Use FileReader to read the file:
Public class FileReaderTest {public static void main (String [] args) throws IOException {FileReader fis=null;try {/ / create byte input stream fis=new FileReader ("E:\\ learnproject\\ Iotest\\ lib\\ src\\ main\\ java\\ com\ Test.txt"); / / create a bamboo tube char [] b=new char [1024] with a length of 1024; / / the actual number of bytes used for storage / / the process of repeatedly fetching water using a loop while ((hasRead=fis.read (b)) > 0) {/ / take out the water droplets (bytes) from the bamboo tube and convert the byte array into a string for output System.out.print (new String);}} catch (IOException e) {e.printStackTrace ();} finally {fis.close ();}
You can see that there is no difference between using FileInputStream and FileReader to read and write files, except that the operation units are different and.
FileOutputStream/FileWriter is the file output stream in Io, and the usage of these two classes is described below.
The usage of FileOutputStream:
Public class FileOutputStreamTest {public static void main (String [] args) throws IOException {FileInputStream fis=null;FileOutputStream fos=null;try {/ / create byte input stream fis=new FileInputStream ("E:\\ learnproject\\ Iotest\\ lib\\ src\\ main\\ java\\ com\ Test.txt"); / / create byte output stream fos=new FileOutputStream ("E:\\ learnproject\\ Iotest\\ lib\\ src\ main\\ java\\ com\ newTest.txt"); byte [] b=new byte [1024]; int hasRead=0 / / Loop to pull data while ((hasRead=fis.read (b)) > 0) {/ / each time it is read, that is, write to the file input stream, write as much as you read. Fos.write;} catch (IOException e) {e.printStackTrace ();} finally {fis.close (); fos.close ();}
When you run the program, you can see that there is one more file in the directory specified by the output stream: newTest.txt, which has exactly the same contents as the Test.txt file. The way FileWriter is used is basically similar to that of FileOutputStream, which has been mentioned here.
Note: when using java's io stream to perform output, don't forget to close the output stream. In addition to ensuring that the physical resources of the stream are reclaimed, it may also be possible to flush the data in the output stream buffer into the physical node (because the flush () method of the output stream is automatically executed before the close () method is executed). Many output streams of java provide caching function by default. In fact, there is no need to deliberately remember which streams have caching function and which streams do not. We can only close all output streams normally to ensure that the program is normal.
Use of buffered streams (BufferedInputStream/BufferedReader, BufferedOutputStream/BufferedWriter):
The following describes the usage of the byte cache stream (not if the use of the character cache stream is consistent with the byte cache stream):
Public class BufferedStreamTest {public static void main (String [] args) throws IOException {FileInputStream fis=null;FileOutputStream fos=null;BufferedInputStream bis=null;BufferedOutputStream bos=null;try {/ / create byte input stream fis=new FileInputStream ("E:\\ learnproject\\ Iotest\\ lib\\ src\\ main\\ java\\ com\ Test.txt"); / / create byte output stream fos=new FileOutputStream ("E:\ learnproject\\ Iotest\ lib\ src\ main\ java\\ com\ newTest.txt") / / create byte cache input stream bis=new BufferedInputStream (fis); / / create byte cache output stream bos=new BufferedOutputStream (fos); byte [] b=new byte [1024]; int hasRead=0;// loop reads data from cache stream while ((hasRead=bis.read (b)) > 0) {/ / write data to cache stream, read how many bos.write (bm0hasRead);}} catch (IOException e) {e.printStackTrace ();} finally {bis.close (); bos.close () }}}
You can see that using a byte cache stream to read and write data is no different from a file stream (FileInputStream,FileOutputStream), except that the processing stream is tied to the file stream for reading and writing. The principle of cache flow is described in the next section.
We used the cache stream and file stream in the above code, but we only closed the cache stream. It is important to note that when we use the processing flow to nest the use on the node flow, we only need to turn off the top-level processing. Java automatically shuts down the underlying node flow for us.
2.3Use the conversion stream (InputStreamReader/OutputStreamWriter):
Let's take getting keyboard input as an example to introduce the use of the conversion stream. Java uses System.in to represent input. That is, keyboard input, but this standard input stream is an instance of InputStream class, which is not easy to use, and the keyboard input content is all text content, so you can use InputStreamReader to package it as BufferedReader, and use the readLine () method of BufferedReader to read one line at a time, as shown in the following code:
Public class InputStreamReaderTest {public static void main (String [] args) throws IOException {try {/ / converts the System.in object to the Reader object InputStreamReader reader=new InputStreamReader (System.in); / / wraps the normal Reader into BufferedReaderBufferedReader bufferedReader=new BufferedReader (reader); String buffer=null;while ((buffer=bufferedReader.readLine ())! = null) {/ / if the read string is "exit", the program exits if (buffer.equals ("exit")) {System.exit (1) } / / print the read content System.out.print ("input content:" + buffer);}} catch (IOException e) {e.printStackTrace ();} finally {}}
The above program wraps System.in as a BufferedReader,BufferedReader stream with caching, which can read one line of text at a time-marked by a newline character, and if it doesn't read the newline character, the program is blocked. Wait until you read the newline character. This feature can be found by running the above program. When we execute the input in the console, the program will print out what we have just typed only if we press the enter key.
2.4 use of object streams (ObjectInputStream/ObjectOutputStream):
Write object:
Public static void writeObject () {OutputStream outputStream=null;BufferedOutputStream buf=null;ObjectOutputStream obj=null;try {/ / serialization file output stream outputStream=new FileOutputStream ("E:\\ learnproject\\ Iotest\\ lib\\ src\\ main\\ java\\ com\\ myfile.tmp"); / / build buffer stream buf=new BufferedOutputStream (outputStream); / / build character output object stream obj=new ObjectOutputStream (buf); / / serialize data is written to obj.writeObject (new Person ("A", 21)) / / Person object / / close stream obj.close ();} catch (FileNotFoundException e) {e.printStackTrace ();} catch (IOException e) {e.printStackTrace ();}}
Read object:
/ * read object * / public static void readObject () throws IOException {try {InputStream inputStream=new FileInputStream ("E:\\ learnproject\\ Iotest\\ lib\\ src\\ main\\ java\\ com\\ myfile.tmp"); / / build buffer stream BufferedInputStream buf=new BufferedInputStream (inputStream); / / build object stream ObjectInputStream obj=new ObjectInputStream (buf) for character input; Person tempPerson= (Person) obj.readObject (); System.out.println ("Person object is" + tempPerson); / / close stream obj.close () Buf.close (); inputStream.close ();} catch (FileNotFoundException e) {e.printStackTrace ();} catch (IOException e) {e.printStackTrace ();} catch (ClassNotFoundException e) {e.printStackTrace ();}}
Some considerations for using object flow
1. The read order and write order must be the same, otherwise there will be a read error.
two。 If you precede an object property with the transient keyword, the object's properties are not serialized.
4. What is NIO and how is it different from traditional Io?
When we use InputStream to read data from the input stream, if no valid data is read, the program blocks the execution of the thread here. In fact, the traditional input and output streams are blocking input and output. Not only that, the traditional input stream and output stream are handled by byte movement (even if we don't deal with byte stream directly, the underlying implementation still depends on byte processing), that is, stream-oriented input and output can only handle one byte at a time, so stream-oriented input and output systems are usually inefficient.
Starting with JDk1.4, java provides a series of new functions for improved input and output processing, which are collectively referred to as the new IO (NIO). A number of new classes have been added to handle input and output, which are placed under the java.nio package and its subpackages, and many of the classes of the original io have been rewritten based on NIO. Added the function that satisfies the NIO.
NIO uses memory-mapped objects to handle input and output, and NIO maps files or an area of files to memory so that files can be accessed as if they were memory. Input / output in this way is much faster than traditional input and output.
After JDk1.4 uses NIO to rewrite the traditional Io, the reading and writing speed of the traditional Io is not much different from that of NIO.
5. Correct use of Io streams in development
After understanding the overall class structure of java io and the characteristics of each class, we can flexibly use different Io streams during the development process. Here are two principles I have sorted out:
If we are manipulating binaries, then we use byte streams, and if we are manipulating text files, then we use character streams.
Using processing streams as much as possible will make our code more flexible and reusable.
This is the end of this article on "sample Analysis of IO system in java". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.