Android 刪除某天前的檔案

APP 內要實作出以下功能

刪除資料夾 shared_prefs 下, 3 天前,檔名中包含 pleaseDeleteMe 的檔案

 

所以本文介紹以下內容

1. 偵測某檔案是否上次修改時間距今已超過 3 天 ?

2. 抓取資料夾 shared_prefs 底下,檔名中包含 pleaseDeleteMe 的所有檔案

3. 刪除資料夾 shared_prefs 底下,檔名中包含 pleaseDeleteMe 的,且上次更新日期已是 3 天前的所有檔案

 

 

1. 偵測某檔案是否上次修改時間距今已超過 3 天 ?

[sourcecode language=”JAVA”]

if (file.exists()) {
Calendar time = Calendar.getInstance();
time.add(Calendar.DAY_OF_YEAR, -3); // -3 代表 3 天前

Date lastModified = new Date(file.lastModified());
if(lastModified.before(time.getTime()))
{
//如果檔案上次修改時間距今 3 天前了,要做些甚麼
// 例如,可以刪除此檔案 file.delete();
}
}
[/sourcecode]

 

 

 

2. 取得資料夾 shared_prefs 底下,檔名中包含 pleaseDeleteMe 的所有檔案

[sourcecode language=”JAVA”]

//利用下方 getAllFilesInDir() 取得 shared_prefs 資料夾底下的檔案
ArrayList files = getAllFilesInDir(new File("/data/data/package_name/shared_prefs"));
//取得某資料夾底下的所有檔案
public static ArrayList<File> getAllFilesInDir(File dir) {
if (dir == null)
return null;

ArrayList<File> files = new ArrayList<File>();

Stack<File> dirlist = new Stack<File>();
dirlist.clear();
dirlist.push(dir);

while (!dirlist.isEmpty()) {
File dirCurrent = dirlist.pop();

File[] fileList = dirCurrent.listFiles();
for (File aFileList : fileList) {
if (aFileList.isDirectory())
dirlist.push(aFileList);
else
files.add(aFileList);
}
}

return files;
}
[/sourcecode]

 

 

3. 刪除資料夾 shared_prefs 底下,檔名中包含 pleaseDeleteMe 的,且上次更新日期已是 3 天前的所有檔案

[sourcecode language=”JAVA”]

//呼叫刪除檔案

deleteFilesInSharedPrefs();

//幫我刪除 shared_prefs 底下,檔名中包含 pleaseDeleteMe 的,且上次更新日期已是 3 天前的所有檔案
private void deleteFilesInSharedPrefs() {
//先取得資料夾底下所有檔案
ArrayList files = getAllFilesInDir(new File("/data/data/package_name/shared_prefs"));

//定規則,只取出檔名裡有 pleaseDeleteMe 字串的檔案
Pattern pattern = Pattern.compile("pleaseDeleteMe");

for (int i = 0; i < files.size(); i++) {
File file = (File) files.get(i);
if (file.exists()) {
Calendar time = Calendar.getInstance();
time.add(Calendar.DAY_OF_YEAR, -3); //判斷要刪除的時間點(三天前的)
Date lastModified = new Date(file.lastModified()); //檔案上次修改時間

if (lastModified.before(time.getTime())) {

//依規則比對檔名
Matcher matcherPleaseDeleteMeFile = pattern.matcher(file.getName());

//如果有找到
while (matcherPleaseDeleteMeFile.find()) {
file.delete(); //刪除
}
}

}
}
}

//取得某資料夾底下的所有檔案
public static ArrayList<File> getAllFilesInDir(File dir) {
if (dir == null)
return null;

ArrayList<File> files = new ArrayList<File>();

Stack<File> dirlist = new Stack<File>();
dirlist.clear();
dirlist.push(dir);

while (!dirlist.isEmpty()) {
File dirCurrent = dirlist.pop();

File[] fileList = dirCurrent.listFiles();
for (File aFileList : fileList) {
if (aFileList.isDirectory())
dirlist.push(aFileList);
else
files.add(aFileList);
}
}

return files;
}
[/sourcecode]

 

16 關於 “Android 刪除某天前的檔案” 的評論

  1. excellence

    I blog quite often and I genuinely thank you for your
    information. This article has truly peaked my interest.
    I’m going to book mark your website and keep checking for
    new information about once a week. I opted
    in for your RSS feed too.

    回覆
  2. Jeannine

    Consists of global and residential nutrition programs, vital
    public health companies, neighborhood analysis approaches, as well as community interaction (2-credits).
    authorizations, such as the Fda and also The
    Nourishment Collection at nationwide level is actually led due to the Team from
    Nourishment from the Administrative agency from Wellness
    as well as co-led through UNICEF and ACF. Tim Johnson on the ABC NewsNow network,
    UNITED STATE authorizations, such as the Fda and also Learn the fundamentals of the Health and nutrition Truth Door, the meaning from
    health claims, phrases as well as symbolic representations on food items packing, and how to utilize that information to determine healthy selections.
    Tim Johnson on the ABC NewsNow system, Lee’s wish to aid others reach their health
    and wellness as well as nourishment objectives influenced him to discovered Labrada Nutrition, and also make his collection of award-winning
    sports health and nutrition as well as supplement
    items.

    回覆
  3. BestRussel

    I have noticed you don’t monetize your page, don’t waste
    your traffic, you can earn additional cash every month because you’ve got high quality content.
    If you want to know how to make extra money, search for: Ercannou’s essential tools best adsense alternative

    回覆
  4. MScarlett

    Hi, is anybody here interested in online job? It’s simple survey filling.
    Even 10$ per survey (10 minutes duration). If you are interested, send me email to hans.orloski[at]gmail.com

    回覆
  5. concrete5 freelancer

    Hello fantastic website! Does running a blog
    similar to this require a lot of work? I’ve virtually no knowledge of computer programming however I
    was hoping to start my own blog soon. Anyhow, should
    you have any ideas or tips for new blog owners please share.
    I know this is off subject but I simply had to ask.
    Thanks!

    回覆
  6. colleges that require act scores

    Unquestionably believe that which you said. Your favorite reason seemed to
    be on the web the simplest thing to be aware of. I say to you, I definitely get annoyed while people think
    about worries that they just don’t know about.
    You managed to hit the nail upon the top and also defined out the whole thing without
    having side-effects , people could take a signal.
    Will likely be back to get more. Thanks

    回覆

發表迴響

你的電子郵件位址並不會被公開。 必要欄位標記為 *