如何在android中获取缓存目录中的文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20235898/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get files in cache directory in android?
提问by omega
In my android app, I make files in a directory I get using this function (notice its putting in a subdirectory).
在我的 android 应用程序中,我在使用此函数的目录中创建文件(注意它放在子目录中)。
gsFile = new File(getCacheDir(), "test/aa.txt");
But how do I iterate through the files in that directory now?
但是我现在如何遍历该目录中的文件?
I tried
我试过
File dir = new File(getCacheDir(), "test/aa.txt");
for (File f : dir.listFiles()) {
}
but it crashed on File f
但它在文件 f 上崩溃了
回答by kalyan pvs
Change your code like this and try..
像这样更改您的代码并尝试..
File dir = new File(getCacheDir(), "test");
if (dir.exists()) {
for (File f : dir.listFiles()) {
//perform here your operation
}
}
回答by vibhorchaudhary
Using the below code you can get all the files which are there in your app's cache directory and you can iterate over it and perform whatever is required
使用下面的代码,您可以获得应用程序缓存目录中的所有文件,您可以对其进行迭代并执行所需的任何操作
File dir = new File(mContext.getCacheDir().getAbsolutePath());
if (dir.exists()) {
for (File f : dir.listFiles()) {
f.getName();
}
}