1.33 如何在Java中创建临时文件而不将随机数附加到文件名中?| Java Debug 笔记

2,382 阅读2分钟

本文正在参加「Java主题月 - Java Debug笔记活动」,详情查看 活动链接

Debug 笔记 <如何在Java中创建临时文件而不将随机数附加到文件名中?>

提问

我需要创建一个临时文件,因此我尝试了以下操作:

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

现在,我不希望将多余的数字(长整数)添加到文件名中。我该如何实现?

谢谢

回答一

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

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

然后,将tDir变量与tempFiles[]数组一起使用以分别创建每个文件。

回答二

可以用下面代码尝试一下

import com.google.common.io.Files;

...

File myTempFile = new File(Files.createTempDir(), "MySpecificName.png");

回答三

如果File.createTempFile用于生成临时文件名,则不能。

我查看了用于生成临时文件的Java源代码(对于Java 1.7,您没有声明您的版本,因此我只使用了我的版本):

private static class TempDirectory {
    private TempDirectory() { }

    // temporary directory location
    private static final File tmpdir = new File(fs.normalize(AccessController
        .doPrivileged(new GetPropertyAction("java.io.tmpdir"))));
    static File location() {
        return tmpdir;
    }

    // file name generation
    private static final SecureRandom random = new SecureRandom();
    static File generateFile(String prefix, String suffix, File dir) {
        long n = random.nextLong();
        if (n == Long.MIN_VALUE) {
            n = 0;      // corner case
        } else {
            n = Math.abs(n);
        }
        return new File(dir, prefix + Long.toString(n) + suffix);
    }
}

这是生成临时文件名的Java JDK中的代码。

您会看到它会生成一个随机数,并将其插入到您的文件名中,介于前缀和后缀之间。

这在“ File.java”中(在java.io中)。我看不出有任何方法可以改变这种状况。

回答四

如果要在系统范围的临时目录中创建具有特定名称的文件,请展开%temp%环境变量并手动创建文件,这没有任何问题。

编辑:实际上,System.getProperty("java.io.tmpdir"));用于此。

回答五

您可以创建一个临时目录,然后在其中存储新文件。

这样,您添加的所有新文件都不会具有随机扩展名。完成后,您要做的就是删除您添加的temp目录。

public static File createTempFile(String prefix, String suffix) {       
    File parent = new File(System.getProperty("java.io.tmpdir"));   

    File temp = new File(parent, prefix + suffix);

    if (temp.exists()) {
        temp.delete();
    }

    try {
        temp.createNewFile();
    } catch (IOException ex) {
        ex.printStackTrace();
    }

    return temp;
}


public static File createTempDirectory(String fileName) {       
    File parent = new File(System.getProperty("java.io.tmpdir"));   

    File temp = new File(parent, fileName);

    if (temp.exists()) {
        temp.delete();
    }

    temp.mkdir();

    return temp;
}

回答六

自定义名称可以保存如下

File temp=new File(tempFile, ".xls");
if (!temp.exists()) {
    temp.createNewFile();
}

文章翻译自Stack Overflow :stackoverflow.com/questions/9…