File and directory management in Java
Directories are a special mechanism for managing files. Saving similar files in the same directory can not only simplify file management, but also improve work efficiency. The Java language defines a File class in the java.io package that is specifically used to manage disk files and directories.
Each File class object represents a disk file or directory, and its object properties contain relevant information about the file or directory. By calling various methods provided by the File class, you can create, delete, rename files, judge the read and write permissions of the file, and set and query the latest modification time of the file. 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 the corresponding file name or directory name. The constructor of the File class is shown in the table.
When using the constructor of the File class, you need to pay attention to the following points:
(1) The path parameter can be an absolute path, a relative path, or a directory on disk.
(2) Since different operating systems use different directory separators, you can use a static variable System.dirSep of the System class to implement paths that are common under different operating systems. like:
"d:"+System.dirSep+"myjava"+System.dirSep+"file"
Get properties and operations
With the help of File objects, you can obtain and manage and operate the attribute information of files and related directories. Table 10-10 lists its commonly used methods and descriptions.
[Example] Determine that the absolute path entered represents a file or a directory. If it is a file, output the absolute path of this file and determine the file attributes of this file (whether it can be read, written or hidden); if it is a directory, output all files in this directory (excluding hidden files) (see source code).
The operation results are shown in the figure:
Basic concepts of Java input and output (IO) and streams
Input and output (I/O) refers to the operation in which a program interacts with an external device or other computer. Almost all programs have input and output operations, such as reading data from a keyboard, reading data from local or network files, or writing data. Information can be received from the outside world through input and output operations, or transmitted to the outside world. Java implements these input and output operations with streams and represents them through a unified interface, thus making programming simpler.
The concept of Java streaming
Stream refers to the flow of data between components during the input and output operations of a computer. According to the transmission direction of data, the stream can be divided into input stream and output stream. The data in the stream sequence in Java language can be either raw binary data that has not been processed or data that meets a certain format after a certain encoding process.
1. Input and output streams In Java, different types of input and output sources are abstracted into streams, where the input and output data are called Data Streams. A data stream is a channel for Java programs to send and receive data. The data stream includes an input stream (Input Stream) and an output stream (Output Stream). Usually, the input stream is used to read out the data and the output stream is written into the data. The characteristic of streaming input and output is that the acquisition and transmission of data are carried out in the sequence of data. Compared with a 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. Generally speaking, there are the following points regarding the characteristics of the stream:
First in, first out, data written to the output stream is first read by the input stream.
Sequential access, you can write a string of bytes into the stream one by one, and a string of bytes will be read in the write order when reading, and the intermediate data cannot be accessed 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 and read data, you must Two streams are provided separately.
2. In order to improve the data transmission efficiency, the concept of buffered stream is introduced, that is, equipped with a buffer (buffer) for a stream, and a buffer is a piece of memory specially used to transmit data.
When data is written to a buffer stream, the system sends the data to the buffer instead of directly sending it to an external device. The buffer automatically records data. When the buffer is full, the system sends all the data to the corresponding external device. 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 the data from the relevant external devices and read as much data as possible. Full buffer. The purpose of using data streams to process input and output is to make the program's input and output operations independent of the relevant devices. Since the program does not need to pay attention to the details of the implementation of the specific device (the specific details are processed by the system), for various input and output devices, only for the stream Just do the processing without modifying the source program, thereby enhancing the portability of the program.
I/O Streaming Class Overview
To facilitate stream processing, the Java language provides the java.io package, in which each class represents a specific input or output stream. In order to use these stream classes, this package needs to be introduced during programming. Java provides two types of input and output streams: one is a byte-oriented stream, and the data processing is based on bytes; the other is a character-oriented stream, used for character data processing. Byte Stream reads and writes an 8-bit binary number every time, also known as a binary byte stream or bit stream. A character stream reads and writes a 16-bit binary number at a time and processes it as a character instead of a binary bit. It should be noted that in order to satisfy the international representation of characters, the character encoding of Java language uses 16-bit Unicode code, while ordinary text files use 8-bit ASCⅡ code.
The hierarchy of classes in java.io is shown in the figure.
For some frequent device interactions, the Java language system reserves 3 stream objects that can be used directly, namely:
The steps of using byte streams and character streams 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 the stream object to input data from the stream, and finally execute the close() method Close the stream.