Use of JAVA Temporary Files

Table of contents

What are temporary files?

Temporary files have various magical uses in programming

The way java creates temporary files in the cache directory

1 Create the file name according to the specified file name + random number

2 Create according to the specified file name

3 by getting the real path of the temp folder


What are temporary files?

Temporary files are files used in computer systems to temporarily store data. They are usually used to store temporary data, intermediate calculation results, or data that needs to be temporarily saved during program execution.
 Temporary files have the following characteristics:
1. Temporary: Temporary files usually only exist within a specific period of time, and are deleted or cleaned up after use.
2. Store temporary data: They are used to store data that needs to be temporarily saved during program execution, such as cached data, temporary calculation results, etc.
3. Temporary folder: The operating system provides a folder dedicated to storing temporary files, usually called a temporary folder or temporary directory. On most operating systems, the path to the temporary folder can be obtained using the `java.io.tmpdir` system property.
4. Automatic cleaning: Temporary files are usually automatically deleted or cleaned up when the program execution ends or the system restarts. This ensures that temporary files do not take up too much disk space.
 Temporary files are widely used in many applications, such as caching, file transfer, temporary storage, error logging, etc. They provide a temporary data storage method to help programs be more efficient and flexible when processing data.

Temporary files have various magical uses in programming


 1. Cache : Temporary files can be used as cache storage, especially when dealing with large amounts of data. You can write intermediate calculation results or temporary data to temporary files to reduce memory usage and improve performance.
 2. File transfer : During the file transfer process, you can write the file to a temporary file before transferring it. This ensures that no original file content is lost during transfer. Such as WeChat, qq transfer files
 3. Temporary storage: If you need to save some temporary data during program execution, but do not need long-term storage, temporary files are a good choice. You can write data to a temporary file, which is automatically deleted after the program ends.
 4. Error log: When an error or exception occurs in the program, you can write the error information to a temporary file for later troubleshooting and analysis.
 5. Database backup: When performing database backup, you can first write the backup data into a temporary file, and then import it into the target database.
 These are just some of the common uses for temporary files, and you are really free to play around with your needs.

Note that when working with temporary files, it is important to ensure that these are properly disposed and cleaned up to avoid resource leaks and security issues.

The way java creates temporary files in the cache directory

1 Create the file name according to the specified file name + random number

        File tempFile = File.createTempFile("temp_XXX", ".jpg");
        System.out.println(tempFile.getAbsolutePath());

result 

It should be noted that there is the following judgment in the source code, the length of the file name cannot be less than 3

 

2 Create according to the specified file name

        File file = new File(Files.createTempDir(), "temp_YYY.jpg");
        System.out.println(file.getAbsolutePath())

result

3 by getting the real path of the temp folder

        String s = System.getProperty("java.io.tmpdir");
        System.out.println("临时文件路径为:"+s);

Then you can create at will; temporary files 

 

Guess you like

Origin blog.csdn.net/qq_53679247/article/details/131493712