Detailed explanation of java file and directory management and input and output related operations

File and directory management in Java

Directory is a special mechanism for managing files. Saving similar files in the same directory can not only simplify file management, but also improve work efficiency. Java language in Java A file class is defined in the IO package to manage disk files and directories.

Each file class object represents a disk file or directory, and its object properties contain information about the file or directory. By calling various methods provided by the file class, you can create, delete and rename files, judge the read-write permission and existence of files, and set and query the latest modification time of files. Different operating systems have different file system organization methods. By using file class objects, Java programs can process files and directories in a platform independent and unified way. Create an object of the file class

To create a file class object, you need to give its corresponding file name or directory name. The construction method of file class is shown in the table.

Get properties and actions

With the help of file object, the attribute information of files and related directories can be obtained, managed and operated. Table 10-10 lists the common methods and descriptions.

The operation results are shown in the figure:

Basic concepts of Java input output (IO) and stream

Input and output (I / O) refers to the operation of the program interacting with external devices or other computers. Almost all programs have input and output operations, such as reading data from the keyboard, reading data or writing data from local or network files, etc. through input and output operations, you can receive information from the outside world or transfer information to the outside world. Java uses these input and output operations It is realized by action flow and expressed by unified interface, which makes the program design simpler. Concept of Java stream

Stream refers to the data flow between various components in the input and output operation of the computer. According to the transmission direction of data, stream can be divided into input stream and output stream. The data in the stream sequence in Java language can be raw binary data or data in a specific format after certain coding processing.

1. Input / output stream in Java, different types of input / output sources are abstracted as streams, The input and output data is called data stream. Data stream is a channel for Java programs to send and receive data. Data stream includes input stream and output stream (output stream). Usually, the input stream is used in the application program to read data and the output stream to write data. The characteristic of streaming input and output is that the acquisition and transmission of data are carried out in the order of data sequence. Compared with the program, the output stream writes data to the storage medium or data channel, while the input stream reads data from the storage medium or data channel, which is generally closed The characteristics of the stream are as follows: first in, first out. The data first written to the output stream is first read by the input stream. Sequential access, you can write a string of bytes to the stream one by one. When reading, you will also read a string of bytes in writing order, and you cannot access the intermediate data randomly. Read only or write only. Each stream can only be one of the input stream or output stream, and cannot have two functions at the same time. In a data transmission channel, if you want to write data and read data, you need to provide two streams respectively.

2. Buffer stream in order to improve the data transmission efficiency, the concept of buffered stream is introduced, that is, a stream is equipped with a buffer, which is a piece of memory dedicated to transmitting data.

When writing data to a buffer stream, the system sends the data to the buffer rather than directly to an external device. The buffer automatically records data. When the buffer is full, the system sends all the data to the corresponding external devices. When reading data from a buffer stream, the system actually reads data from the buffer. When the buffer is empty, the system will automatically read data from relevant external devices and read as much data as possible to fill the buffer. The purpose of using data flow to process input and output is to make the input and output operation of the program independent of relevant devices. Because the program does not need to pay attention to the details of the implementation of specific devices (the specific details are processed by the system), it is possible to process various input and output devices as long as the stream is processed without modifying the source program, so as to enhance the portability of the program. Overview of I / O flow classes

In order to facilitate the processing of streams, the Java language provides Java IO package, in which each class represents a specific input or output stream. In order to use these flow classes, this package needs to be introduced during programming. Java provides two types of input and output streams: one is byte oriented stream, and data processing takes bytes as the basic unit; The other is character oriented stream, which is used for character data processing. Byte stream (byte stream) reads and writes 8-bit binary numbers each time, also known as binary byte stream or bit stream. The character stream reads and writes 16 bit binary numbers at a time and treats them as a character instead of a binary bit. It should be noted that in order to meet the international representation of characters, the character coding of Java language adopts 16 bit Unicode code, while the 8-bit ASC Ⅱ code is used in ordinary text files 。

java. The hierarchy of classes in io is shown in the figure.

For some frequent device interactions, the Java language system has reserved three stream objects that can be used directly, namely:

The steps of using byte stream and character stream in Java language are basically the same. Taking the input stream as an example, first create a stream object related to the data source, then use the method of stream object to input data from the stream, and finally execute the close () method to close the stream.

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
分享
二维码
< <上一篇
下一篇>>