java实现创建临时文件然后在程序退出时自动删除文件
作者:hebedich 时间:2023-02-15 18:27:00
通过java的File类创建临时文件,然后在程序退出时自动删除临时文件。下面将通过创建一个JFrame界面,点击创建按钮在当前目录下面创建temp文件夹且创建一个以mytempfile******.tmp格式的文本文件。代码如下:
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);
}
}
}
效果图:
点击创建临时文件效果图:
非常简单实用的功能,希望小伙伴们能够喜欢。
标签:java,自动删除,临时文件
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
android viewpager实现竖屏滑动效果
2021-12-19 18:05:44
![](https://img.aspxhome.com/file/2023/7/125527_0s.gif)
Android Studio实现标题栏和状态栏的隐藏
2022-11-29 08:45:10
Java基础学习之构造方法详解
2021-06-16 16:25:53
spring boot starter actuator(健康监控)配置和使用教程
2021-07-29 06:24:18
SpringCloud Feign实现微服务之间相互请求问题
2022-08-29 08:20:53
![](https://img.aspxhome.com/file/2023/6/119136_0s.jpg)
Java注解@Transactional事务类内调用不生效问题及解决办法
2022-04-03 18:42:06
Java编写日历表的3种方式
2023-02-16 00:50:31
![](https://img.aspxhome.com/file/2023/8/70978_0s.jpg)
Springboot如何通过yml配置文件为静态成员变量赋值
2021-08-03 12:03:17
![](https://img.aspxhome.com/file/2023/1/78391_0s.png)
C#多线程学习之(三)生产者和消费者用法分析
2021-08-11 01:55:26
Java 8 中的 10 个特性总结及详解
2023-07-21 00:06:51
![](https://img.aspxhome.com/file/2023/0/65200_0s.png)
IDEA远程连接HBase及其Java API实战详解
2023-11-27 21:54:46
![](https://img.aspxhome.com/file/2023/9/60169_0s.png)
深入学习Android ANR 的原理分析及解决办法
2023-01-02 22:43:01
![](https://img.aspxhome.com/file/2023/1/104621_0s.png)
23种设计模式(12)java模版方法模式
2021-10-24 11:00:31
![](https://img.aspxhome.com/file/2023/6/94306_0s.jpg)
Android实现图片加载进度提示
2022-09-11 17:54:44
![](https://img.aspxhome.com/file/2023/4/118934_0s.jpg)
C#操作JSON(序列化与反序列化)的方法详解
2022-01-27 20:15:10
详解hbase与hive数据同步
2023-02-09 00:20:36
![](https://img.aspxhome.com/file/2023/3/132013_0s.png)
android studio 项目 :UI设计高精度实现简单计算器
2022-07-14 02:09:29
![](https://img.aspxhome.com/file/2023/2/98622_0s.png)
Java编程实现高斯模糊和图像的空间卷积详解
2022-06-18 10:44:34
![](https://img.aspxhome.com/file/2023/6/90976_0s.png)
Android连接MySQL数据库并进行增删改查操作示例讲解
2023-07-03 13:20:15
实例讲解Java并发编程之闭锁
2023-10-25 14:25:07