Java notes: IO streams

1. IO flow understanding

I in the IO stream is the abbreviation of the word input, It means input or read. O is the abbreviation of the word output, which means output or write. Input, output or read-write are relative to memory. Input is to read data from the hard disk into memory, and output is to write data in memory to the hard disk. IO stream is the data stream during input and output (the pipeline between the memory and the hard disk), the input or output of IO can also be regarded as the reading and writing of files, because the data in the hard disk is actually stored in the form of files. The reading and writing of files on the hard disk corresponds to the input and output of data in the memory.

2. Read mode of stream

By byte: only one byte (8 binary bits) is read at a time. This reading method is characterized by reading all types of files, including ordinary text, pictures, sounds, etc.

By character: only one character can be read at a time. The characteristic of this reading method is that it can only read ordinary text. It can automatically recognize the number of bytes occupied by a character and then read it. For example, a letter in English only occupies one byte, while a character in Chinese will occupy two bytes. For example, "a ah", it only needs to be read twice by character, but it will be read three times by byte, because there are three bytes.

3. File class

"Java. Io. File" class has nothing to do with the operation of file stream, that is, it cannot read or write files. File class is an abstract representation of file and directory path names, that is, a file is a file object and a directory is also a file object. Its construction method only needs to pass in the path name of a file or directory. Note that for the path separator, Windows and Linux use '\' and '/' respectively, but the path passed in file can use '/' uniformly, which can also be recognized in windows.

Common methods in file:

General method for obtaining a file and file path:

Note: this method can obtain the absolute path or file stream of a file, but it should be noted that the premise of using this method is that the file is under the class path, and the directory structure of the idea tool is under the "[module_name] / SRC" path, that is, the parameter in getresource is the relative path under a SRC folder.

4. IO flow in Java

The class packages of IO streams in Java are all in "Java. IO", which can be divided into four categories: "Java. Io. InputStream", "Java. Io. OutputStream", "Java. Io. Reader" and "Java. Io. Writer". The first two streams end in byte stream and the last two are read and written in character stream, If you want to know whether a subclass reads file data in byte stream or character stream, you can use the ending word of the class name to quickly distinguish. Those ending in stream are read and written in byte stream, while those ending in reader or writer are read and written in character stream. It should be noted that, Classes like "xxxstreamreader" also read and write by looking at the ending word, that is, it is also a character stream.

All IO streams in java have a close method. After using the IO stream, be sure to use the close method to close the pipeline. For output streams, there is a flush method, which means that the data in the pipeline (buffer) is forcibly output and emptied. Its function is to empty the pipeline. Remember to use the flush method to refresh after manual output, otherwise the output data may be incomplete, that is, data loss.

5. File flow

FileInputStream (commonly used, focus on mastering): read the file in the form of byte stream. The construction method can pass in a file path or a file object representing the file.

Common methods are:

Use example:

FileOutpuStream (commonly used, focus on mastering): write the byte content into the file. In the construction method, you can also pass in a file path or a file object representing the file. At the same time, you can also specify that the second parameter is true. When only one file path or file object is passed in, if the file already exists, the file content will be cleared. If the file does not exist, the file will be automatically created; or Specify the second parameter as true, which means that if the file already exists, the file will not be emptied, but data will be appended to the end of the file.

Common methods are:

Use example:

FileReader: compared with FileInputStream, it is similar in use. It only needs to replace byte array with char array.

Filewriter: compared with fileoutpustream, it is similar in use. It only needs to replace byte array with char array. In addition, due to the particularity of string, the write method of filewriter can also directly write a string to the file.

6. Buffer flow

A buffer stream represents a byte stream or a character stream with a buffer. It is characterized in that there is no need to customize byte array or char array when using these streams.

Wrapper flow and node flow: the use of buffer flow involves some concepts. When another flow is required in the construction method of one flow, the incoming flow is called node flow, and the external flow responsible for wrapping the node flow is called wrapper flow or processing flow. When closing the wrapper flow, you only need to call the close () method of the outermost wrapper flow, and the node flow inside will be closed automatically. Similarly, for the output stream, you only need to call the flush () method of the outermost wrapper stream.

Bufferedinputstream: it is a wrapper stream. A node stream of InputStream type (byte type) needs to be passed in the construction method.

Bufferedoutputstream: it is a wrapper stream. A node stream of OutputStream type (byte type) needs to be passed in the construction method.

BufferedReader: it is a wrapper stream. A reader type (character type) node stream needs to be passed in the constructor.

Bufferedwriter: a wrapper stream. A node stream of writer type (character type) needs to be passed in the constructor.

Use example:

7. Data flow

The characteristic of this stream is that it will write the data and data type into the file together. Different data types can be used each time the file is written, and it needs to be read out according to the data type in the order of writing.

Datainputstream: reads a specific type of data from a file according to specified rules.

Dataoutputstream: writes a specific type of data to a file according to a specified rule.

8. Standard output stream

The standard output does not need to be closed manually.

Printstream: Standard byte output stream, which is output to the console by default. System. Out is actually a printstream standard output byte stream. You can use system Setout sets the standard output stream output to the specified file and no longer outputs to the console by default.

Printwriter: standard character output stream. It is output to the console by default. You can also use system Setout changes the default output direction.

Use example:

9. Object stream (serialization and deserialization)

Serialization and deserialization: the use of object streams involves two concepts, serialization and deserialization, Serialization refers to storing Java objects in files and saving the state of Java objects in the form of files (deserialize) is the deserialization process, that is, the process of recovering the data in the file into memory and into Java objects. The serialization process needs to use objectoutputstream, while the deserialization process needs to use objectinputstream.

ObjectOutputStream: by passing in a FileOutputStream object in the construction method, and then calling the writeObject method, you can save a Java object that needs serialization to the specified file. If you need to serialize multiple objects, you can put these objects into the ArrayList collection, and finally pass this collection into the writeobject method. However, it should be noted that the ArrayList collection also implements the serializable interface, that is, The collection used and the elements in the collection need to implement the serializable interface to serialize. For serializing multiple objects, it is recommended to directly use the collection method. If you write one object at a time by calling writeobject multiple times, this method is not allowed.

ObjectInputStream: by passing in a FileInputStream object in the construction method, and then calling the readObject method, you can read a Java object (ordinary Java object or collection object) into the memory from the anti serialization result of the specified file.

Serializable interface: objects participating in serialization and deserialization need to implement the serializable interface, but note that this interface does not have any methods, It just acts as an identification (this interface is also called flag interface), which tells the JVM that this kind of object may be serialized. With this flag interface, the JVM will automatically generate a serialized version number for this Java object during compilation. This serialized version number is used to identify the class definition in the current source code and the previous serialization operation during deserialization Whether the class definitions are consistent. If they are inconsistent, an error will be reported and deserialization cannot be performed, that is, the class definition of the deserialized object in the current source code has been modified, Unable to deserialize (when deserialization comes back, you also need a variable corresponding to class type to receive it. If they are different, they will naturally fail to pass). Therefore, it can be seen that this serialization version number is essentially used to distinguish the classification and judge whether the definitions of the two classes are the same. It is generally recommended that this serialization version be manually defined by our programmers rather than automatically generated by the JVM Yes.

Transient keyword: when serializing, if you want a field in the class not to serialize it into a file, you only need to add this transient keyword when defining the attribute.

Generate serialization version number manually: Generally speaking, after a class is defined, it is impossible to guarantee that it will never be modified, but after it is modified, its serialization version number will change, which will cause the data previously serialized into the file to not be deserialized back. This result is usually not good, Therefore, it is recommended to manually write a serialization version number for the classes to be serialized, rather than using the method of automatic generation by the JVM. The manual writing method is to add a serialized version number attribute in the class definition, that is, "private static final long serialVersionUID = 233l;", Generally speaking, the attribute value here needs to ensure that it is the only number in the project, so it can be customized according to the project situation. If you don't trust it, You can also generate a globally unique uid by yourself (idea can also automatically generate this attribute. Search the serialVersionUID directly in the setting interface. You can see that there is a corresponding inspection item "serializable class without 'serialVersionUID" under inspections, which can be checked).

Use example:

Ordinary Java object

Serialize a single Java object

Serialize multiple Java objects

Deserialize Java objects

10. IO and properties are used together to read the configuration file. If each line of data in a text file is like this, "key = value" uses an equal sign or colon (colon is not recommended) separated into two parts, you can use a file input stream to read it, then use the load method of the properties object to load the file stream input object, and then pass in the string to the left of the equal sign in the getproperty method of properties to get the string to the right of the equal sign.

user-conf.properties

Resource binder: for the configuration files in the above example, the more common method is to use the resource binder instead of using the properties object. The specific examples are as follows:

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>