在Java当中如何优雅地处理临时文件

背景

最近接手了同事的代码,发现他是这样处理excel文件的:

1
2
3
复制代码1. 将文件保存到一个事先定义好的目录;如果目录不存在则新建
2. 使用excel处理工具根据文件路径读取文件内容之后处理业务逻辑
3. 编写一个定时任务每天凌晨1点删除该目录内的.xlsx文件

这样虽然可以达到效果,但实在是繁琐至极,非常不优雅。其实jdk中就提供了处理临时文件(Temporary File)的方法,现在让我们来看一看。

创建临时文件

在java中创建临时文件有许多场景,但是大多数是在单元测试或者是对上传的文件进行内容处理。当测试用例或者文件处理完成后,你并不关心文件是否还存在。况且持续累积的无效文件无疑会浪费许多磁盘空间。

通过使用java.io.File.createTempFile()创建临时文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
复制代码public class TemporaryFileExample
{
public static void main(String[] args)
{
File temp;
try
{
temp = File.createTempFile("myTempFile", ".txt");

System.out.println("Temp file created : " + temp.getAbsolutePath());
} catch (IOException e)
{
e.printStackTrace();
}
}
}

windows系统中的输出: C:\Users\admin\AppData\Local\Temp\myTempFile7295261447112135643.txt

通过使用NIO创建临时文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
复制代码public class TemporaryFileExample
{
public static void main(String[] args)
{
try
{
final Path path = Files.createTempFile("myTempFile", ".txt");
System.out.println("Temp file : " + path);


} catch (IOException e)
{
e.printStackTrace();
}
}
}

windows系统中的输出: C:\Users\admin\AppData\Local\Temp\myTempFile3492283537103788196.txt

写入临时文件

比如在文件上传的时候,我们就可以将字节流写入临时文件当中。

使用java.io.BufferedWriter写入临时文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
复制代码import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

public class TemporaryFileExample
{
public static void main(String[] args)
{
File temp;
try
{
temp = File.createTempFile("myTempFile", ".txt");

BufferedWriter bw = new BufferedWriter(new FileWriter(temp));
bw.write("This is the temporary data written to temp file");
bw.close();

System.out.println("Written to Temp file : " + temp.getAbsolutePath());
} catch (IOException e)
{
e.printStackTrace();
}
}
}

使用NIO写入临时文件

如果你要使用java NIO库,你就可以使用Files.write()方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
复制代码public class TemporaryFileExample
{
public static void main(String[] args)
{
try
{
final Path path = Files.createTempFile("myTempFile", ".txt");
System.out.println("Temp file : " + path);


byte[] buf = "some data".getBytes();
Files.write(path, buf);

} catch (IOException e)
{
e.printStackTrace();
}
}
}

删除临时文件

删除临时文件是非常重要的一步,因为你不想让你的磁盘空间爆炸。
为了当在应用exit时(jvm终止)删除文件,你可以使用:

1
2
复制代码File temp = File.createTempFile("myTempFile", ".txt");
temp.deleteOnExit();

或者如果你想要立马删除文件,你可以直接使用delete()方法

1
2
复制代码File temp = File.createTempFile("myTempFile", ".txt");
temp.delete();

使用IO删除临时文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
复制代码import java.io.File;
import java.io.IOException;

public class TemporaryFileExample
{
public static void main(String[] args)
{
File temp;
try
{
temp = File.createTempFile("myTempFile", ".txt");

System.out.println("Temp file created : " + temp.getAbsolutePath());

//temp.delete(); //立即删除

temp.deleteOnExit(); //运行结束时删除

System.out.println("Temp file exists : " + temp.exists());
} catch (IOException e)
{
e.printStackTrace();
}
}
}

使用NIO删除临时文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
复制代码public class TemporaryFileExample
{
public static void main(String[] args)
{
try
{
final Path path = Files.createTempFile("myTempFile", ".txt");
System.out.println("Temp file : " + path);

// Files.delete(path); //立即删除文件

Files.deleteIfExists(path);



} catch (IOException e)
{
e.printStackTrace();
}
}
}

本文转载自: 掘金

开发者博客 – 和开发相关的 这里全都有

0%