临时文件的使用

临时文件的使用

1、创建临时文件
File tempFile = File.createTempFile(prefix, suffix);

prefix :文件名的前缀

suffix :文件名的后缀

注意:

  • 当你运行上面一行代码时,会创建一个空的文件

使用场景:

例如: 你后端接收前端传来的文件MultipartFile file 需要把这个文件生成预览上传到OSS上,把这个文件保存到临时文件使用后删除。

file.transferTo(tempFile.getAbsoluteFile());
2、文件默认的位置

win10 : C:\Users[用户名]\AppData\Local\Temp\ 文件夹下

linux : /tmp 下

3、文件的删除
tempFile.deleteOnExit();  //当系统停止运行时,会自动删除该临时文件

tempFile.delete();  //当系统运行到当前代码时删除文件,该方法返回一个booleasn值
4、深入了解临时文件的创建

源码:

public static File createTempFile(String prefix, String suffix)
    throws IOException
{
    return createTempFile(prefix, suffix, null);
}

createTempFile方法源码:

public static File createTempFile(String prefix, String suffix, File directory)
    throws IOException
{
    if (prefix.length() < 3)
        throw new IllegalArgumentException("Prefix string too short");
    if (suffix == null)
        suffix = ".tmp";

    File tmpdir = (directory != null) ? directory : TempDirectory.location();
    SecurityManager sm = System.getSecurityManager();
    File f;
    do {
        f = TempDirectory.generateFile(prefix, suffix, tmpdir);
        if (sm != null) {
            try {
                sm.checkWrite(f.getPath());
            } catch (SecurityException se) {
                // don't reveal temporary directory location
                if (directory == null)
                    throw new SecurityException("Unable to create temporary file");
                throw se;
            }
        }
    } while ((fs.getBooleanAttributes(f) & FileSystem.BA_EXISTS) != 0);

    if (!fs.createFileExclusively(f.getPath()))
        throw new IOException("Unable to create temporary file");

    return f;
}

注意一下几点:

  • 前缀的长度小于3,会抛异常说前缀过短
  • 如果你后缀赋值为null,他会默认创建一个后缀为.tmp的文件
  • 如果你没有传directory文件他会使用之际默认的TempDirectory.location()

TempDirectory.location()源码:

// temporary directory location
private static final File tmpdir = new File(AccessController.doPrivileged(new GetPropertyAction("java.io.tmpdir")));
static File location() {
    return tmpdir;
}
  • TempDirectory.location() 源码解释

    • 看到这里大概😵了,解释一下,AccessController.doPrivileged(new GetPropertyAction("java.io.tmpdir"))输出为 :C:\Users\DELL\AppData\Local\Temp\,这样应该懂了吧,这个就是返回你当前系统默认的临时文件夹。
    • 然后location方法返回这个File
  • f = TempDirectory.generateFile(prefix, suffix, tmpdir);  //得到你的路径以后在这个路径下创建文件
    
  • generateFile方法:

    private static final SecureRandom random = new SecureRandom();
    static File generateFile(String prefix, String suffix, File dir)
        throws IOException
    {
        long n = random.nextLong();
        if (n == Long.MIN_VALUE) {
            n = 0;      // corner case
        } else {
            n = Math.abs(n);
        }
    
        // Use only the file name from the supplied prefix
        prefix = (new File(prefix)).getName();
    
        String name = prefix + Long.toString(n) + suffix;
        File f = new File(dir, name);
        if (!name.equals(f.getName()) || f.isInvalid()) {
            if (System.getSecurityManager() != null)
                throw new IOException("Unable to create temporary file");
            else
                throw new IOException("Unable to create temporary file, " + f);
        }
        return f;
    }
    
  • generateFile方法:

    • 随机生成一个long类型的数,如果等于最小值,则赋值为0,否则取绝对值
    • 文件名为 String name = prefix + Long.toString(n) + suffix; 前缀+随机数+后缀

后续会有补充…

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值