File and directory management in java
Directories are special mechanisms for managing files,Keeping similar files in the same directory not only simplifies file management,It also improves work efficiency.The java language defines a file class in the java.io package specifically for managing disk files and directories.
Each file class object represents a disk file or directory,Its object properties contain information about files or directories.By calling various methods provided by the file class,Ability to create, delete, rename files, determine file read and write permissions, and whether they exist,Set and query the file's last modification time, etc.Different operating systems have different ways of organizing file systems,By using the file class object, a java program can process files and directories in a platform-independent and uniform manner.
Create an object of class file
To create a file object, you need to give its corresponding file name or directory nameThe constructor of the file class is shown in the table.
When using the constructor of the file class,Note the following:
(1) The path parameter can be an absolute path,Can also be a relative path,It can also be a directory on disk.
(2) Due to different directory separators used by different operating systems,You can use a static variable system.dirsep of the system class to implement a common path under different operating systems.Such as:
"d:" + system.dirsep + "myjava" + system.dirsep + "file"
Get attributes and operations
With the help of the file object, you can get attribute information about files and related directories and manage and manipulate them.Table 10-10 lists the commonly used methods and descriptions.
[Example] Determine whether the input absolute path represents a file or a directory.If the file is an absolute path to this file,And judge the file attributes of this file (whether it is readable, writable or hidden);if it is a directory, output all files (excluding hidden files) in the directory (see source code).
The results are shown in the figure:
Basic concepts of java input and output (io) and streams
Input/output (i/o) refers to the operation of the program to interact with external devices or other computers.Almost all programs have input and output operations,If you read data from the keyboard,Read data from or write to files on the local or network.Information can be received from the outside through input and output operations,Or pass the information to the outside world.Java implements these input and output operations with streams,Represented by a unified interface,This makes programming easier.
java stream concept
Stream (stream) refers to the flow of data between components in the input and output operations of a computer.According to the direction of data transmission,Streams can be divided into input streams and output streams.The data in the stream sequence in the Java language can be raw raw binary data,It can also be data that conforms to a specific format after a certain encoding process.
1. Input and output streams
In java, abstract different types of input and output sources into streams,The input and output data is called a data stream. A data stream is a channel through which Java programs send and receive data.The data stream includes an input stream and an output stream. Usually input data is read out in the application,Data is written to the output stream. The characteristic of streaming input and output is that data acquisition and transmission are performed along the data sequence.Relative to the program,The output stream is writing data to a storage medium or data channel.The input stream reads data from a storage medium or data channel.Generally speaking about the characteristics of the stream:
FIFO,The data written first to the output stream is read first by the input stream.
Sequential access,You can write a string of bytes to the stream one by one,When reading, a string of bytes will also be read in the writing order.Intermediate data cannot be accessed randomly.
Read-only or write-only,Each stream can only be one of an input stream or an output stream.Cannot have two functions at the same time,In a data transmission channel,If i want to write data,Read the data again,Provide two streams separately.
2. Buffer stream
In order to improve the efficiency of data transmission,The concept of a buffered stream is introduced, that is, a buffer is provided for a stream, and a buffer is a piece of memory dedicated to transmitting data.
When writing data to a buffered stream,The system sends the data to the buffer,Instead of sending directly to an external device.Buffer automatically records data,When the buffer is full,The system sends all data to the corresponding external device.When reading data from a buffered stream,The system is actually reading data from the buffer,When the buffer is empty,The system will automatically read the data from the relevant external device,And read as much data as possible to fill the buffer. The purpose of using data streams to process input and output is to make the program's input and output operations independent of related equipment.Because the program does not need to pay attention to the details of specific device implementation (the specific details are handled by the system), for various input and output devices,Just process the stream,No need to modify the source program,This enhances the portability of the program.
i/o stream class overview
To facilitate stream processing,The java language provides the java.io package. Each class in this package represents a specific input or output stream.To use these stream classes,This package needs to be introduced when programming. Java provides two types of input and output streams:a byte-oriented stream,Data processing is based on bytes;The other is a character-oriented stream,Used for character data processing.A byte stream reads and writes 8-bit binary numbers at a time.Also called a binary byte stream or bit stream.The character stream reads and writes 16-bit binary numbers at once,And treat it as a character instead of a binary bit.have to be aware of is,To satisfy the international representation of characters,The character encoding of the Java language uses a 16-bit unicode code, while the ordinary text file uses an 8-bit ascII 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.They are:system.in (standard input), which usually represents keyboard input. system.out (standard output):Usually written to the display. system.err (standard error output):Usually written to the display.
The steps for using byte and character streams in Java are basically the same.Take 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,Finally, the close () method is executed to close the stream.
- Detailed usage of ByteArray input and output stream in Java
- Java implementation of input and output stream code sharing
- Java implementation of text box and text area input and output
- Detailed usage of FilterInputStream and FilterOutputStream in Java
- Example of using BufferedInputStream and BufferedOutputStream in Java
- python - you may need to restart the kernel to use updated packages error
- php - coincheck api authentication doesn't work
- php - i would like to introduce the coincheck api so that i can make payments with bitcoin on my ec site
- [php] i want to get account information using coincheck api
- the emulator process for avd pixel_2_api_29 was killed occurred when the android studio emulator was started, so i would like to
- python 3x - typeerror: 'method' object is not subscriptable
- i want to call a child component method from a parent in vuejs
- xcode - pod install [!] no `podfile 'found in the project directory
- dart - flutter: the instance member'stars' can't be accessed in an initializer error