如何在java中创建临时文件而不在文件名后附加随机数? [英] How to create a temp file in java without the random number appended to the filename?

查看:766
本文介绍了如何在java中创建临时文件而不在文件名后附加随机数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要创建一个临时文件,所以我尝试了这个:

I need to create a temp file, so I tried this:

String[] TempFiles = {"c1234c10","c1234c11","c1234c12","c1234c13"};
for (int i = 0; i <= 3; i++) {
    try {
        String tempFile = TempFiles[i]; 
        File temp = File.createTempFile(tempFile, ".xls"); 
        System.out.println("Temp file : " + temp.getAbsolutePath());
    } catch (IOException e) {
        e.printStackTrace();
    }
}

输出如下:

 Temp file : C:\Users\MD1000\AppData\Local\Temp\c1234c108415816200650069233.xls
 Temp file : C:\Users\MD1000\AppData\Local\Temp\c1234c113748833645638701089.xls
 Temp file : C:\Users\MD1000\AppData\Local\Temp\c1234c126104766829220422260.xls
 Temp file : C:\Users\MD1000\AppData\Local\Temp\c1234c137493179265536640669.xls

现在,我不想要添加到文件名中的额外数字(long int)。我怎样才能做到这一点?谢谢

Now, I don't want the extra numbers (long int) which is getting added to the file name. How can I achieve that? Thanks

推荐答案

首先,使用以下代码片段获取系统的临时目录:

First, use the following snippet to get the system's temp directory:

String tDir = System.getProperty("java.io.tmpdir");

然后结合使用 tDir 变量你的 tempFiles [] 数组可以单独创建每个文件。

Then use the tDir variable in conjunction with your tempFiles[] array to create each file individually.

这篇关于如何在java中创建临时文件而不在文件名后附加随机数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆