我有一个方法从位于目录中的文件中读取内容.但由于功能原因,有必要从最旧的文件开始(由属性lastmodified指示)并以最新文件结束.
这是我打开和读取文件的代码:
FilenameFilter filter = new FilenameFilter() {
public boolean accept(File dir, String name) {
return name.matches("access_log.*");
}
};
File folder = new File("/home/myfiles");
File[] listOfFiles = folder.listFiles(filter);
for (int i = 0; i < listOfFiles.length; i++) {
String sFileName = listOfFiles[i].getName();
File accessLogFile = new File(aLog.getPath(), sFileName);
long time=accessLogFile.lastModified();
// do something with the file
}
Run Code Online (Sandbox Code Playgroud)
有没有人可以解决我如何按日期快速排序我的文件列表?
我想以编程方式删除我的应用程序的数据.我找到了clearApplicationUserData方法.但是当我运行它时,应用程序最小化了自己.也就是说,应用程序转到后台,就像按下主页按钮一样.这是我的代码:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
((ActivityManager)context.getSystemService(Context.ACTIVITY_SERVICE))
.clearApplicationUserData();
} else {
// TODO
}
Run Code Online (Sandbox Code Playgroud)
有一种方法可以使用此方法擦除数据而不会最小化应用程序?
我在测试应用程序中使用fedor的延迟加载列表实现,通过单击该按钮即可清除缓存。如何获取列表视图中已加载图像的缓存大小并以编程方式清除缓存?
这是用于保存缓存图像的代码:
public ImageLoader(Context context){
//Make the background thead low priority. This way it will not affect the UI performance.
photoLoaderThread.setPriority(Thread.NORM_PRIORITY-1);
mAssetManager = context.getAssets();
//Find the dir to save cached images
if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED))
cacheDir = new File(android.os.Environment.getExternalStorageDirectory(),"LazyList");
else
cacheDir = context.getCacheDir();
if(!cacheDir.exists())
cacheDir.mkdirs();
}
Run Code Online (Sandbox Code Playgroud)
编辑:
因此,基本上我将这段代码添加到了clearCache()方法中,但是在滚动时仍然看不到图像再次开始加载。
public void clearCache() {
//clear memory cache
long size=0;
cache.clear();
//clear SD cache
File[] files = cacheDir.listFiles();
for (File f:files) {
size = size+f.length();
if(size >= 200)
f.delete();
}
}
Run Code Online (Sandbox Code Playgroud)