简体   繁体   中英

Android - How to get all file path from internal storage or delete all files from internal storage

I am in middle of a project where I am storing files in internal storage as follows:

try {
     FileWriter file = new FileWriter("/data/data/" + context.getPackageName() + "/" + filename);
     file.write(stringData);
     file.flush();
     file.close();
   } catch (IOException e) {
         e.printStackTrace();
   }

After creating the file I can able to delete the file by its name which I am giving when I am creating it as follows:

File f = new File("/data/data/" + context.getPackageName() + "/" + fileName);
f.delete();

That all is done and working.

My problem is I want to get all the files which are stored in the internal storage without specifying the name of the files.

Note: I can get the file by specifying the name one by one.

But can I get all the files from internal storage or delete all of them directly without specifying each filename.

You can get File objects for all files in the directory like this:

File f = new File("/data/data/" + context.getPackageName());
File[] files = f.listFiles();
for (File fInDir : files) {
    fInDir.delete();
}

See http://developer.android.com/reference/java/io/File.html#listFiles()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM