Android NDK file traversal and deletion

Source: Internet
Author: User

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);


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.