Home > Mobile >  Android7.1 how to delete folders and files under/data/logs?
Android7.1 how to delete folders and files under/data/logs?

Time:09-24

 package com. Example. A0113_log; 

The import android. App. The Activity;
The import android. Content. PM. PackageManager;
The import android. OS. Bundle;
The import android. Util. Log;

The import androidx. Appcompat. App. AppCompatActivity;
The import androidx. Core. App. ActivityCompat;

import java.io.File;
Import the Java. Text. SimpleDateFormat;
import java.util.ArrayList;
Import the Java. Util. Collections;
The import java.util.Com parator;
import java.util.Date;
Import the Java. Util. List;

/* *
* 2020.1.13 demand: screening of files/folders in five days, more than five days of file deletion
* refer to: https://blog.csdn.net/fujiakai/article/details/54087773
* https://blog.csdn.net/qq_41201881/article/details/81086535
*/
Public class MainActivity3 extends AppCompatActivity {
//need to find the document root path
//private String rootPath="/sdcard/Download";
Private String rootPath="/data/logs/";
Private String TAG="wangp";
Private static final ints REQUEST_EXTERNAL_STORAGE=1;
Private static String [] PERMISSIONS_STORAGE={
"Android. Permission. READ_EXTERNAL_STORAGE,"
"Android. Permission. WRITE_EXTERNAL_STORAGE"};

/* *
* dynamic permissions apply, android6.0 later read sdcard must apply for the
* @ param activity
*/
Public static void verifyStoragePermissions Activity (Activity) {

Try {
//check whether have write permissions
Int permission=ActivityCompat. CheckSelfPermission (activity,
"Android. Permission. WRITE_EXTERNAL_STORAGE");
If (permission!=PackageManager. PERMISSION_GRANTED) {
//do not have permission to write, to apply for a write permissions, will pop up a dialog
ActivityCompat. RequestPermissions (activity, PERMISSIONS_STORAGE REQUEST_EXTERNAL_STORAGE);
}
} the catch (Exception e) {
e.printStackTrace();
}
}

@ Override
Protected void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
The setContentView (R.l ayout. Activity_main);
VerifyStoragePermissions (this);

//1. Get all under/data/logs/folder name and the last modified time
//testFileDirOrName rootPath ();
//File tmpFile=new File (rootPath + "the 20-01-14-20-01-14/");
.//long modifileTime=tmpFile lastModified ();
//the Log i. (TAG, "modifiy time:" + modifileTime);
//2. Delete the last modification time is greater than 5 days of the folder
Del5Days rootPath ();
}

/* *
* refer to: http://www.voidcn.com/article/p-eeswpcan-bpm.html
* delete more than five days of file
* @ param path
*/
Private void del5Days (String path) {
Long fiveDayTime=5 l * 24 * 60 * 60 * 1000 l l l l;//note that don't write L may lead to negative results
Long currentTime=System. CurrentTimeMillis ();
The File dirFile=new File (path);
{if (dirFile. The exists ())
The File files []=dirFile. ListFiles ();
If (files!=null) {
For (the File fileChildDir: files) {
//output file or folder name
//System. Out. Print (" "+ fileChildDir getName ());
If (fileChildDir isDirectory ()) {
Long lastTime=fileChildDir. LastModified ();
Long diffenTime=currentTime - lastTime.
If (diffenTime> FiveDayTime)//is more than five days
{
The Log i. (TAG, "del=====& gt;" + + fileChildDir path. The getName ());
Try {
String tmpPath=path + fileChildDir. GetName () + "/";
DelFiles (tmpPath);
FileChildDir. Delete ();
The Log i. (TAG, "delete success!" );
} the catch (Exception e) {
e.printStackTrace();
The Log i. (TAG, "delete the error" + e. oString ());
}
}
}
If (fileChildDir isFile ()) {
//System. Out. Println (fileChildDir getName () + ": this is a file name");
}
}
}
} else {
System. The out. Println (" do you want to find the file does not exist ");
return ;
}
}

/* *
* recursive delete specified path file
* @ return
*/
Public void delFiles (String filePath) {
The File File=new File (filePath);
if(! File. The exists ()) {
return;
}
String [] list=file. The list ();
The File temp=null;
String path=null;
For (String item: the list) {
Path=filePath + File. The separator + item;
Temp=new File (path);
{if (temp. IsFile ())
Temp. The delete ();
continue;
}
{if (temp. IsDirectory ())
DelFiles (path);
The new File (path). The delete ();
continue;
}
}
}

/* *
*
* @ param srcTime long
the primitive data types* @ param format MM - dd yyyy - HH: MM: ss
* @ return String format output time, failure was to long to output the String
*/
Public String LongTime2DateFormat (long srcTime, String format) {
Try {
The Date d=new Date (srcTime);
String time=new SimpleDateFormat (format). The format (d);
The return time.
} the catch (Exception e) {
e.printStackTrace();
The Log. I (" wan ", "LongTime2DateFormat error:" + e. oString ());
Return srcTime + "";
}
}

/* *
*
* access to all files in directory
*
* @ param realpath
* @ param files
* @ return
*/
Public static List GetFiles (String realpath List Files) {
The File realFile=new File (realpath);
If (realFile isDirectory ()) {
The File [] subfiles.=realFile listFiles ();
For (the File File: subfiles) {
If (the file. The isDirectory ()) {
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related