


Java implementation creates temporary files and then automatically deletes the files when the program exits
Create temporary files through Java's File class, and then automatically delete the temporary files when the program exits. Next, we will create a JFrame interface, click the Create button to create a temp folder under the current directory and create a text file in the format of mytempfile******.tmp. The code is as follows:
import java.io.*; import java.util.*; import javax.swing.*; import java.awt.event.*; /** * 功能: 创建临时文件(在指定的路径下) */ public class TempFile implements ActionListener { private File tempPath; public static void main(String args[]){ TempFile ttf = new TempFile(); ttf.init(); ttf.createUI(); } //创建UI public void createUI() { JFrame frame = new JFrame(); JButton jb = new JButton("创建临时文件"); jb.addActionListener(this); frame.add(jb,"North"); frame.setSize(200,100); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } //初始化 public void init(){ tempPath = new File("./temp"); if(!tempPath.exists() || !tempPath.isDirectory()) { tempPath.mkdir(); //如果不存在,则创建该文件夹 } } //处理事件 public void actionPerformed(ActionEvent e) { try { //在tempPath路径下创建临时文件"mytempfileXXXX.tmp" //XXXX 是系统自动产生的随机数, tempPath对应的路径应事先存在 File tempFile = File.createTempFile("mytempfile", ".txt", tempPath); System.out.println(tempFile.getAbsolutePath()); FileWriter fout = new FileWriter(tempFile); PrintWriter out = new PrintWriter(fout); out.println("some info!" ); out.close(); //注意:如无此关闭语句,文件将不能删除 //tempFile.delete(); tempFile.deleteOnExit(); } catch(IOException e1) { System.out.println(e1); } } }
Rendering:
Click to create a temporary file Rendering:
A very simple and practical function, I hope you guys will like it.
For more java implementations that create temporary files and then automatically delete the files when the program exits, please pay attention to the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Troubleshooting and solutions to the company's security software that causes some applications to not function properly. Many companies will deploy security software in order to ensure internal network security. ...

Field mapping processing in system docking often encounters a difficult problem when performing system docking: how to effectively map the interface fields of system A...

Solutions to convert names to numbers to implement sorting In many application scenarios, users may need to sort in groups, especially in one...

When using MyBatis-Plus or other ORM frameworks for database operations, it is often necessary to construct query conditions based on the attribute name of the entity class. If you manually every time...

Start Spring using IntelliJIDEAUltimate version...

Conversion of Java Objects and Arrays: In-depth discussion of the risks and correct methods of cast type conversion Many Java beginners will encounter the conversion of an object into an array...

Detailed explanation of the design of SKU and SPU tables on e-commerce platforms This article will discuss the database design issues of SKU and SPU in e-commerce platforms, especially how to deal with user-defined sales...

When using TKMyBatis for database queries, how to gracefully get entity class variable names to build query conditions is a common problem. This article will pin...
