in the mobile phone development process, it is inevitable to do some local file management operations, such as many common apps such as Weibo, etc. have clear cache function, this function is to traverse the app's own cache directory, and then delete all cache files. The use of the Java file class can implement local files traversal and deletion, and so on, if you use the NDK way to implement it? Previously wrote, "using the Win32 API to traverse folders based on C + +" is not available because the Android NDK platform is part of a Linux system. Through the search of Linux file management related materials, the smooth implementation of the file traversal and deletion function, the following code, need to include <dirent.h>header file.
Delete all files in the specified folder Jniexport void Jnicall java_com_test_util_t_deletefiles (jnienv *env, Jclass obj, jstring filefolder) { const char *file_folder = (*env)->getstringutfchars (env, Filefolder, 0);D ir *pdir = null;struct dirent *dmsg;char szFil Ename[128];char szfoldername[128];strcpy (Szfoldername, File_folder); Strcat (Szfoldername, "/%s"); if (PDir = Opendir ( File_folder)) = null) {//traverse the directory and delete the file while ((dmsg = Readdir (pdir)) = null) {if (strcmp (Dmsg->d_name, ".") = 0 && strcmp (Dmsg->d_name, "...")! = 0) {sprintf (szFileName, Szfoldername, dmsg->d_name); remove (szFileName);}} if (pdir! = NULL) {closedir (pdir);} (*env)->releasestringutfchars (env, Filefolder, File_folder);}
By the way, this file traversal method needs to include the <io.h> header file, in the Windows environment test can be used, but ported to the Android NDK Environment, the compile-time prompt cannot find the header file, the reason may be my ndk path configuration problem, No further detailed reasons were subsequently found.
Long hfile = 0;struct _finddata_t file_info;hfile = _findfirst ("test\\*", &file_info); while (_findnext (HFile, & F ile_info) = = 0) {if (strcmp (file_info. Name, ".")! = 0 && strcmp (file_info. Name, "...")! = 0) {printf ("%s\n", file_i Nfo.name);} else{printf ("%s\n", "Invalid");}} _findclose (hfile);