在 Android 中创建临时文件 [英] Creating temporary files in Android

查看:28
本文介绍了在 Android 中创建临时文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Android 中创建临时文件的最佳方法是什么?

What's the best way to create a temporary file in Android?

可以 File.createTempFile 能用吗?文档对此非常模糊.

Can File.createTempFile be used? The documentation is very vague about it.

特别是,使用 File.createTempFile 创建的临时文件何时被删除(如果有的话)并不清楚.

In particular, it's not clear when temporary files created with File.createTempFile are deleted, if ever.

推荐答案

我通常这样做:

File outputDir = context.getCacheDir(); // context being the Activity pointer
File outputFile = File.createTempFile("prefix", ".extension", outputDir);

至于他们的删除,我也不确定.因为我在缓存的实现中使用了它,所以我手动删除了最旧的文件,直到缓存目录大小下降到我的预设值.

As for their deletion, I am not complete sure either. Since I use this in my implementation of a cache, I manually delete the oldest files till the cache directory size comes down to my preset value.

这篇关于在 Android 中创建临时文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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